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 Jul 21 22:34:46 2017 |
---|
13 | // Update Count : 1 |
---|
14 | // |
---|
15 | |
---|
16 | #include "thread" |
---|
17 | |
---|
18 | #include "kernel_private.h" |
---|
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 | |
---|
33 | void ?{}(thread_desc& this) with( this ) { |
---|
34 | self_cor{}; |
---|
35 | self_cor.name = "Anonymous Coroutine"; |
---|
36 | self_mon.owner = &this; |
---|
37 | self_mon.recursion = 1; |
---|
38 | self_mon_p = &self_mon; |
---|
39 | next = NULL; |
---|
40 | __cfaabi_dbg_debug_do( |
---|
41 | dbg_next = NULL; |
---|
42 | dbg_prev = NULL; |
---|
43 | __cfaabi_dbg_thread_register(&this); |
---|
44 | ) |
---|
45 | |
---|
46 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
47 | } |
---|
48 | |
---|
49 | void ^?{}(thread_desc& this) with( this ) { |
---|
50 | ^self_cor{}; |
---|
51 | } |
---|
52 | |
---|
53 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
54 | void ?{}( scoped(T)& this ) with( this ) { |
---|
55 | handle{}; |
---|
56 | __thrd_start(handle); |
---|
57 | } |
---|
58 | |
---|
59 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
60 | void ?{}( scoped(T)& this, P params ) with( this ) { |
---|
61 | handle{ params }; |
---|
62 | __thrd_start(handle); |
---|
63 | } |
---|
64 | |
---|
65 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
66 | void ^?{}( scoped(T)& this ) with( this ) { |
---|
67 | ^handle{}; |
---|
68 | } |
---|
69 | |
---|
70 | //----------------------------------------------------------------------------- |
---|
71 | // Starting and stopping threads |
---|
72 | forall( dtype T | is_thread(T) ) |
---|
73 | void __thrd_start( T& this ) { |
---|
74 | coroutine_desc* thrd_c = get_coroutine(this); |
---|
75 | thread_desc * thrd_h = get_thread (this); |
---|
76 | thrd_c->last = this_coroutine; |
---|
77 | |
---|
78 | // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h); |
---|
79 | |
---|
80 | disable_interrupts(); |
---|
81 | create_stack(&thrd_c->stack, thrd_c->stack.size); |
---|
82 | this_coroutine = thrd_c; |
---|
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 | void yield( void ) { |
---|
92 | BlockInternal( this_thread ); |
---|
93 | } |
---|
94 | |
---|
95 | void yield( unsigned times ) { |
---|
96 | for( unsigned i = 0; i < times; i++ ) { |
---|
97 | yield(); |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { |
---|
102 | // set state of current coroutine to inactive |
---|
103 | src->state = src->state == Halted ? Halted : Inactive; |
---|
104 | dst->state = Active; |
---|
105 | |
---|
106 | //update the last resumer |
---|
107 | dst->last = src; |
---|
108 | |
---|
109 | // set new coroutine that the processor is executing |
---|
110 | // and context switch to it |
---|
111 | this_coroutine = dst; |
---|
112 | assert( src->stack.context ); |
---|
113 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
114 | this_coroutine = src; |
---|
115 | |
---|
116 | // set state of new coroutine to active |
---|
117 | dst->state = dst->state == Halted ? Halted : Inactive; |
---|
118 | src->state = Active; |
---|
119 | } |
---|
120 | |
---|
121 | // Local Variables: // |
---|
122 | // mode: c // |
---|
123 | // tab-width: 4 // |
---|
124 | // End: // |
---|