| [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 | 
|---|
| [121be3e] | 12 | // Last Modified On : Wed Dec  4 09:17:49 2019 | 
|---|
|  | 13 | // Update Count     : 9 | 
|---|
| [78b3f52] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [2026bb6] | 16 | #define __cforall_thread__ | 
|---|
|  | 17 |  | 
|---|
| [58b6d1b] | 18 | #include "thread.hfa" | 
|---|
| [78b3f52] | 19 |  | 
|---|
| [73abe95] | 20 | #include "kernel_private.hfa" | 
|---|
| [8118303] | 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 |  | 
|---|
| [b10affd] | 30 | //extern volatile thread_local processor * this_processor; | 
|---|
| [8118303] | 31 |  | 
|---|
|  | 32 | //----------------------------------------------------------------------------- | 
|---|
|  | 33 | // Thread ctors and dtors | 
|---|
| [de6319f] | 34 | void ?{}(thread_desc & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) { | 
|---|
| [121be3e] | 35 | context{ 0p, 0p }; | 
|---|
| [de6319f] | 36 | self_cor{ name, storage, storageSize }; | 
|---|
| [e8e457e] | 37 | state = Start; | 
|---|
| [82c948c] | 38 | curr_cor = &self_cor; | 
|---|
| [65deb18] | 39 | self_mon.owner = &this; | 
|---|
|  | 40 | self_mon.recursion = 1; | 
|---|
|  | 41 | self_mon_p = &self_mon; | 
|---|
| [de6319f] | 42 | curr_cluster = &cl; | 
|---|
| [121be3e] | 43 | next = 0p; | 
|---|
| [de94a60] | 44 |  | 
|---|
| [121be3e] | 45 | node.next = 0p; | 
|---|
|  | 46 | node.prev = 0p; | 
|---|
| [a1a17a74] | 47 | doregister(curr_cluster, this); | 
|---|
| [5ea06d6] | 48 |  | 
|---|
| [65deb18] | 49 | monitors{ &self_mon_p, 1, (fptr_t)0 }; | 
|---|
| [8118303] | 50 | } | 
|---|
|  | 51 |  | 
|---|
| [65deb18] | 52 | void ^?{}(thread_desc& this) with( this ) { | 
|---|
| [a1a17a74] | 53 | unregister(curr_cluster, this); | 
|---|
| [65deb18] | 54 | ^self_cor{}; | 
|---|
| [8118303] | 55 | } | 
|---|
|  | 56 |  | 
|---|
| [242a902] | 57 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) | 
|---|
| [65deb18] | 58 | void ?{}( scoped(T)& this ) with( this ) { | 
|---|
|  | 59 | handle{}; | 
|---|
| [09f357ec] | 60 | __thrd_start(handle, main); | 
|---|
| [8118303] | 61 | } | 
|---|
|  | 62 |  | 
|---|
| [242a902] | 63 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) | 
|---|
| [65deb18] | 64 | void ?{}( scoped(T)& this, P params ) with( this ) { | 
|---|
|  | 65 | handle{ params }; | 
|---|
| [09f357ec] | 66 | __thrd_start(handle, main); | 
|---|
| [8118303] | 67 | } | 
|---|
|  | 68 |  | 
|---|
| [9f1695b] | 69 | forall( dtype T | sized(T) | is_thread(T) ) | 
|---|
| [65deb18] | 70 | void ^?{}( scoped(T)& this ) with( this ) { | 
|---|
|  | 71 | ^handle{}; | 
|---|
| [8118303] | 72 | } | 
|---|
|  | 73 |  | 
|---|
|  | 74 | //----------------------------------------------------------------------------- | 
|---|
|  | 75 | // Starting and stopping threads | 
|---|
| [0c92c9f] | 76 | forall( dtype T | is_thread(T) ) | 
|---|
| [09f357ec] | 77 | void __thrd_start( T & this, void (*main_p)(T &) ) { | 
|---|
| [e8e457e] | 78 | thread_desc * this_thrd = get_thread(this); | 
|---|
|  | 79 | thread_desc * curr_thrd = TL_GET( this_thread ); | 
|---|
| [8118303] | 80 |  | 
|---|
| [1c273d0] | 81 | disable_interrupts(); | 
|---|
| [09f357ec] | 82 | CtxStart(main_p, get_coroutine(this), this, CtxInvokeThread); | 
|---|
|  | 83 |  | 
|---|
| [e8e457e] | 84 | this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP]; | 
|---|
|  | 85 | verify( this_thrd->context.SP ); | 
|---|
| [09f357ec] | 86 | // CtxSwitch( &curr_thrd->context, &this_thrd->context ); | 
|---|
| [8118303] | 87 |  | 
|---|
| [e8e457e] | 88 | ScheduleThread(this_thrd); | 
|---|
| [36982fc] | 89 | enable_interrupts( __cfaabi_dbg_ctx ); | 
|---|
| [8118303] | 90 | } | 
|---|
|  | 91 |  | 
|---|
| [bd98b58] | 92 | void yield( void ) { | 
|---|
| [14a61b5] | 93 | // Safety note : This could cause some false positives due to preemption | 
|---|
| [afd550c] | 94 | verify( TL_GET( preemption_state.enabled ) ); | 
|---|
| [b10affd] | 95 | BlockInternal( TL_GET( this_thread ) ); | 
|---|
| [14a61b5] | 96 | // Safety note : This could cause some false positives due to preemption | 
|---|
| [afd550c] | 97 | verify( TL_GET( preemption_state.enabled ) ); | 
|---|
| [bd98b58] | 98 | } | 
|---|
|  | 99 |  | 
|---|
| [44264c5] | 100 | void yield( unsigned times ) { | 
|---|
|  | 101 | for( unsigned i = 0; i < times; i++ ) { | 
|---|
|  | 102 | yield(); | 
|---|
|  | 103 | } | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
| [78b3f52] | 106 | // Local Variables: // | 
|---|
|  | 107 | // mode: c // | 
|---|
|  | 108 | // tab-width: 4 // | 
|---|
| [6a3d2e7] | 109 | // End: // | 
|---|