1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // thread.c -- |
---|
8 | // |
---|
9 | // Author : Thierry Delisle |
---|
10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Fri Mar 30 17:19:52 2018 |
---|
13 | // Update Count : 8 |
---|
14 | // |
---|
15 | |
---|
16 | #include "thread.hfa" |
---|
17 | |
---|
18 | #include "kernel_private.hfa" |
---|
19 | |
---|
20 | #define __CFA_INVOKE_PRIVATE__ |
---|
21 | #include "invoke.h" |
---|
22 | |
---|
23 | extern "C" { |
---|
24 | #include <fenv.h> |
---|
25 | #include <stddef.h> |
---|
26 | } |
---|
27 | |
---|
28 | //extern volatile thread_local processor * this_processor; |
---|
29 | |
---|
30 | //----------------------------------------------------------------------------- |
---|
31 | // Thread ctors and dtors |
---|
32 | void ?{}(thread_desc & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) { |
---|
33 | context{ NULL, NULL }; |
---|
34 | self_cor{ name, storage, storageSize }; |
---|
35 | state = Start; |
---|
36 | curr_cor = &self_cor; |
---|
37 | self_mon.owner = &this; |
---|
38 | self_mon.recursion = 1; |
---|
39 | self_mon_p = &self_mon; |
---|
40 | curr_cluster = &cl; |
---|
41 | next = NULL; |
---|
42 | |
---|
43 | node.next = NULL; |
---|
44 | node.prev = NULL; |
---|
45 | doregister(curr_cluster, this); |
---|
46 | |
---|
47 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
48 | } |
---|
49 | |
---|
50 | void ^?{}(thread_desc& this) with( this ) { |
---|
51 | unregister(curr_cluster, this); |
---|
52 | ^self_cor{}; |
---|
53 | } |
---|
54 | |
---|
55 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
56 | void ?{}( scoped(T)& this ) with( this ) { |
---|
57 | handle{}; |
---|
58 | __thrd_start(handle); |
---|
59 | } |
---|
60 | |
---|
61 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
62 | void ?{}( scoped(T)& this, P params ) with( this ) { |
---|
63 | handle{ params }; |
---|
64 | __thrd_start(handle); |
---|
65 | } |
---|
66 | |
---|
67 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
68 | void ^?{}( scoped(T)& this ) with( this ) { |
---|
69 | ^handle{}; |
---|
70 | } |
---|
71 | |
---|
72 | //----------------------------------------------------------------------------- |
---|
73 | // Starting and stopping threads |
---|
74 | forall( dtype T | is_thread(T) ) |
---|
75 | void __thrd_start( T& this ) { |
---|
76 | thread_desc * this_thrd = get_thread(this); |
---|
77 | thread_desc * curr_thrd = TL_GET( this_thread ); |
---|
78 | |
---|
79 | disable_interrupts(); |
---|
80 | CtxStart(&this, CtxInvokeThread); |
---|
81 | this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP]; |
---|
82 | verify( this_thrd->context.SP ); |
---|
83 | CtxSwitch( &curr_thrd->context, &this_thrd->context ); |
---|
84 | |
---|
85 | ScheduleThread(this_thrd); |
---|
86 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
87 | } |
---|
88 | |
---|
89 | extern "C" { |
---|
90 | // KERNEL ONLY |
---|
91 | void __finish_creation(thread_desc * this) { |
---|
92 | // set new coroutine that the processor is executing |
---|
93 | // and context switch to it |
---|
94 | verify( kernelTLS.this_thread != this ); |
---|
95 | verify( kernelTLS.this_thread->context.SP ); |
---|
96 | CtxSwitch( &this->context, &kernelTLS.this_thread->context ); |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | void yield( void ) { |
---|
101 | // Safety note : This could cause some false positives due to preemption |
---|
102 | verify( TL_GET( preemption_state.enabled ) ); |
---|
103 | BlockInternal( TL_GET( this_thread ) ); |
---|
104 | // Safety note : This could cause some false positives due to preemption |
---|
105 | verify( TL_GET( preemption_state.enabled ) ); |
---|
106 | } |
---|
107 | |
---|
108 | void yield( unsigned times ) { |
---|
109 | for( unsigned i = 0; i < times; i++ ) { |
---|
110 | yield(); |
---|
111 | } |
---|
112 | } |
---|
113 | |
---|
114 | // Local Variables: // |
---|
115 | // mode: c // |
---|
116 | // tab-width: 4 // |
---|
117 | // End: // |
---|