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