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 | // threads.c --
|
---|
9 | //
|
---|
10 | // Author : Thierry Delisle
|
---|
11 | // Created On : Tue Jan 17 12:27:26 2016
|
---|
12 | // Last Modified By : Thierry Delisle
|
---|
13 | // Last Modified On : --
|
---|
14 | // Update Count : 0
|
---|
15 | //
|
---|
16 |
|
---|
17 | #include "threads"
|
---|
18 |
|
---|
19 | #include "kernel"
|
---|
20 | #include "libhdr.h"
|
---|
21 |
|
---|
22 | #define __CFA_INVOKE_PRIVATE__
|
---|
23 | #include "invoke.h"
|
---|
24 |
|
---|
25 | extern "C" {
|
---|
26 | #include <stddef.h>
|
---|
27 | }
|
---|
28 |
|
---|
29 | extern processor * get_this_processor();
|
---|
30 |
|
---|
31 | //-----------------------------------------------------------------------------
|
---|
32 | // Forward declarations
|
---|
33 | forall( dtype T | is_thread(T) )
|
---|
34 | void start( T* this );
|
---|
35 |
|
---|
36 | forall( dtype T | is_thread(T) )
|
---|
37 | void stop( T* this );
|
---|
38 |
|
---|
39 | //-----------------------------------------------------------------------------
|
---|
40 | // Thread ctors and dtors
|
---|
41 |
|
---|
42 | void ?{}(thread* this) {
|
---|
43 | (&this->c){};
|
---|
44 | this->c.name = "Anonymous Coroutine";
|
---|
45 | (&this->lock){};
|
---|
46 | this->next = NULL;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void ^?{}(thread* this) {
|
---|
50 | ^(&this->c){};
|
---|
51 | }
|
---|
52 |
|
---|
53 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T*); } )
|
---|
54 | void ?{}( scoped(T)* this ) {
|
---|
55 | (&this->handle){};
|
---|
56 | start(&this->handle);
|
---|
57 | }
|
---|
58 |
|
---|
59 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T*, P); } )
|
---|
60 | void ?{}( scoped(T)* this, P params ) {
|
---|
61 | (&this->handle){ params };
|
---|
62 | start(&this->handle);
|
---|
63 | }
|
---|
64 |
|
---|
65 | forall( dtype T | sized(T) | is_thread(T) | { void ^?{}(T*); } )
|
---|
66 | void ^?{}( scoped(T)* this ) {
|
---|
67 | stop(&this->handle);
|
---|
68 | ^(&this->handle){};
|
---|
69 | }
|
---|
70 |
|
---|
71 | //-----------------------------------------------------------------------------
|
---|
72 | // Starting and stopping threads
|
---|
73 | extern "C" {
|
---|
74 | forall(dtype T | is_thread(T))
|
---|
75 | void CtxInvokeThread(T * this);
|
---|
76 | }
|
---|
77 |
|
---|
78 | extern void thread_schedule( thread * );
|
---|
79 |
|
---|
80 | forall( dtype T | is_thread(T) )
|
---|
81 | void start( T* this ) {
|
---|
82 | coroutine* thrd_c = get_coroutine(this);
|
---|
83 | thread* thrd_h = get_thread (this);
|
---|
84 | thrd_c->last = this_coroutine();
|
---|
85 | get_this_processor()->current_coroutine = thrd_c;
|
---|
86 |
|
---|
87 | LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
---|
88 |
|
---|
89 | create_stack(&thrd_c->stack, thrd_c->stack.size);
|
---|
90 | CtxStart(this, CtxInvokeThread);
|
---|
91 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
|
---|
92 |
|
---|
93 | LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
---|
94 |
|
---|
95 | thread_schedule(thrd_h);
|
---|
96 | }
|
---|
97 |
|
---|
98 | forall( dtype T | is_thread(T) )
|
---|
99 | void stop( T* this ) {
|
---|
100 | thread* thrd = get_thread(this);
|
---|
101 | if( thrd->c.notHalted ) {
|
---|
102 | lock( &thrd->lock );
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | void yield( void ) {
|
---|
107 | thread_schedule( this_thread() );
|
---|
108 | suspend();
|
---|
109 | }
|
---|
110 |
|
---|
111 | void ThreadCtxSwitch(coroutine* src, coroutine* dst) {
|
---|
112 | dst->last = src;
|
---|
113 |
|
---|
114 | // set state of current coroutine to inactive
|
---|
115 | src->state = Inactive;
|
---|
116 |
|
---|
117 | // set new coroutine that task is executing
|
---|
118 | get_this_processor()->current_coroutine = dst;
|
---|
119 |
|
---|
120 | // context switch to specified coroutine
|
---|
121 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
122 | // when CtxSwitch returns we are back in the src coroutine
|
---|
123 |
|
---|
124 | // set state of new coroutine to active
|
---|
125 | src->state = Active;
|
---|
126 | }
|
---|
127 |
|
---|
128 | // C Helper to signal the termination of a thread
|
---|
129 | // Used in invoke.c
|
---|
130 | extern "C" {
|
---|
131 | void __thread_signal_termination( thread * this ) {
|
---|
132 | this->c.state = Halt;
|
---|
133 | this->c.notHalted = false;
|
---|
134 | unlock( &this->lock );
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 | // Local Variables: //
|
---|
139 | // mode: c //
|
---|
140 | // tab-width: 4 //
|
---|
141 | // End: //
|
---|