[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
|
---|
[b10affd] | 12 | // Last Modified On : Fri Mar 30 17:19:52 2018
|
---|
| 13 | // Update Count : 8
|
---|
[78b3f52] | 14 | //
|
---|
| 15 |
|
---|
[58b6d1b] | 16 | #include "thread.hfa"
|
---|
[78b3f52] | 17 |
|
---|
[73abe95] | 18 | #include "kernel_private.hfa"
|
---|
[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 |
|
---|
[b10affd] | 28 | //extern volatile thread_local processor * this_processor;
|
---|
[8118303] | 29 |
|
---|
| 30 | //-----------------------------------------------------------------------------
|
---|
| 31 | // Thread ctors and dtors
|
---|
[de6319f] | 32 | void ?{}(thread_desc & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
|
---|
| 33 | self_cor{ name, storage, storageSize };
|
---|
| 34 | verify(&self_cor);
|
---|
[82c948c] | 35 | curr_cor = &self_cor;
|
---|
[65deb18] | 36 | self_mon.owner = &this;
|
---|
| 37 | self_mon.recursion = 1;
|
---|
| 38 | self_mon_p = &self_mon;
|
---|
[de6319f] | 39 | curr_cluster = &cl;
|
---|
[65deb18] | 40 | next = NULL;
|
---|
[de94a60] | 41 |
|
---|
| 42 | node.next = NULL;
|
---|
| 43 | node.prev = NULL;
|
---|
[a1a17a74] | 44 | doregister(curr_cluster, this);
|
---|
[5ea06d6] | 45 |
|
---|
[65deb18] | 46 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
[8118303] | 47 | }
|
---|
| 48 |
|
---|
[65deb18] | 49 | void ^?{}(thread_desc& this) with( this ) {
|
---|
[a1a17a74] | 50 | unregister(curr_cluster, this);
|
---|
[65deb18] | 51 | ^self_cor{};
|
---|
[8118303] | 52 | }
|
---|
| 53 |
|
---|
[242a902] | 54 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
|
---|
[65deb18] | 55 | void ?{}( scoped(T)& this ) with( this ) {
|
---|
| 56 | handle{};
|
---|
| 57 | __thrd_start(handle);
|
---|
[8118303] | 58 | }
|
---|
| 59 |
|
---|
[242a902] | 60 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
|
---|
[65deb18] | 61 | void ?{}( scoped(T)& this, P params ) with( this ) {
|
---|
| 62 | handle{ params };
|
---|
| 63 | __thrd_start(handle);
|
---|
[8118303] | 64 | }
|
---|
| 65 |
|
---|
[9f1695b] | 66 | forall( dtype T | sized(T) | is_thread(T) )
|
---|
[65deb18] | 67 | void ^?{}( scoped(T)& this ) with( this ) {
|
---|
| 68 | ^handle{};
|
---|
[8118303] | 69 | }
|
---|
| 70 |
|
---|
| 71 | //-----------------------------------------------------------------------------
|
---|
| 72 | // Starting and stopping threads
|
---|
[0c92c9f] | 73 | forall( dtype T | is_thread(T) )
|
---|
[83a071f9] | 74 | void __thrd_start( T& this ) {
|
---|
[c3acb841] | 75 | coroutine_desc* thrd_c = get_coroutine(this);
|
---|
[65deb18] | 76 | thread_desc * thrd_h = get_thread (this);
|
---|
[b10affd] | 77 | thrd_c->last = TL_GET( this_coroutine );
|
---|
[8118303] | 78 |
|
---|
[36982fc] | 79 | // __cfaabi_dbg_print_safe("Thread start : %p (t %p, c %p)\n", this, thrd_c, thrd_h);
|
---|
[8118303] | 80 |
|
---|
[1c273d0] | 81 | disable_interrupts();
|
---|
[8118303] | 82 | create_stack(&thrd_c->stack, thrd_c->stack.size);
|
---|
[14a61b5] | 83 | kernelTLS.this_coroutine = thrd_c;
|
---|
[83a071f9] | 84 | CtxStart(&this, CtxInvokeThread);
|
---|
[1c273d0] | 85 | assert( thrd_c->last->stack.context );
|
---|
[8118303] | 86 | CtxSwitch( thrd_c->last->stack.context, thrd_c->stack.context );
|
---|
| 87 |
|
---|
[75f3522] | 88 | ScheduleThread(thrd_h);
|
---|
[36982fc] | 89 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[8118303] | 90 | }
|
---|
| 91 |
|
---|
[b69ea6b] | 92 | extern "C" {
|
---|
[14a61b5] | 93 | // KERNEL ONLY
|
---|
[b69ea6b] | 94 | void __finish_creation(void) {
|
---|
[14a61b5] | 95 | coroutine_desc* thrd_c = kernelTLS.this_coroutine;
|
---|
[b69ea6b] | 96 | ThreadCtxSwitch( thrd_c, thrd_c->last );
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
[bd98b58] | 100 | void yield( void ) {
|
---|
[14a61b5] | 101 | // Safety note : This could cause some false positives due to preemption
|
---|
[afd550c] | 102 | verify( TL_GET( preemption_state.enabled ) );
|
---|
[b10affd] | 103 | BlockInternal( TL_GET( this_thread ) );
|
---|
[14a61b5] | 104 | // Safety note : This could cause some false positives due to preemption
|
---|
[afd550c] | 105 | verify( TL_GET( preemption_state.enabled ) );
|
---|
[bd98b58] | 106 | }
|
---|
| 107 |
|
---|
[44264c5] | 108 | void yield( unsigned times ) {
|
---|
| 109 | for( unsigned i = 0; i < times; i++ ) {
|
---|
| 110 | yield();
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[14a61b5] | 114 | // KERNEL ONLY
|
---|
[c3acb841] | 115 | void ThreadCtxSwitch(coroutine_desc* src, coroutine_desc* dst) {
|
---|
[0c92c9f] | 116 | // set state of current coroutine to inactive
|
---|
[1c273d0] | 117 | src->state = src->state == Halted ? Halted : Inactive;
|
---|
[75f3522] | 118 | dst->state = Active;
|
---|
[0c92c9f] | 119 |
|
---|
[75f3522] | 120 | // set new coroutine that the processor is executing
|
---|
| 121 | // and context switch to it
|
---|
[14a61b5] | 122 | kernelTLS.this_coroutine = dst;
|
---|
[1c273d0] | 123 | assert( src->stack.context );
|
---|
[0c92c9f] | 124 | CtxSwitch( src->stack.context, dst->stack.context );
|
---|
[14a61b5] | 125 | kernelTLS.this_coroutine = src;
|
---|
[0c92c9f] | 126 |
|
---|
| 127 | // set state of new coroutine to active
|
---|
[1c273d0] | 128 | dst->state = dst->state == Halted ? Halted : Inactive;
|
---|
[0c92c9f] | 129 | src->state = Active;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
[78b3f52] | 132 | // Local Variables: //
|
---|
| 133 | // mode: c //
|
---|
| 134 | // tab-width: 4 //
|
---|
[6a3d2e7] | 135 | // End: //
|
---|