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