[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 ) {
|
---|
[e8e457e] | 33 | context{ NULL, NULL };
|
---|
[de6319f] | 34 | self_cor{ name, storage, storageSize };
|
---|
[e8e457e] | 35 | state = Start;
|
---|
[82c948c] | 36 | curr_cor = &self_cor;
|
---|
[65deb18] | 37 | self_mon.owner = &this;
|
---|
| 38 | self_mon.recursion = 1;
|
---|
| 39 | self_mon_p = &self_mon;
|
---|
[de6319f] | 40 | curr_cluster = &cl;
|
---|
[65deb18] | 41 | next = NULL;
|
---|
[de94a60] | 42 |
|
---|
| 43 | node.next = NULL;
|
---|
| 44 | node.prev = NULL;
|
---|
[a1a17a74] | 45 | doregister(curr_cluster, this);
|
---|
[5ea06d6] | 46 |
|
---|
[65deb18] | 47 | monitors{ &self_mon_p, 1, (fptr_t)0 };
|
---|
[8118303] | 48 | }
|
---|
| 49 |
|
---|
[65deb18] | 50 | void ^?{}(thread_desc& this) with( this ) {
|
---|
[a1a17a74] | 51 | unregister(curr_cluster, this);
|
---|
[65deb18] | 52 | ^self_cor{};
|
---|
[8118303] | 53 | }
|
---|
| 54 |
|
---|
[242a902] | 55 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
|
---|
[65deb18] | 56 | void ?{}( scoped(T)& this ) with( this ) {
|
---|
| 57 | handle{};
|
---|
| 58 | __thrd_start(handle);
|
---|
[8118303] | 59 | }
|
---|
| 60 |
|
---|
[242a902] | 61 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
|
---|
[65deb18] | 62 | void ?{}( scoped(T)& this, P params ) with( this ) {
|
---|
| 63 | handle{ params };
|
---|
| 64 | __thrd_start(handle);
|
---|
[8118303] | 65 | }
|
---|
| 66 |
|
---|
[9f1695b] | 67 | forall( dtype T | sized(T) | is_thread(T) )
|
---|
[65deb18] | 68 | void ^?{}( scoped(T)& this ) with( this ) {
|
---|
| 69 | ^handle{};
|
---|
[8118303] | 70 | }
|
---|
| 71 |
|
---|
| 72 | //-----------------------------------------------------------------------------
|
---|
| 73 | // Starting and stopping threads
|
---|
[0c92c9f] | 74 | forall( dtype T | is_thread(T) )
|
---|
[83a071f9] | 75 | void __thrd_start( T& this ) {
|
---|
[e8e457e] | 76 | thread_desc * this_thrd = get_thread(this);
|
---|
| 77 | thread_desc * curr_thrd = TL_GET( this_thread );
|
---|
[8118303] | 78 |
|
---|
[1c273d0] | 79 | disable_interrupts();
|
---|
[83a071f9] | 80 | CtxStart(&this, CtxInvokeThread);
|
---|
[e8e457e] | 81 | this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
|
---|
| 82 | verify( this_thrd->context.SP );
|
---|
| 83 | CtxSwitch( &curr_thrd->context, &this_thrd->context );
|
---|
[8118303] | 84 |
|
---|
[e8e457e] | 85 | ScheduleThread(this_thrd);
|
---|
[36982fc] | 86 | enable_interrupts( __cfaabi_dbg_ctx );
|
---|
[8118303] | 87 | }
|
---|
| 88 |
|
---|
[b69ea6b] | 89 | extern "C" {
|
---|
[14a61b5] | 90 | // KERNEL ONLY
|
---|
[e8e457e] | 91 | void __finish_creation(thread_desc * this) {
|
---|
| 92 | // set new coroutine that the processor is executing
|
---|
| 93 | // and context switch to it
|
---|
| 94 | verify( kernelTLS.this_thread != this );
|
---|
| 95 | verify( kernelTLS.this_thread->context.SP );
|
---|
| 96 | CtxSwitch( &this->context, &kernelTLS.this_thread->context );
|
---|
[b69ea6b] | 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 |
|
---|
[78b3f52] | 114 | // Local Variables: //
|
---|
| 115 | // mode: c //
|
---|
| 116 | // tab-width: 4 //
|
---|
[6a3d2e7] | 117 | // End: //
|
---|