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