| 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 : Thu Jan 13 20:11:55 2022 | 
|---|
| 13 | // Update Count     : 42 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #define __cforall_thread__ | 
|---|
| 17 | #define _GNU_SOURCE | 
|---|
| 18 |  | 
|---|
| 19 | #include "thread.hfa" | 
|---|
| 20 |  | 
|---|
| 21 | #include "kernel_private.hfa" | 
|---|
| 22 | #include "exception.hfa" | 
|---|
| 23 |  | 
|---|
| 24 | #define __CFA_INVOKE_PRIVATE__ | 
|---|
| 25 | #include "invoke.h" | 
|---|
| 26 |  | 
|---|
| 27 | extern uint32_t __global_random_seed; | 
|---|
| 28 |  | 
|---|
| 29 | //----------------------------------------------------------------------------- | 
|---|
| 30 | // Thread ctors and dtors | 
|---|
| 31 | void ?{}( thread$ & this, const char * const name, cluster & cl, void * storage, size_t storageSize ) with( this ) { | 
|---|
| 32 | context{ 0p, 0p }; | 
|---|
| 33 | self_cor{ name, storage, storageSize }; | 
|---|
| 34 | ticket = TICKET_RUNNING; | 
|---|
| 35 | state = Start; | 
|---|
| 36 | preempted = __NO_PREEMPTION; | 
|---|
| 37 | corctx_flag = false; | 
|---|
| 38 | curr_cor = &self_cor; | 
|---|
| 39 | self_mon.owner = &this; | 
|---|
| 40 | self_mon.recursion = 1; | 
|---|
| 41 | self_mon_p = &self_mon; | 
|---|
| 42 | curr_cluster = &cl; | 
|---|
| 43 | link.next = 0p; | 
|---|
| 44 | link.ts   = -1llu; | 
|---|
| 45 | preferred = ready_queue_new_preferred(); | 
|---|
| 46 | last_proc = 0p; | 
|---|
| 47 | random_state = __global_random_seed; | 
|---|
| 48 | #if defined( __CFA_WITH_VERIFY__ ) | 
|---|
| 49 | canary = 0x0D15EA5E0D15EA5Ep; | 
|---|
| 50 | #endif | 
|---|
| 51 |  | 
|---|
| 52 | seqable.next = 0p; | 
|---|
| 53 | seqable.back = 0p; | 
|---|
| 54 |  | 
|---|
| 55 | node.next = 0p; | 
|---|
| 56 | node.prev = 0p; | 
|---|
| 57 | doregister(curr_cluster, this); | 
|---|
| 58 |  | 
|---|
| 59 | monitors{ &self_mon_p, 1, (fptr_t)0 }; | 
|---|
| 60 | } | 
|---|
| 61 |  | 
|---|
| 62 | void ^?{}(thread$& this) with( this ) { | 
|---|
| 63 | #if defined( __CFA_WITH_VERIFY__ ) | 
|---|
| 64 | canary = 0xDEADDEADDEADDEADp; | 
|---|
| 65 | #endif | 
|---|
| 66 | unregister(curr_cluster, this); | 
|---|
| 67 | ^self_cor{}; | 
|---|
| 68 | } | 
|---|
| 69 |  | 
|---|
| 70 | forall(T &) | 
|---|
| 71 | void copy(ThreadCancelled(T) * dst, ThreadCancelled(T) * src) { | 
|---|
| 72 | dst->virtual_table = src->virtual_table; | 
|---|
| 73 | dst->the_thread = src->the_thread; | 
|---|
| 74 | dst->the_exception = src->the_exception; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | forall(T &) | 
|---|
| 78 | const char * msg(ThreadCancelled(T) *) { | 
|---|
| 79 | return "ThreadCancelled(...)"; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | forall(T &) | 
|---|
| 83 | static void default_thread_cancel_handler(ThreadCancelled(T) & ) { | 
|---|
| 84 | // Improve this error message, can I do formatting? | 
|---|
| 85 | abort( "Unhandled thread cancellation.\n" ); | 
|---|
| 86 | } | 
|---|
| 87 |  | 
|---|
| 88 | forall(T & | is_thread(T) | IS_EXCEPTION(ThreadCancelled, (T)) | 
|---|
| 89 | | { EHM_DEFAULT_VTABLE(ThreadCancelled, (T)); }) | 
|---|
| 90 | void ?{}( thread_dtor_guard_t & this, | 
|---|
| 91 | T & thrd, void(*cancelHandler)(ThreadCancelled(T) &)) { | 
|---|
| 92 | monitor$ * m = get_monitor(thrd); | 
|---|
| 93 | thread$ * desc = get_thread(thrd); | 
|---|
| 94 |  | 
|---|
| 95 | // Setup the monitor guard | 
|---|
| 96 | void (*dtor)(T& mutex this) = ^?{}; | 
|---|
| 97 | bool join = cancelHandler != (void(*)(ThreadCancelled(T)&))0; | 
|---|
| 98 | (this.mg){&m, (void(*)())dtor, join}; | 
|---|
| 99 |  | 
|---|
| 100 |  | 
|---|
| 101 | /* paranoid */ verifyf( Halted == desc->state || Cancelled == desc->state, "Expected thread to be Halted or Cancelled, was %d\n", (int)desc->state ); | 
|---|
| 102 |  | 
|---|
| 103 | // After the guard set-up and any wait, check for cancellation. | 
|---|
| 104 | struct _Unwind_Exception * cancellation = desc->self_cor.cancellation; | 
|---|
| 105 | if ( likely( 0p == cancellation ) ) { | 
|---|
| 106 | return; | 
|---|
| 107 | } else if ( Cancelled == desc->state ) { | 
|---|
| 108 | return; | 
|---|
| 109 | } | 
|---|
| 110 | desc->state = Cancelled; | 
|---|
| 111 | void(*defaultResumptionHandler)(ThreadCancelled(T) &) = | 
|---|
| 112 | join ? cancelHandler : default_thread_cancel_handler; | 
|---|
| 113 |  | 
|---|
| 114 | // TODO: Remove explitate vtable set once trac#186 is fixed. | 
|---|
| 115 | ThreadCancelled(T) except; | 
|---|
| 116 | except.virtual_table = &_default_vtable; | 
|---|
| 117 | except.the_thread = &thrd; | 
|---|
| 118 | except.the_exception = __cfaehm_cancellation_exception( cancellation ); | 
|---|
| 119 | // Why is this cast required? | 
|---|
| 120 | throwResume (ThreadCancelled(T) &)except; | 
|---|
| 121 |  | 
|---|
| 122 | except.the_exception->virtual_table->free( except.the_exception ); | 
|---|
| 123 | free( cancellation ); | 
|---|
| 124 | desc->self_cor.cancellation = 0p; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | void ^?{}( thread_dtor_guard_t & this ) { | 
|---|
| 128 | ^(this.mg){}; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | //----------------------------------------------------------------------------- | 
|---|
| 132 | // Starting and stopping threads | 
|---|
| 133 | forall( T & | is_thread(T) ) | 
|---|
| 134 | void __thrd_start( T & this, void (*main_p)(T &) ) { | 
|---|
| 135 | thread$ * this_thrd = get_thread(this); | 
|---|
| 136 |  | 
|---|
| 137 | disable_interrupts(); | 
|---|
| 138 | __cfactx_start(main_p, get_coroutine(this), this, __cfactx_invoke_thread); | 
|---|
| 139 |  | 
|---|
| 140 | this_thrd->context.[SP, FP] = this_thrd->self_cor.context.[SP, FP]; | 
|---|
| 141 | /* paranoid */ verify( this_thrd->context.SP ); | 
|---|
| 142 |  | 
|---|
| 143 | schedule_thread$( this_thrd, UNPARK_LOCAL ); | 
|---|
| 144 | enable_interrupts(); | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | //----------------------------------------------------------------------------- | 
|---|
| 148 | // Support for threads that don't ues the thread keyword | 
|---|
| 149 | forall( T & | sized(T) | is_thread(T) | { void ?{}(T&); } ) | 
|---|
| 150 | void ?{}( scoped(T)& this ) with( this ) { | 
|---|
| 151 | handle{}; | 
|---|
| 152 | __thrd_start(handle, main); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | forall( T &, P... | sized(T) | is_thread(T) | { void ?{}(T&, P); } ) | 
|---|
| 156 | void ?{}( scoped(T)& this, P params ) with( this ) { | 
|---|
| 157 | handle{ params }; | 
|---|
| 158 | __thrd_start(handle, main); | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | forall( T & | sized(T) | is_thread(T) ) | 
|---|
| 162 | void ^?{}( scoped(T)& this ) with( this ) { | 
|---|
| 163 | ^handle{}; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | //----------------------------------------------------------------------------- | 
|---|
| 167 | forall(T & | is_thread(T) | IS_RESUMPTION_EXCEPTION(ThreadCancelled, (T)) | 
|---|
| 168 | | { EHM_DEFAULT_VTABLE(ThreadCancelled, (T)); }) | 
|---|
| 169 | T & join( T & this ) { | 
|---|
| 170 | thread_dtor_guard_t guard = { this, defaultResumptionHandler }; | 
|---|
| 171 | return this; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | //----------------------------------------------------------------------------- | 
|---|
| 175 | #define GENERATOR LCG | 
|---|
| 176 |  | 
|---|
| 177 | void set_seed( uint32_t seed ) { | 
|---|
| 178 | active_thread()->random_state = __global_random_seed = seed; | 
|---|
| 179 | GENERATOR( active_thread()->random_state ); | 
|---|
| 180 | } // set_seed | 
|---|
| 181 | uint32_t prng( void ) { return GENERATOR( active_thread()->random_state ); } // [0,UINT_MAX] | 
|---|
| 182 |  | 
|---|
| 183 | // Local Variables: // | 
|---|
| 184 | // mode: c // | 
|---|
| 185 | // tab-width: 4 // | 
|---|
| 186 | // End: // | 
|---|