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 | self_cor{ name, storage, storageSize }; |
---|
34 | verify(&self_cor); |
---|
35 | curr_cor = &self_cor; |
---|
36 | self_mon.owner = &this; |
---|
37 | self_mon.recursion = 1; |
---|
38 | self_mon_p = &self_mon; |
---|
39 | curr_cluster = &cl; |
---|
40 | next = NULL; |
---|
41 | |
---|
42 | node.next = NULL; |
---|
43 | node.prev = NULL; |
---|
44 | doregister(curr_cluster, this); |
---|
45 | |
---|
46 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
47 | } |
---|
48 | |
---|
49 | void ^?{}(thread_desc& this) with( this ) { |
---|
50 | unregister(curr_cluster, this); |
---|
51 | ^self_cor{}; |
---|
52 | } |
---|
53 | |
---|
54 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
55 | void ?{}( scoped(T)& this ) with( this ) { |
---|
56 | handle{}; |
---|
57 | __thrd_start(handle); |
---|
58 | } |
---|
59 | |
---|
60 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
61 | void ?{}( scoped(T)& this, P params ) with( this ) { |
---|
62 | handle{ params }; |
---|
63 | __thrd_start(handle); |
---|
64 | } |
---|
65 | |
---|
66 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
67 | void ^?{}( scoped(T)& this ) with( this ) { |
---|
68 | ^handle{}; |
---|
69 | } |
---|
70 | |
---|
71 | //----------------------------------------------------------------------------- |
---|
72 | // Starting and stopping threads |
---|
73 | forall( dtype T | is_thread(T) ) |
---|
74 | void __thrd_start( T& this ) { |
---|
75 | coroutine_desc* thrd_c = get_coroutine(this); |
---|
76 | thread_desc * thrd_h = get_thread (this); |
---|
77 | thrd_c->last = TL_GET( this_thread )->curr_cor; |
---|
78 | |
---|
79 | // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h); |
---|
80 | |
---|
81 | disable_interrupts(); |
---|
82 | create_stack(&thrd_c->stack, thrd_c->stack.size); |
---|
83 | CtxStart(&this, CtxInvokeThread); |
---|
84 | assert( thrd_c->last->stack.context ); |
---|
85 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); |
---|
86 | |
---|
87 | ScheduleThread(thrd_h); |
---|
88 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
89 | } |
---|
90 | |
---|
91 | extern "C" { |
---|
92 | // KERNEL ONLY |
---|
93 | void __finish_creation(coroutine_desc * thrd_c) { |
---|
94 | ThreadCtxSwitch( thrd_c, thrd_c->last ); |
---|
95 | } |
---|
96 | } |
---|
97 | |
---|
98 | void yield( void ) { |
---|
99 | // Safety note : This could cause some false positives due to preemption |
---|
100 | verify( TL_GET( preemption_state.enabled ) ); |
---|
101 | BlockInternal( TL_GET( this_thread ) ); |
---|
102 | // Safety note : This could cause some false positives due to preemption |
---|
103 | verify( TL_GET( preemption_state.enabled ) ); |
---|
104 | } |
---|
105 | |
---|
106 | void yield( unsigned times ) { |
---|
107 | for( unsigned i = 0; i < times; i++ ) { |
---|
108 | yield(); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | // KERNEL ONLY |
---|
113 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { |
---|
114 | // set state of current coroutine to inactive |
---|
115 | src->state = src->state == Halted ? Halted : Inactive; |
---|
116 | dst->state = Active; |
---|
117 | |
---|
118 | // set new coroutine that the processor is executing |
---|
119 | // and context switch to it |
---|
120 | assert( src->stack.context ); |
---|
121 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
122 | |
---|
123 | // set state of new coroutine to active |
---|
124 | dst->state = dst->state == Halted ? Halted : Inactive; |
---|
125 | src->state = Active; |
---|
126 | } |
---|
127 | |
---|
128 | // Local Variables: // |
---|
129 | // mode: c // |
---|
130 | // tab-width: 4 // |
---|
131 | // End: // |
---|