| 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 | //
 | 
|---|
| 7 | // thread.c --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // Created On       : Tue Jan 17 12:27:26 2017
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Wed Dec  4 09:17:49 2019
 | 
|---|
| 13 | // Update Count     : 9
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #define __cforall_thread__
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "thread.hfa"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "kernel_private.hfa"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #define __CFA_INVOKE_PRIVATE__
 | 
|---|
| 23 | #include "invoke.h"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | //-----------------------------------------------------------------------------
 | 
|---|
| 26 | // Thread ctors and dtors
 | 
|---|
| 27 | void ?{}($thread & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) {
 | 
|---|
| 28 |         context{ 0p, 0p };
 | 
|---|
| 29 |         self_cor{ name, storage, storageSize };
 | 
|---|
| 30 |         ticket = 1;
 | 
|---|
| 31 |         state = Start;
 | 
|---|
| 32 |         preempted = __NO_PREEMPTION;
 | 
|---|
| 33 |         curr_cor = &self_cor;
 | 
|---|
| 34 |         self_mon.owner = &this;
 | 
|---|
| 35 |         self_mon.recursion = 1;
 | 
|---|
| 36 |         self_mon_p = &self_mon;
 | 
|---|
| 37 |         curr_cluster = &cl;
 | 
|---|
| 38 |         link.next = 0p;
 | 
|---|
| 39 |         link.prev = 0p;
 | 
|---|
| 40 |         link.preferred = -1;
 | 
|---|
| 41 |         #if defined( __CFA_WITH_VERIFY__ )
 | 
|---|
| 42 |                 canary = 0x0D15EA5E0D15EA5E;
 | 
|---|
| 43 |         #endif
 | 
|---|
| 44 | 
 | 
|---|
| 45 |         node.next = 0p;
 | 
|---|
| 46 |         node.prev = 0p;
 | 
|---|
| 47 |         doregister(curr_cluster, this);
 | 
|---|
| 48 | 
 | 
|---|
| 49 |         monitors{ &self_mon_p, 1, (fptr_t)0 };
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | void ^?{}($thread& this) with( this ) {
 | 
|---|
| 53 |         #if defined( __CFA_WITH_VERIFY__ )
 | 
|---|
| 54 |                 canary = 0xDEADDEADDEADDEAD;
 | 
|---|
| 55 |         #endif
 | 
|---|
| 56 |         unregister(curr_cluster, this);
 | 
|---|
| 57 |         ^self_cor{};
 | 
|---|
| 58 | }
 | 
|---|
| 59 | 
 | 
|---|
| 60 | //-----------------------------------------------------------------------------
 | 
|---|
| 61 | // Starting and stopping threads
 | 
|---|
| 62 | forall( dtype T | is_thread(T) )
 | 
|---|
| 63 | void __thrd_start( T & this, void (*main_p)(T &) ) {
 | 
|---|
| 64 |         $thread * this_thrd = get_thread(this);
 | 
|---|
| 65 | 
 | 
|---|
| 66 |         disable_interrupts();
 | 
|---|
| 67 |         __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |         this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP];
 | 
|---|
| 70 |         verify( this_thrd->context.SP );
 | 
|---|
| 71 | 
 | 
|---|
| 72 |         __schedule_thread( (__processor_id_t *)kernelTLS.this_processor, this_thrd);
 | 
|---|
| 73 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| 74 | }
 | 
|---|
| 75 | 
 | 
|---|
| 76 | //-----------------------------------------------------------------------------
 | 
|---|
| 77 | // Support for threads that don't ues the thread keyword
 | 
|---|
| 78 | forall( dtype T | sized(T) | is_thread(T) | { void ?{}(T&); } )
 | 
|---|
| 79 | void ?{}( scoped(T)& this ) with( this ) {
 | 
|---|
| 80 |         handle{};
 | 
|---|
| 81 |         __thrd_start(handle, main);
 | 
|---|
| 82 | }
 | 
|---|
| 83 | 
 | 
|---|
| 84 | forall( dtype T, ttype P | sized(T) | is_thread(T) | { void ?{}(T&, P); } )
 | 
|---|
| 85 | void ?{}( scoped(T)& this, P params ) with( this ) {
 | 
|---|
| 86 |         handle{ params };
 | 
|---|
| 87 |         __thrd_start(handle, main);
 | 
|---|
| 88 | }
 | 
|---|
| 89 | 
 | 
|---|
| 90 | forall( dtype T | sized(T) | is_thread(T) )
 | 
|---|
| 91 | void ^?{}( scoped(T)& this ) with( this ) {
 | 
|---|
| 92 |         ^handle{};
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | // Local Variables: //
 | 
|---|
| 96 | // mode: c //
 | 
|---|
| 97 | // tab-width: 4 //
 | 
|---|
| 98 | // End: //
 | 
|---|