source: libcfa/src/concurrency/thread.cfa@ 478c610

ADT ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 478c610 was c86ee4c, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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