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