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