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 | // Thread ctors and dtors |
---|
34 | |
---|
35 | void ?{}(thread_desc* this) { |
---|
36 | (&this->cor){}; |
---|
37 | this->cor.name = "Anonymous Coroutine"; |
---|
38 | this->mon.owner = this; |
---|
39 | this->mon.recursion = 1; |
---|
40 | this->next = NULL; |
---|
41 | |
---|
42 | this->current_monitors = &this->mon; |
---|
43 | this->current_monitor_count = 1; |
---|
44 | } |
---|
45 | |
---|
46 | void ^?{}(thread_desc* this) { |
---|
47 | ^(&this->cor){}; |
---|
48 | } |
---|
49 | |
---|
50 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } ) |
---|
51 | void ?{}( scoped(T)* this ) { |
---|
52 | (&this->handle){}; |
---|
53 | __thrd_start(&this->handle); |
---|
54 | } |
---|
55 | |
---|
56 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } ) |
---|
57 | void ?{}( scoped(T)* this, P params ) { |
---|
58 | (&this->handle){ params }; |
---|
59 | __thrd_start(&this->handle); |
---|
60 | } |
---|
61 | |
---|
62 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
63 | void ^?{}( scoped(T)* this ) { |
---|
64 | ^(&this->handle){}; |
---|
65 | } |
---|
66 | |
---|
67 | //----------------------------------------------------------------------------- |
---|
68 | // Starting and stopping threads |
---|
69 | forall( dtype T | is_thread(T) ) |
---|
70 | void __thrd_start( T* this ) { |
---|
71 | coroutine_desc* thrd_c = get_coroutine(this); |
---|
72 | thread_desc* thrd_h = get_thread (this); |
---|
73 | thrd_c->last = this_coroutine(); |
---|
74 | this_processor->current_coroutine = thrd_c; |
---|
75 | |
---|
76 | LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h); |
---|
77 | |
---|
78 | create_stack(&thrd_c->stack, thrd_c->stack.size); |
---|
79 | CtxStart(this, CtxInvokeThread); |
---|
80 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); |
---|
81 | |
---|
82 | ScheduleThread(thrd_h); |
---|
83 | } |
---|
84 | |
---|
85 | void yield( void ) { |
---|
86 | ScheduleInternal( this_processor->current_thread ); |
---|
87 | } |
---|
88 | |
---|
89 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { |
---|
90 | // set state of current coroutine to inactive |
---|
91 | src->state = Inactive; |
---|
92 | dst->state = Active; |
---|
93 | |
---|
94 | //update the last resumer |
---|
95 | dst->last = src; |
---|
96 | |
---|
97 | // set new coroutine that the processor is executing |
---|
98 | // and context switch to it |
---|
99 | this_processor->current_coroutine = dst; |
---|
100 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
101 | this_processor->current_coroutine = src; |
---|
102 | |
---|
103 | // set state of new coroutine to active |
---|
104 | dst->state = Inactive; |
---|
105 | src->state = Active; |
---|
106 | } |
---|
107 | |
---|
108 | // Local Variables: // |
---|
109 | // mode: c // |
---|
110 | // tab-width: 4 // |
---|
111 | // End: // |
---|