1 | // -*- Mode: CFA -*- |
---|
2 | // |
---|
3 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
4 | // |
---|
5 | // The contents of this file are covered under the licence agreement in the |
---|
6 | // file "LICENCE" distributed with Cforall. |
---|
7 | // |
---|
8 | // thread.c -- |
---|
9 | // |
---|
10 | // Author : Thierry Delisle |
---|
11 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
12 | // Last Modified By : Thierry Delisle |
---|
13 | // Last Modified On : -- |
---|
14 | // Update Count : 0 |
---|
15 | // |
---|
16 | |
---|
17 | #include "thread" |
---|
18 | |
---|
19 | #include "kernel_private.h" |
---|
20 | #include "libhdr.h" |
---|
21 | |
---|
22 | #define __CFA_INVOKE_PRIVATE__ |
---|
23 | #include "invoke.h" |
---|
24 | |
---|
25 | extern "C" { |
---|
26 | #include <fenv.h> |
---|
27 | #include <stddef.h> |
---|
28 | } |
---|
29 | |
---|
30 | extern thread_local processor * this_processor; |
---|
31 | |
---|
32 | //----------------------------------------------------------------------------- |
---|
33 | // Forward declarations |
---|
34 | forall( dtype T | is_thread(T) ) |
---|
35 | void start( T* this ); |
---|
36 | |
---|
37 | forall( dtype T | is_thread(T) ) |
---|
38 | void stop( T* this ); |
---|
39 | |
---|
40 | //----------------------------------------------------------------------------- |
---|
41 | // Thread ctors and dtors |
---|
42 | |
---|
43 | void ?{}(thread_desc* this) { |
---|
44 | (&this->c){}; |
---|
45 | this->c.name = "Anonymous Coroutine"; |
---|
46 | (&this->terminated){}; |
---|
47 | this->next = NULL; |
---|
48 | } |
---|
49 | |
---|
50 | void ^?{}(thread_desc* this) { |
---|
51 | ^(&this->c){}; |
---|
52 | } |
---|
53 | |
---|
54 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } ) |
---|
55 | void ?{}( scoped(T)* this ) { |
---|
56 | (&this->handle){}; |
---|
57 | start(&this->handle); |
---|
58 | } |
---|
59 | |
---|
60 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } ) |
---|
61 | void ?{}( scoped(T)* this, P params ) { |
---|
62 | (&this->handle){ params }; |
---|
63 | start(&this->handle); |
---|
64 | } |
---|
65 | |
---|
66 | forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } ) |
---|
67 | void ^?{}( scoped(T)* this ) { |
---|
68 | stop(&this->handle); |
---|
69 | ^(&this->handle){}; |
---|
70 | } |
---|
71 | |
---|
72 | //----------------------------------------------------------------------------- |
---|
73 | // Starting and stopping threads |
---|
74 | forall( dtype T | is_thread(T) ) |
---|
75 | void start( T* this ) { |
---|
76 | coroutine_desc* thrd_c = get_coroutine(this); |
---|
77 | thread_desc* thrd_h = get_thread (this); |
---|
78 | thrd_c->last = this_coroutine(); |
---|
79 | this_processor->current_coroutine = thrd_c; |
---|
80 | |
---|
81 | LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h); |
---|
82 | |
---|
83 | create_stack(&thrd_c->stack, thrd_c->stack.size); |
---|
84 | CtxStart(this, CtxInvokeThread); |
---|
85 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); |
---|
86 | |
---|
87 | ScheduleThread(thrd_h); |
---|
88 | } |
---|
89 | |
---|
90 | forall( dtype T | is_thread(T) ) |
---|
91 | void stop( T* this ) { |
---|
92 | wait( & get_thread(this)->terminated ); |
---|
93 | } |
---|
94 | |
---|
95 | void yield( void ) { |
---|
96 | ScheduleInternal( this_processor->current_thread ); |
---|
97 | } |
---|
98 | |
---|
99 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { |
---|
100 | // set state of current coroutine to inactive |
---|
101 | src->state = Inactive; |
---|
102 | dst->state = Active; |
---|
103 | |
---|
104 | //update the last resumer |
---|
105 | dst->last = src; |
---|
106 | |
---|
107 | // set new coroutine that the processor is executing |
---|
108 | // and context switch to it |
---|
109 | this_processor->current_coroutine = dst; |
---|
110 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
111 | this_processor->current_coroutine = src; |
---|
112 | |
---|
113 | // set state of new coroutine to active |
---|
114 | dst->state = Inactive; |
---|
115 | src->state = Active; |
---|
116 | } |
---|
117 | |
---|
118 | // C Helper to signal the termination of a thread_desc |
---|
119 | // Used in invoke.c |
---|
120 | extern "C" { |
---|
121 | void __thread_signal_termination( thread_desc * this ) { |
---|
122 | this->c.state = Halted; |
---|
123 | LIB_DEBUG_PRINTF("Thread end : %p\n", this); |
---|
124 | signal( &this->terminated ); |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | // Local Variables: // |
---|
129 | // mode: c // |
---|
130 | // tab-width: 4 // |
---|
131 | // End: // |
---|