[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 | |
---|
| 20 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 21 | #include "invoke.h" |
---|
| 22 | |
---|
[bd98b58] | 23 | extern "C" { |
---|
[8fcbb4c] | 24 | #include <fenv.h> |
---|
[bd98b58] | 25 | #include <stddef.h> |
---|
| 26 | } |
---|
| 27 | |
---|
[4e6fb8e] | 28 | extern volatile thread_local processor * this_processor; |
---|
[8118303] | 29 | |
---|
| 30 | //----------------------------------------------------------------------------- |
---|
| 31 | // Thread ctors and dtors |
---|
| 32 | |
---|
[242a902] | 33 | void ?{}(thread_desc& this) { |
---|
[b18830e] | 34 | (this.self_cor){}; |
---|
[f7d6bb0] | 35 | this.self_cor.name = "Anonymous Thread"; |
---|
[b18830e] | 36 | this.self_mon.owner = &this; |
---|
| 37 | this.self_mon.recursion = 1; |
---|
| 38 | this.self_mon_p = &this.self_mon; |
---|
[242a902] | 39 | this.next = NULL; |
---|
[f7d6bb0] | 40 | __cfaabi_dbg_debug_do( |
---|
| 41 | this.dbg_next = NULL; |
---|
| 42 | this.dbg_prev = NULL; |
---|
| 43 | __cfaabi_dbg_thread_register(&this); |
---|
| 44 | ) |
---|
[5ea06d6] | 45 | |
---|
[b18830e] | 46 | (this.monitors){ &this.self_mon_p, 1, (fptr_t)0 }; |
---|
[8118303] | 47 | } |
---|
| 48 | |
---|
[242a902] | 49 | void ^?{}(thread_desc& this) { |
---|
[f7d6bb0] | 50 | __cfaabi_dbg_debug_do( |
---|
| 51 | __cfaabi_dbg_thread_unregister(&this); |
---|
| 52 | ) |
---|
[b18830e] | 53 | ^(this.self_cor){}; |
---|
[8118303] | 54 | } |
---|
| 55 | |
---|
[242a902] | 56 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) |
---|
| 57 | void ?{}( scoped(T)& this ) { |
---|
| 58 | (this.handle){}; |
---|
[83a071f9] | 59 | __thrd_start(this.handle); |
---|
[8118303] | 60 | } |
---|
| 61 | |
---|
[242a902] | 62 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) |
---|
| 63 | void ?{}( scoped(T)& this, P params ) { |
---|
| 64 | (this.handle){ params }; |
---|
[83a071f9] | 65 | __thrd_start(this.handle); |
---|
[8118303] | 66 | } |
---|
| 67 | |
---|
[9f1695b] | 68 | forall( dtype T | sized(T) | is_thread(T) ) |
---|
[242a902] | 69 | void ^?{}( scoped(T)& this ) { |
---|
| 70 | ^(this.handle){}; |
---|
[8118303] | 71 | } |
---|
| 72 | |
---|
| 73 | //----------------------------------------------------------------------------- |
---|
| 74 | // Starting and stopping threads |
---|
[0c92c9f] | 75 | forall( dtype T | is_thread(T) ) |
---|
[83a071f9] | 76 | void __thrd_start( T& this ) { |
---|
[c3acb841] | 77 | coroutine_desc* thrd_c = get_coroutine(this); |
---|
[348006f] | 78 | thread_desc* thrd_h = get_thread (this); |
---|
[1c273d0] | 79 | thrd_c->last = this_coroutine; |
---|
[8118303] | 80 | |
---|
[36982fc] | 81 | // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h); |
---|
[8118303] | 82 | |
---|
[1c273d0] | 83 | disable_interrupts(); |
---|
[8118303] | 84 | create_stack(&thrd_c->stack, thrd_c->stack.size); |
---|
[1c273d0] | 85 | this_coroutine = thrd_c; |
---|
[83a071f9] | 86 | CtxStart(&this, CtxInvokeThread); |
---|
[1c273d0] | 87 | assert( thrd_c->last->stack.context ); |
---|
[8118303] | 88 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context ); |
---|
| 89 | |
---|
[75f3522] | 90 | ScheduleThread(thrd_h); |
---|
[36982fc] | 91 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[8118303] | 92 | } |
---|
| 93 | |
---|
[bd98b58] | 94 | void yield( void ) { |
---|
[3175147] | 95 | BlockInternal( this_thread ); |
---|
[bd98b58] | 96 | } |
---|
| 97 | |
---|
[44264c5] | 98 | void yield( unsigned times ) { |
---|
| 99 | for( unsigned i = 0; i < times; i++ ) { |
---|
| 100 | yield(); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
[c3acb841] | 104 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) { |
---|
[0c92c9f] | 105 | // set state of current coroutine to inactive |
---|
[1c273d0] | 106 | src->state = src->state == Halted ? Halted : Inactive; |
---|
[75f3522] | 107 | dst->state = Active; |
---|
[0c92c9f] | 108 | |
---|
[75f3522] | 109 | //update the last resumer |
---|
| 110 | dst->last = src; |
---|
[0c92c9f] | 111 | |
---|
[75f3522] | 112 | // set new coroutine that the processor is executing |
---|
| 113 | // and context switch to it |
---|
[1c273d0] | 114 | this_coroutine = dst; |
---|
| 115 | assert( src->stack.context ); |
---|
[0c92c9f] | 116 | CtxSwitch( src->stack.context, dst->stack.context ); |
---|
[1c273d0] | 117 | this_coroutine = src; |
---|
[0c92c9f] | 118 | |
---|
| 119 | // set state of new coroutine to active |
---|
[1c273d0] | 120 | dst->state = dst->state == Halted ? Halted : Inactive; |
---|
[0c92c9f] | 121 | src->state = Active; |
---|
| 122 | } |
---|
| 123 | |
---|
[78b3f52] | 124 | // Local Variables: // |
---|
| 125 | // mode: c // |
---|
| 126 | // tab-width: 4 // |
---|
[6a3d2e7] | 127 | // End: // |
---|