| [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 |  | 
|---|
|  | 25 | //----------------------------------------------------------------------------- | 
|---|
|  | 26 | // Thread ctors and dtors | 
|---|
| [ac2b598] | 27 | void ?{}($thread & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) { | 
|---|
| [121be3e] | 28 | context{ 0p, 0p }; | 
|---|
| [de6319f] | 29 | self_cor{ name, storage, storageSize }; | 
|---|
| [ff79d5e] | 30 | ticket = 1; | 
|---|
| [e8e457e] | 31 | state = Start; | 
|---|
| [3381ed7] | 32 | preempted = __NO_PREEMPTION; | 
|---|
| [82c948c] | 33 | curr_cor = &self_cor; | 
|---|
| [65deb18] | 34 | self_mon.owner = &this; | 
|---|
|  | 35 | self_mon.recursion = 1; | 
|---|
|  | 36 | self_mon_p = &self_mon; | 
|---|
| [de6319f] | 37 | curr_cluster = &cl; | 
|---|
| [b798713] | 38 | link.next = 0p; | 
|---|
|  | 39 | link.prev = 0p; | 
|---|
| [d72c074] | 40 | link.preferred = -1; | 
|---|
| [de94a60] | 41 |  | 
|---|
| [121be3e] | 42 | node.next = 0p; | 
|---|
|  | 43 | node.prev = 0p; | 
|---|
| [a1a17a74] | 44 | doregister(curr_cluster, this); | 
|---|
| [5ea06d6] | 45 |  | 
|---|
| [65deb18] | 46 | monitors{ &self_mon_p, 1, (fptr_t)0 }; | 
|---|
| [8118303] | 47 | } | 
|---|
|  | 48 |  | 
|---|
| [ac2b598] | 49 | void ^?{}($thread& this) with( this ) { | 
|---|
| [a1a17a74] | 50 | unregister(curr_cluster, this); | 
|---|
| [65deb18] | 51 | ^self_cor{}; | 
|---|
| [8118303] | 52 | } | 
|---|
|  | 53 |  | 
|---|
|  | 54 | //----------------------------------------------------------------------------- | 
|---|
|  | 55 | // Starting and stopping threads | 
|---|
| [0c92c9f] | 56 | forall( dtype T | is_thread(T) ) | 
|---|
| [09f357ec] | 57 | void __thrd_start( T & this, void (*main_p)(T &) ) { | 
|---|
| [ac2b598] | 58 | $thread * this_thrd = get_thread(this); | 
|---|
| [8118303] | 59 |  | 
|---|
| [1c273d0] | 60 | disable_interrupts(); | 
|---|
| [c7a900a] | 61 | __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread); | 
|---|
| [09f357ec] | 62 |  | 
|---|
| [e8e457e] | 63 | this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP]; | 
|---|
|  | 64 | verify( this_thrd->context.SP ); | 
|---|
| [8118303] | 65 |  | 
|---|
| [9b1dcc2] | 66 | __schedule_thread( (__processor_id_t *)kernelTLS.this_processor, this_thrd); | 
|---|
| [36982fc] | 67 | enable_interrupts( __cfaabi_dbg_ctx ); | 
|---|
| [8118303] | 68 | } | 
|---|
|  | 69 |  | 
|---|
| [8c50aed] | 70 | //----------------------------------------------------------------------------- | 
|---|
|  | 71 | // Support for threads that don't ues the thread keyword | 
|---|
| [242a902] | 72 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } ) | 
|---|
| [65deb18] | 73 | void ?{}( scoped(T)& this ) with( this ) { | 
|---|
|  | 74 | handle{}; | 
|---|
| [09f357ec] | 75 | __thrd_start(handle, main); | 
|---|
| [bd98b58] | 76 | } | 
|---|
|  | 77 |  | 
|---|
| [242a902] | 78 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) | 
|---|
| [65deb18] | 79 | void ?{}( scoped(T)& this, P params ) with( this ) { | 
|---|
|  | 80 | handle{ params }; | 
|---|
| [09f357ec] | 81 | __thrd_start(handle, main); | 
|---|
| [8118303] | 82 | } | 
|---|
|  | 83 |  | 
|---|
| [9f1695b] | 84 | forall( dtype T | sized(T) | is_thread(T) ) | 
|---|
| [65deb18] | 85 | void ^?{}( scoped(T)& this ) with( this ) { | 
|---|
|  | 86 | ^handle{}; | 
|---|
| [44264c5] | 87 | } | 
|---|
|  | 88 |  | 
|---|
| [78b3f52] | 89 | // Local Variables: // | 
|---|
|  | 90 | // mode: c // | 
|---|
|  | 91 | // tab-width: 4 // | 
|---|
| [6a3d2e7] | 92 | // End: // | 
|---|