| [78b3f52] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
| [75a17f1] | 7 | // thread.c -- | 
|---|
| [78b3f52] | 8 | // | 
|---|
|  | 9 | // Author           : Thierry Delisle | 
|---|
| [f07e037] | 10 | // Created On       : Tue Jan 17 12:27:26 2017 | 
|---|
| [6b0b624] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Fri Jul 21 22:34:46 2017 | 
|---|
|  | 13 | // Update Count     : 1 | 
|---|
| [78b3f52] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [75a17f1] | 16 | #include "thread" | 
|---|
| [78b3f52] | 17 |  | 
|---|
| [75f3522] | 18 | #include "kernel_private.h" | 
|---|
| [8118303] | 19 | #include "libhdr.h" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | #define __CFA_INVOKE_PRIVATE__ | 
|---|
|  | 22 | #include "invoke.h" | 
|---|
|  | 23 |  | 
|---|
| [bd98b58] | 24 | extern "C" { | 
|---|
| [8fcbb4c] | 25 | #include <fenv.h> | 
|---|
| [bd98b58] | 26 | #include <stddef.h> | 
|---|
|  | 27 | } | 
|---|
|  | 28 |  | 
|---|
| [4e6fb8e] | 29 | extern volatile thread_local processor * this_processor; | 
|---|
| [8118303] | 30 |  | 
|---|
|  | 31 | //----------------------------------------------------------------------------- | 
|---|
|  | 32 | // Thread ctors and dtors | 
|---|
|  | 33 |  | 
|---|
| [242a902] | 34 | void ?{}(thread_desc& this) { | 
|---|
| [b18830e] | 35 | (this.self_cor){}; | 
|---|
|  | 36 | this.self_cor.name = "Anonymous Coroutine"; | 
|---|
|  | 37 | this.self_mon.owner = &this; | 
|---|
|  | 38 | this.self_mon.recursion = 1; | 
|---|
|  | 39 | this.self_mon_p = &this.self_mon; | 
|---|
| [242a902] | 40 | this.next = NULL; | 
|---|
| [5ea06d6] | 41 |  | 
|---|
| [b18830e] | 42 | (this.monitors){ &this.self_mon_p, 1, (fptr_t)0 }; | 
|---|
| [8118303] | 43 | } | 
|---|
|  | 44 |  | 
|---|
| [242a902] | 45 | void ^?{}(thread_desc& this) { | 
|---|
| [b18830e] | 46 | ^(this.self_cor){}; | 
|---|
| [8118303] | 47 | } | 
|---|
|  | 48 |  | 
|---|
| [242a902] | 49 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) | 
|---|
|  | 50 | void ?{}( scoped(T)& this ) { | 
|---|
|  | 51 | (this.handle){}; | 
|---|
| [83a071f9] | 52 | __thrd_start(this.handle); | 
|---|
| [8118303] | 53 | } | 
|---|
|  | 54 |  | 
|---|
| [242a902] | 55 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) | 
|---|
|  | 56 | void ?{}( scoped(T)& this, P params ) { | 
|---|
|  | 57 | (this.handle){ params }; | 
|---|
| [83a071f9] | 58 | __thrd_start(this.handle); | 
|---|
| [8118303] | 59 | } | 
|---|
|  | 60 |  | 
|---|
| [9f1695b] | 61 | forall( dtype T | sized(T) | is_thread(T) ) | 
|---|
| [242a902] | 62 | void ^?{}( scoped(T)& this ) { | 
|---|
|  | 63 | ^(this.handle){}; | 
|---|
| [8118303] | 64 | } | 
|---|
|  | 65 |  | 
|---|
|  | 66 | //----------------------------------------------------------------------------- | 
|---|
|  | 67 | // Starting and stopping threads | 
|---|
| [0c92c9f] | 68 | forall( dtype T | is_thread(T) ) | 
|---|
| [83a071f9] | 69 | void __thrd_start( T& this ) { | 
|---|
| [c3acb841] | 70 | coroutine_desc* thrd_c = get_coroutine(this); | 
|---|
| [348006f] | 71 | thread_desc*  thrd_h = get_thread   (this); | 
|---|
| [1c273d0] | 72 | thrd_c->last = this_coroutine; | 
|---|
| [8118303] | 73 |  | 
|---|
| [1c273d0] | 74 | // LIB_DEBUG_PRINT_SAFE("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h); | 
|---|
| [8118303] | 75 |  | 
|---|
| [1c273d0] | 76 | disable_interrupts(); | 
|---|
| [8118303] | 77 | create_stack(&thrd_c->stack, thrd_c->stack.size); | 
|---|
| [1c273d0] | 78 | this_coroutine = thrd_c; | 
|---|
| [83a071f9] | 79 | CtxStart(&this, CtxInvokeThread); | 
|---|
| [1c273d0] | 80 | assert( thrd_c->last->stack.context ); | 
|---|
| [8118303] | 81 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); | 
|---|
|  | 82 |  | 
|---|
| [75f3522] | 83 | ScheduleThread(thrd_h); | 
|---|
| [2ac095d] | 84 | enable_interrupts( DEBUG_CTX ); | 
|---|
| [8118303] | 85 | } | 
|---|
|  | 86 |  | 
|---|
| [bd98b58] | 87 | void yield( void ) { | 
|---|
| [3175147] | 88 | BlockInternal( this_thread ); | 
|---|
| [bd98b58] | 89 | } | 
|---|
|  | 90 |  | 
|---|
| [44264c5] | 91 | void yield( unsigned times ) { | 
|---|
|  | 92 | for( unsigned i = 0; i < times; i++ ) { | 
|---|
|  | 93 | yield(); | 
|---|
|  | 94 | } | 
|---|
|  | 95 | } | 
|---|
|  | 96 |  | 
|---|
| [c3acb841] | 97 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { | 
|---|
| [0c92c9f] | 98 | // set state of current coroutine to inactive | 
|---|
| [1c273d0] | 99 | src->state = src->state == Halted ? Halted : Inactive; | 
|---|
| [75f3522] | 100 | dst->state = Active; | 
|---|
| [0c92c9f] | 101 |  | 
|---|
| [75f3522] | 102 | //update the last resumer | 
|---|
|  | 103 | dst->last = src; | 
|---|
| [0c92c9f] | 104 |  | 
|---|
| [75f3522] | 105 | // set new coroutine that the processor is executing | 
|---|
|  | 106 | // and context switch to it | 
|---|
| [1c273d0] | 107 | this_coroutine = dst; | 
|---|
|  | 108 | assert( src->stack.context ); | 
|---|
| [0c92c9f] | 109 | CtxSwitch( src->stack.context, dst->stack.context ); | 
|---|
| [1c273d0] | 110 | this_coroutine = src; | 
|---|
| [0c92c9f] | 111 |  | 
|---|
|  | 112 | // set state of new coroutine to active | 
|---|
| [1c273d0] | 113 | dst->state = dst->state == Halted ? Halted : Inactive; | 
|---|
| [0c92c9f] | 114 | src->state = Active; | 
|---|
|  | 115 | } | 
|---|
|  | 116 |  | 
|---|
| [78b3f52] | 117 | // Local Variables: // | 
|---|
|  | 118 | // mode: c // | 
|---|
|  | 119 | // tab-width: 4 // | 
|---|
| [6a3d2e7] | 120 | // End: // | 
|---|