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 | // kernel.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 Jul 9 06:22:54 2020 |
---|
13 | // Update Count : 66 |
---|
14 | // |
---|
15 | |
---|
16 | #define __cforall_thread__ |
---|
17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__ |
---|
18 | |
---|
19 | //C Includes |
---|
20 | #include <errno.h> |
---|
21 | #include <stdio.h> |
---|
22 | #include <signal.h> |
---|
23 | #include <unistd.h> |
---|
24 | |
---|
25 | //CFA Includes |
---|
26 | #include "kernel_private.hfa" |
---|
27 | #include "preemption.hfa" |
---|
28 | |
---|
29 | //Private includes |
---|
30 | #define __CFA_INVOKE_PRIVATE__ |
---|
31 | #include "invoke.h" |
---|
32 | |
---|
33 | |
---|
34 | //----------------------------------------------------------------------------- |
---|
35 | // Some assembly required |
---|
36 | #if defined( __i386 ) |
---|
37 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
38 | // fcw : X87 FPU control word (preserved across function calls) |
---|
39 | #define __x87_store \ |
---|
40 | uint32_t __mxcr; \ |
---|
41 | uint16_t __fcw; \ |
---|
42 | __asm__ volatile ( \ |
---|
43 | "stmxcsr %0\n" \ |
---|
44 | "fnstcw %1\n" \ |
---|
45 | : "=m" (__mxcr),\ |
---|
46 | "=m" (__fcw) \ |
---|
47 | ) |
---|
48 | |
---|
49 | #define __x87_load \ |
---|
50 | __asm__ volatile ( \ |
---|
51 | "fldcw %1\n" \ |
---|
52 | "ldmxcsr %0\n" \ |
---|
53 | ::"m" (__mxcr),\ |
---|
54 | "m" (__fcw) \ |
---|
55 | ) |
---|
56 | |
---|
57 | #elif defined( __x86_64 ) |
---|
58 | #define __x87_store \ |
---|
59 | uint32_t __mxcr; \ |
---|
60 | uint16_t __fcw; \ |
---|
61 | __asm__ volatile ( \ |
---|
62 | "stmxcsr %0\n" \ |
---|
63 | "fnstcw %1\n" \ |
---|
64 | : "=m" (__mxcr),\ |
---|
65 | "=m" (__fcw) \ |
---|
66 | ) |
---|
67 | |
---|
68 | #define __x87_load \ |
---|
69 | __asm__ volatile ( \ |
---|
70 | "fldcw %1\n" \ |
---|
71 | "ldmxcsr %0\n" \ |
---|
72 | :: "m" (__mxcr),\ |
---|
73 | "m" (__fcw) \ |
---|
74 | ) |
---|
75 | |
---|
76 | |
---|
77 | #elif defined( __ARM_ARCH ) |
---|
78 | #else |
---|
79 | #error unknown hardware architecture |
---|
80 | #endif |
---|
81 | |
---|
82 | extern $thread * mainThread; |
---|
83 | extern processor * mainProcessor; |
---|
84 | |
---|
85 | //----------------------------------------------------------------------------- |
---|
86 | // Kernel Scheduling logic |
---|
87 | static $thread * __next_thread(cluster * this); |
---|
88 | static bool __has_next_thread(cluster * this); |
---|
89 | static void __run_thread(processor * this, $thread * dst); |
---|
90 | static bool __wake_one(struct __processor_id_t * id, cluster * cltr); |
---|
91 | static void __halt(processor * this); |
---|
92 | bool __wake_proc(processor *); |
---|
93 | |
---|
94 | //============================================================================================= |
---|
95 | // Kernel Scheduling logic |
---|
96 | //============================================================================================= |
---|
97 | //Main of the processor contexts |
---|
98 | void main(processorCtx_t & runner) { |
---|
99 | // Because of a bug, we couldn't initialized the seed on construction |
---|
100 | // Do it here |
---|
101 | kernelTLS.rand_seed ^= rdtscl(); |
---|
102 | |
---|
103 | processor * this = runner.proc; |
---|
104 | verify(this); |
---|
105 | |
---|
106 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this); |
---|
107 | #if !defined(__CFA_NO_STATISTICS__) |
---|
108 | if( this->print_halts ) { |
---|
109 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this); |
---|
110 | } |
---|
111 | #endif |
---|
112 | |
---|
113 | { |
---|
114 | // Setup preemption data |
---|
115 | preemption_scope scope = { this }; |
---|
116 | |
---|
117 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); |
---|
118 | |
---|
119 | $thread * readyThread = 0p; |
---|
120 | for( unsigned int spin_count = 0;; spin_count++ ) { |
---|
121 | // Try to get the next thread |
---|
122 | readyThread = __next_thread( this->cltr ); |
---|
123 | |
---|
124 | // Check if we actually found a thread |
---|
125 | if( readyThread ) { |
---|
126 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
127 | /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted); |
---|
128 | /* paranoid */ verifyf( readyThread->link.next == 0p, "Expected null got %p", readyThread->link.next ); |
---|
129 | __builtin_prefetch( readyThread->context.SP ); |
---|
130 | |
---|
131 | // We found a thread run it |
---|
132 | __run_thread(this, readyThread); |
---|
133 | |
---|
134 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
135 | } |
---|
136 | |
---|
137 | if(__atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST)) break; |
---|
138 | |
---|
139 | if( !readyThread ) { |
---|
140 | // Block until a thread is ready |
---|
141 | __halt(this); |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this); |
---|
146 | } |
---|
147 | |
---|
148 | V( this->terminated ); |
---|
149 | |
---|
150 | if(this == mainProcessor) { |
---|
151 | // HACK : the coroutine context switch expects this_thread to be set |
---|
152 | // and it make sense for it to be set in all other cases except here |
---|
153 | // fake it |
---|
154 | kernelTLS.this_thread = mainThread; |
---|
155 | } |
---|
156 | |
---|
157 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this); |
---|
158 | } |
---|
159 | |
---|
160 | static int * __volatile_errno() __attribute__((noinline)); |
---|
161 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
162 | |
---|
163 | // KERNEL ONLY |
---|
164 | // runThread runs a thread by context switching |
---|
165 | // from the processor coroutine to the target thread |
---|
166 | static void __run_thread(processor * this, $thread * thrd_dst) { |
---|
167 | $coroutine * proc_cor = get_coroutine(this->runner); |
---|
168 | |
---|
169 | // Update global state |
---|
170 | kernelTLS.this_thread = thrd_dst; |
---|
171 | |
---|
172 | // set state of processor coroutine to inactive |
---|
173 | verify(proc_cor->state == Active); |
---|
174 | proc_cor->state = Blocked; |
---|
175 | |
---|
176 | // Actually run the thread |
---|
177 | RUNNING: while(true) { |
---|
178 | thrd_dst->preempted = __NO_PREEMPTION; |
---|
179 | thrd_dst->state = Active; |
---|
180 | |
---|
181 | __cfaabi_dbg_debug_do( |
---|
182 | thrd_dst->park_stale = true; |
---|
183 | thrd_dst->unpark_stale = true; |
---|
184 | ) |
---|
185 | |
---|
186 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
187 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst ); |
---|
188 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
189 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
190 | |
---|
191 | // set context switch to the thread that the processor is executing |
---|
192 | verify( thrd_dst->context.SP ); |
---|
193 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
194 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
195 | |
---|
196 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); |
---|
197 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); |
---|
198 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst ); |
---|
199 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
200 | |
---|
201 | |
---|
202 | // We just finished running a thread, there are a few things that could have happened. |
---|
203 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
204 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
205 | // 4 - Preempted |
---|
206 | // In case 1, we may have won a race so we can't write to the state again. |
---|
207 | // In case 2, we lost the race so we now own the thread. |
---|
208 | |
---|
209 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
210 | // The thread was preempted, reschedule it and reset the flag |
---|
211 | __schedule_thread( (__processor_id_t*)this, thrd_dst ); |
---|
212 | break RUNNING; |
---|
213 | } |
---|
214 | |
---|
215 | if(unlikely(thrd_dst->state == Halted)) { |
---|
216 | // The thread has halted, it should never be scheduled/run again |
---|
217 | // We may need to wake someone up here since |
---|
218 | unpark( this->destroyer __cfaabi_dbg_ctx2 ); |
---|
219 | this->destroyer = 0p; |
---|
220 | break RUNNING; |
---|
221 | } |
---|
222 | |
---|
223 | /* paranoid */ verify( thrd_dst->state == Active ); |
---|
224 | thrd_dst->state = Blocked; |
---|
225 | |
---|
226 | // set state of processor coroutine to active and the thread to inactive |
---|
227 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST); |
---|
228 | __cfaabi_dbg_debug_do( thrd_dst->park_result = old_ticket; ) |
---|
229 | switch(old_ticket) { |
---|
230 | case 1: |
---|
231 | // This is case 1, the regular case, nothing more is needed |
---|
232 | break RUNNING; |
---|
233 | case 2: |
---|
234 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
235 | // In this case, just run it again. |
---|
236 | continue RUNNING; |
---|
237 | default: |
---|
238 | // This makes no sense, something is wrong abort |
---|
239 | abort(); |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | // Just before returning to the processor, set the processor coroutine to active |
---|
244 | proc_cor->state = Active; |
---|
245 | kernelTLS.this_thread = 0p; |
---|
246 | } |
---|
247 | |
---|
248 | // KERNEL_ONLY |
---|
249 | void returnToKernel() { |
---|
250 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
251 | $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner); |
---|
252 | $thread * thrd_src = kernelTLS.this_thread; |
---|
253 | |
---|
254 | #if !defined(__CFA_NO_STATISTICS__) |
---|
255 | struct processor * last_proc = kernelTLS.this_processor; |
---|
256 | #endif |
---|
257 | |
---|
258 | // Run the thread on this processor |
---|
259 | { |
---|
260 | int local_errno = *__volatile_errno(); |
---|
261 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
262 | __x87_store; |
---|
263 | #endif |
---|
264 | verify( proc_cor->context.SP ); |
---|
265 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
266 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
267 | __x87_load; |
---|
268 | #endif |
---|
269 | *__volatile_errno() = local_errno; |
---|
270 | } |
---|
271 | |
---|
272 | #if !defined(__CFA_NO_STATISTICS__) |
---|
273 | if(last_proc != kernelTLS.this_processor) { |
---|
274 | __tls_stats()->ready.threads.migration++; |
---|
275 | } |
---|
276 | #endif |
---|
277 | |
---|
278 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
279 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src ); |
---|
280 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src ); |
---|
281 | } |
---|
282 | |
---|
283 | //----------------------------------------------------------------------------- |
---|
284 | // Scheduler routines |
---|
285 | // KERNEL ONLY |
---|
286 | void __schedule_thread( struct __processor_id_t * id, $thread * thrd ) { |
---|
287 | /* paranoid */ verify( thrd ); |
---|
288 | /* paranoid */ verify( thrd->state != Halted ); |
---|
289 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
290 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
291 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
292 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
293 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active, |
---|
294 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
295 | /* paranoid */ #endif |
---|
296 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next ); |
---|
297 | |
---|
298 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
299 | |
---|
300 | ready_schedule_lock ( id ); |
---|
301 | push( thrd->curr_cluster, thrd ); |
---|
302 | |
---|
303 | #if !defined(__CFA_NO_STATISTICS__) |
---|
304 | bool woke = |
---|
305 | #endif |
---|
306 | __wake_one(id, thrd->curr_cluster); |
---|
307 | |
---|
308 | #if !defined(__CFA_NO_STATISTICS__) |
---|
309 | if(woke) __tls_stats()->ready.sleep.wakes++; |
---|
310 | #endif |
---|
311 | ready_schedule_unlock( id ); |
---|
312 | |
---|
313 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
314 | } |
---|
315 | |
---|
316 | // KERNEL ONLY |
---|
317 | static $thread * __next_thread(cluster * this) with( *this ) { |
---|
318 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
319 | |
---|
320 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor ); |
---|
321 | $thread * head = pop( this ); |
---|
322 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor ); |
---|
323 | |
---|
324 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
325 | return head; |
---|
326 | } |
---|
327 | |
---|
328 | // KERNEL ONLY |
---|
329 | static bool __has_next_thread(cluster * this) with( *this ) { |
---|
330 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
331 | |
---|
332 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor ); |
---|
333 | bool not_empty = query( this ); |
---|
334 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor ); |
---|
335 | |
---|
336 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
337 | return not_empty; |
---|
338 | } |
---|
339 | |
---|
340 | // KERNEL ONLY unpark with out disabling interrupts |
---|
341 | void __unpark( struct __processor_id_t * id, $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
342 | // record activity |
---|
343 | __cfaabi_dbg_record_thrd( *thrd, false, caller ); |
---|
344 | |
---|
345 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); |
---|
346 | __cfaabi_dbg_debug_do( thrd->unpark_result = old_ticket; thrd->unpark_state = thrd->state; ) |
---|
347 | switch(old_ticket) { |
---|
348 | case 1: |
---|
349 | // Wake won the race, the thread will reschedule/rerun itself |
---|
350 | break; |
---|
351 | case 0: |
---|
352 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
353 | /* paranoid */ verify( thrd->state == Blocked ); |
---|
354 | |
---|
355 | // Wake lost the race, |
---|
356 | __schedule_thread( id, thrd ); |
---|
357 | break; |
---|
358 | default: |
---|
359 | // This makes no sense, something is wrong abort |
---|
360 | abort(); |
---|
361 | } |
---|
362 | } |
---|
363 | |
---|
364 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
365 | if( !thrd ) return; |
---|
366 | |
---|
367 | disable_interrupts(); |
---|
368 | __unpark( (__processor_id_t*)kernelTLS.this_processor, thrd __cfaabi_dbg_ctx_fwd2 ); |
---|
369 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
370 | } |
---|
371 | |
---|
372 | void park( __cfaabi_dbg_ctx_param ) { |
---|
373 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
374 | disable_interrupts(); |
---|
375 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
376 | /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION ); |
---|
377 | |
---|
378 | // record activity |
---|
379 | __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller ); |
---|
380 | |
---|
381 | returnToKernel(); |
---|
382 | |
---|
383 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
384 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
385 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
386 | |
---|
387 | } |
---|
388 | |
---|
389 | // KERNEL ONLY |
---|
390 | void __leave_thread() { |
---|
391 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
392 | returnToKernel(); |
---|
393 | abort(); |
---|
394 | } |
---|
395 | |
---|
396 | // KERNEL ONLY |
---|
397 | bool force_yield( __Preemption_Reason reason ) { |
---|
398 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
399 | disable_interrupts(); |
---|
400 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
401 | |
---|
402 | $thread * thrd = kernelTLS.this_thread; |
---|
403 | /* paranoid */ verify(thrd->state == Active); |
---|
404 | |
---|
405 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
406 | // it was going to park itself. If that is the case and it is already using the |
---|
407 | // intrusive fields then we can't use them to preempt the thread |
---|
408 | // If that is the case, abandon the preemption. |
---|
409 | bool preempted = false; |
---|
410 | if(thrd->link.next == 0p) { |
---|
411 | preempted = true; |
---|
412 | thrd->preempted = reason; |
---|
413 | returnToKernel(); |
---|
414 | } |
---|
415 | |
---|
416 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
417 | enable_interrupts_noPoll(); |
---|
418 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
419 | |
---|
420 | return preempted; |
---|
421 | } |
---|
422 | |
---|
423 | //============================================================================================= |
---|
424 | // Kernel Idle Sleep |
---|
425 | //============================================================================================= |
---|
426 | // Wake a thread from the front if there are any |
---|
427 | static bool __wake_one(struct __processor_id_t * id, cluster * this) { |
---|
428 | /* paranoid */ verify( ready_schedule_islocked( id ) ); |
---|
429 | |
---|
430 | // Check if there is a sleeping processor |
---|
431 | processor * p = pop(this->idles); |
---|
432 | |
---|
433 | // If no one is sleeping, we are done |
---|
434 | if( 0p == p ) return false; |
---|
435 | |
---|
436 | // We found a processor, wake it up |
---|
437 | post( p->idle ); |
---|
438 | |
---|
439 | return true; |
---|
440 | } |
---|
441 | |
---|
442 | // Unconditionnaly wake a thread |
---|
443 | bool __wake_proc(processor * this) { |
---|
444 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
445 | |
---|
446 | disable_interrupts(); |
---|
447 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
448 | bool ret = post( this->idle ); |
---|
449 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
450 | |
---|
451 | return ret; |
---|
452 | } |
---|
453 | |
---|
454 | static void __halt(processor * this) with( *this ) { |
---|
455 | if( do_terminate ) return; |
---|
456 | |
---|
457 | #if !defined(__CFA_NO_STATISTICS__) |
---|
458 | __tls_stats()->ready.sleep.halts++; |
---|
459 | #endif |
---|
460 | // Push self to queue |
---|
461 | push(cltr->idles, *this); |
---|
462 | |
---|
463 | // Makre sure we don't miss a thread |
---|
464 | if( __has_next_thread(cltr) ) { |
---|
465 | // A thread was posted, make sure a processor is woken up |
---|
466 | struct __processor_id_t *id = (struct __processor_id_t *) this; |
---|
467 | ready_schedule_lock ( id ); |
---|
468 | __wake_one( id, cltr ); |
---|
469 | ready_schedule_unlock( id ); |
---|
470 | #if !defined(__CFA_NO_STATISTICS__) |
---|
471 | __tls_stats()->ready.sleep.cancels++; |
---|
472 | #endif |
---|
473 | } |
---|
474 | |
---|
475 | #if !defined(__CFA_NO_STATISTICS__) |
---|
476 | if(this->print_halts) { |
---|
477 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl()); |
---|
478 | } |
---|
479 | #endif |
---|
480 | |
---|
481 | wait( idle ); |
---|
482 | |
---|
483 | #if !defined(__CFA_NO_STATISTICS__) |
---|
484 | if(this->print_halts) { |
---|
485 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl()); |
---|
486 | } |
---|
487 | #endif |
---|
488 | } |
---|
489 | |
---|
490 | //============================================================================================= |
---|
491 | // Unexpected Terminating logic |
---|
492 | //============================================================================================= |
---|
493 | static __spinlock_t kernel_abort_lock; |
---|
494 | static bool kernel_abort_called = false; |
---|
495 | |
---|
496 | void * kernel_abort(void) __attribute__ ((__nothrow__)) { |
---|
497 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
498 | // the globalAbort flag is true. |
---|
499 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
500 | |
---|
501 | // first task to abort ? |
---|
502 | if ( kernel_abort_called ) { // not first task to abort ? |
---|
503 | unlock( kernel_abort_lock ); |
---|
504 | |
---|
505 | sigset_t mask; |
---|
506 | sigemptyset( &mask ); |
---|
507 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
508 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals |
---|
509 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
510 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
511 | } |
---|
512 | else { |
---|
513 | kernel_abort_called = true; |
---|
514 | unlock( kernel_abort_lock ); |
---|
515 | } |
---|
516 | |
---|
517 | return kernelTLS.this_thread; |
---|
518 | } |
---|
519 | |
---|
520 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
521 | $thread * thrd = ( $thread * ) kernel_data; |
---|
522 | |
---|
523 | if(thrd) { |
---|
524 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
525 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
526 | |
---|
527 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
528 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
529 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
530 | } |
---|
531 | else { |
---|
532 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
533 | } |
---|
534 | } |
---|
535 | else { |
---|
536 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
537 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
538 | } |
---|
539 | } |
---|
540 | |
---|
541 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
542 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
543 | } |
---|
544 | |
---|
545 | static __spinlock_t kernel_debug_lock; |
---|
546 | |
---|
547 | extern "C" { |
---|
548 | void __cfaabi_bits_acquire() { |
---|
549 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
550 | } |
---|
551 | |
---|
552 | void __cfaabi_bits_release() { |
---|
553 | unlock( kernel_debug_lock ); |
---|
554 | } |
---|
555 | } |
---|
556 | |
---|
557 | //============================================================================================= |
---|
558 | // Kernel Utilities |
---|
559 | //============================================================================================= |
---|
560 | //----------------------------------------------------------------------------- |
---|
561 | // Locks |
---|
562 | void ?{}( semaphore & this, int count = 1 ) { |
---|
563 | (this.lock){}; |
---|
564 | this.count = count; |
---|
565 | (this.waiting){}; |
---|
566 | } |
---|
567 | void ^?{}(semaphore & this) {} |
---|
568 | |
---|
569 | bool P(semaphore & this) with( this ){ |
---|
570 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
571 | count -= 1; |
---|
572 | if ( count < 0 ) { |
---|
573 | // queue current task |
---|
574 | append( waiting, kernelTLS.this_thread ); |
---|
575 | |
---|
576 | // atomically release spin lock and block |
---|
577 | unlock( lock ); |
---|
578 | park( __cfaabi_dbg_ctx ); |
---|
579 | return true; |
---|
580 | } |
---|
581 | else { |
---|
582 | unlock( lock ); |
---|
583 | return false; |
---|
584 | } |
---|
585 | } |
---|
586 | |
---|
587 | bool V(semaphore & this) with( this ) { |
---|
588 | $thread * thrd = 0p; |
---|
589 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
590 | count += 1; |
---|
591 | if ( count <= 0 ) { |
---|
592 | // remove task at head of waiting list |
---|
593 | thrd = pop_head( waiting ); |
---|
594 | } |
---|
595 | |
---|
596 | unlock( lock ); |
---|
597 | |
---|
598 | // make new owner |
---|
599 | unpark( thrd __cfaabi_dbg_ctx2 ); |
---|
600 | |
---|
601 | return thrd != 0p; |
---|
602 | } |
---|
603 | |
---|
604 | bool V(semaphore & this, unsigned diff) with( this ) { |
---|
605 | $thread * thrd = 0p; |
---|
606 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
607 | int release = max(-count, (int)diff); |
---|
608 | count += diff; |
---|
609 | for(release) { |
---|
610 | unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 ); |
---|
611 | } |
---|
612 | |
---|
613 | unlock( lock ); |
---|
614 | |
---|
615 | return thrd != 0p; |
---|
616 | } |
---|
617 | |
---|
618 | //----------------------------------------------------------------------------- |
---|
619 | // Debug |
---|
620 | __cfaabi_dbg_debug_do( |
---|
621 | extern "C" { |
---|
622 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
623 | this.prev_name = prev_name; |
---|
624 | this.prev_thrd = kernelTLS.this_thread; |
---|
625 | } |
---|
626 | |
---|
627 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) { |
---|
628 | if(park) { |
---|
629 | this.park_caller = prev_name; |
---|
630 | this.park_stale = false; |
---|
631 | } |
---|
632 | else { |
---|
633 | this.unpark_caller = prev_name; |
---|
634 | this.unpark_stale = false; |
---|
635 | } |
---|
636 | } |
---|
637 | } |
---|
638 | ) |
---|
639 | |
---|
640 | //----------------------------------------------------------------------------- |
---|
641 | // Debug |
---|
642 | bool threading_enabled(void) __attribute__((const)) { |
---|
643 | return true; |
---|
644 | } |
---|
645 | |
---|
646 | //----------------------------------------------------------------------------- |
---|
647 | // Statistics |
---|
648 | #if !defined(__CFA_NO_STATISTICS__) |
---|
649 | void print_halts( processor & this ) { |
---|
650 | this.print_halts = true; |
---|
651 | } |
---|
652 | #endif |
---|
653 | // Local Variables: // |
---|
654 | // mode: c // |
---|
655 | // tab-width: 4 // |
---|
656 | // End: // |
---|