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 <fenv.h>
|
---|
27 | #include <stddef.h>
|
---|
28 | }
|
---|
29 |
|
---|
30 | extern processor * get_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* this) {
|
---|
44 | (&this->c){};
|
---|
45 | this->c.name = "Anonymous Coroutine";
|
---|
46 | (&this->lock){};
|
---|
47 | this->next = NULL;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void ^?{}(thread* 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 | extern "C" {
|
---|
75 | forall(dtype T | is_thread(T))
|
---|
76 | void CtxInvokeThread(T * this);
|
---|
77 | }
|
---|
78 |
|
---|
79 | extern void thread_schedule( thread * );
|
---|
80 |
|
---|
81 | forall( dtype T | is_thread(T) )
|
---|
82 | void start( T* this ) {
|
---|
83 | coroutine* thrd_c = get_coroutine(this);
|
---|
84 | thread* thrd_h = get_thread (this);
|
---|
85 | thrd_c->last = this_coroutine();
|
---|
86 | get_this_processor()->current_coroutine = thrd_c;
|
---|
87 |
|
---|
88 | LIB_DEBUG_PRINTF("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
---|
89 |
|
---|
90 | create_stack(&thrd_c->stack, thrd_c->stack.size);
|
---|
91 | CtxStart(this, CtxInvokeThread);
|
---|
92 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
|
---|
93 |
|
---|
94 | fenv_t envp;
|
---|
95 | fegetenv( &envp );
|
---|
96 | LIB_DEBUG_PRINTF("Thread : mxcsr %x\n", envp.__mxcsr);
|
---|
97 | LIB_DEBUG_PRINTF("Thread started : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
---|
98 |
|
---|
99 | thread_schedule(thrd_h);
|
---|
100 | }
|
---|
101 |
|
---|
102 | forall( dtype T | is_thread(T) )
|
---|
103 | void stop( T* this ) {
|
---|
104 | thread* thrd = get_thread(this);
|
---|
105 | if( thrd->c.notHalted ) {
|
---|
106 | lock( &thrd->lock );
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | void yield( void ) {
|
---|
111 | get_this_processor()->thread_action = Reschedule;
|
---|
112 | suspend();
|
---|
113 | }
|
---|
114 |
|
---|
115 | void ThreadCtxSwitch(coroutine* src, coroutine* dst) {
|
---|
116 | dst->last = src;
|
---|
117 |
|
---|
118 | // set state of current coroutine to inactive
|
---|
119 | src->state = Inactive;
|
---|
120 |
|
---|
121 | // set new coroutine that task is executing
|
---|
122 | get_this_processor()->current_coroutine = dst;
|
---|
123 |
|
---|
124 | // context switch to specified coroutine
|
---|
125 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
126 | // when CtxSwitch returns we are back in the src coroutine
|
---|
127 |
|
---|
128 | // set state of new coroutine to active
|
---|
129 | src->state = Active;
|
---|
130 | }
|
---|
131 |
|
---|
132 | // C Helper to signal the termination of a thread
|
---|
133 | // Used in invoke.c
|
---|
134 | extern "C" {
|
---|
135 | void __thread_signal_termination( thread * this ) {
|
---|
136 | this->c.state = Halt;
|
---|
137 | this->c.notHalted = false;
|
---|
138 | unlock( &this->lock );
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | // Local Variables: //
|
---|
143 | // mode: c //
|
---|
144 | // tab-width: 4 //
|
---|
145 | // End: //
|
---|