source: libcfa/src/concurrency/kernel.cfa@ d5d3a90

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since d5d3a90 was 95789be, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

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

  • Property mode set to 100644
File size: 20.4 KB
RevLine 
[8118303]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
[75f3522]10// Created On : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[bb83b47]12// Last Modified On : Thu Jul 9 06:22:54 2020
13// Update Count : 66
[8118303]14//
15
[2026bb6]16#define __cforall_thread__
[4069faad]17// #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
[2026bb6]18
[8118303]19//C Includes
[214e8da]20#include <errno.h>
[9d944b2]21#include <stdio.h>
[58b6d1b]22#include <signal.h>
[9d944b2]23#include <unistd.h>
[8118303]24
25//CFA Includes
[73abe95]26#include "kernel_private.hfa"
27#include "preemption.hfa"
[8118303]28
29//Private includes
30#define __CFA_INVOKE_PRIVATE__
31#include "invoke.h"
32
[4069faad]33
[deca0f5]34//-----------------------------------------------------------------------------
35// Some assembly required
[1805b1b]36#if defined( __i386 )
[deca0f5]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
[e660761]82extern $thread * mainThread;
83extern processor * mainProcessor;
[2ac095d]84
[92e7631]85//-----------------------------------------------------------------------------
86// Kernel Scheduling logic
87static $thread * __next_thread(cluster * this);
[64a7146]88static bool __has_next_thread(cluster * this);
[92e7631]89static void __run_thread(processor * this, $thread * dst);
[64a7146]90static bool __wake_one(struct __processor_id_t * id, cluster * cltr);
91static void __halt(processor * this);
[e660761]92bool __wake_proc(processor *);
[c84e80a]93
[75f3522]94//=============================================================================================
95// Kernel Scheduling logic
96//=============================================================================================
[8fcbb4c]97//Main of the processor contexts
[83a071f9]98void main(processorCtx_t & runner) {
[21184e3]99 // Because of a bug, we couldn't initialized the seed on construction
100 // Do it here
[7768b8d]101 kernelTLS.rand_seed ^= rdtscl();
[21184e3]102
[83a071f9]103 processor * this = runner.proc;
[094476d]104 verify(this);
[c81ebf9]105
[4069faad]106 __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
[28d73c1]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
[b798713]112
[75f3522]113 {
[c81ebf9]114 // Setup preemption data
115 preemption_scope scope = { this };
116
[4069faad]117 __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
[8118303]118
[ac2b598]119 $thread * readyThread = 0p;
[398e8e9]120 for( unsigned int spin_count = 0;; spin_count++ ) {
[92e7631]121 // Try to get the next thread
[8c50aed]122 readyThread = __next_thread( this->cltr );
[75f3522]123
[92e7631]124 // Check if we actually found a thread
125 if( readyThread ) {
[3381ed7]126 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[92e7631]127 /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted);
[504a7dc]128 /* paranoid */ verifyf( readyThread->link.next == 0p, "Expected null got %p", readyThread->link.next );
[04b5cef]129 __builtin_prefetch( readyThread->context.SP );
[4e6fb8e]130
[92e7631]131 // We found a thread run it
[8c50aed]132 __run_thread(this, readyThread);
[c81ebf9]133
[3381ed7]134 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[c81ebf9]135 }
[398e8e9]136
137 if(__atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST)) break;
138
139 if( !readyThread ) {
[64a7146]140 // Block until a thread is ready
141 __halt(this);
142 }
[c81ebf9]143 }
144
[4069faad]145 __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
[c84e80a]146 }
[8118303]147
[4cedd9f]148 V( this->terminated );
[bdeba0b]149
[28d73c1]150 if(this == mainProcessor) {
[6a490b2]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 }
[7768b8d]156
[4069faad]157 __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
[c84e80a]158}
159
[5c1a531]160static int * __volatile_errno() __attribute__((noinline));
161static int * __volatile_errno() { asm(""); return &errno; }
162
[14a61b5]163// KERNEL ONLY
[1c273d0]164// runThread runs a thread by context switching
165// from the processor coroutine to the target thread
[ac2b598]166static void __run_thread(processor * this, $thread * thrd_dst) {
167 $coroutine * proc_cor = get_coroutine(this->runner);
[8fcbb4c]168
[14a61b5]169 // Update global state
[e8e457e]170 kernelTLS.this_thread = thrd_dst;
171
[9f575ea]172 // set state of processor coroutine to inactive
173 verify(proc_cor->state == Active);
[ae7be7a]174 proc_cor->state = Blocked;
[e8e457e]175
[9f575ea]176 // Actually run the thread
[3381ed7]177 RUNNING: while(true) {
[ff79d5e]178 thrd_dst->preempted = __NO_PREEMPTION;
179 thrd_dst->state = Active;
[e8e457e]180
[ae66348]181 __cfaabi_dbg_debug_do(
[276ae57e]182 thrd_dst->park_stale = true;
183 thrd_dst->unpark_stale = true;
[ae66348]184 )
[75f3522]185
[3381ed7]186 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[a7b486b]187 /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
[210b8b3]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
[3381ed7]190
[9f575ea]191 // set context switch to the thread that the processor is executing
192 verify( thrd_dst->context.SP );
[c7a900a]193 __cfactx_switch( &proc_cor->context, &thrd_dst->context );
194 // when __cfactx_switch returns we are back in the processor coroutine
[9f575ea]195
[210b8b3]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 );
[a7b486b]198 /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
[3381ed7]199 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[75f3522]200
[3381ed7]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
[9b1dcc2]211 __schedule_thread( (__processor_id_t*)this, thrd_dst );
[3381ed7]212 break RUNNING;
213 }
[75f3522]214
[ff79d5e]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
[3381ed7]226 // set state of processor coroutine to active and the thread to inactive
[ff79d5e]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:
[3381ed7]231 // This is case 1, the regular case, nothing more is needed
232 break RUNNING;
[ff79d5e]233 case 2:
[3381ed7]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
[ff79d5e]239 abort();
[3381ed7]240 }
[9f575ea]241 }
[e8e457e]242
[9f575ea]243 // Just before returning to the processor, set the processor coroutine to active
[e8e457e]244 proc_cor->state = Active;
[ae7be7a]245 kernelTLS.this_thread = 0p;
[82c948c]246}
247
[14a61b5]248// KERNEL_ONLY
[b0c7419]249void returnToKernel() {
[3381ed7]250 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[ac2b598]251 $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
252 $thread * thrd_src = kernelTLS.this_thread;
[e8e457e]253
[29cb302]254 #if !defined(__CFA_NO_STATISTICS__)
255 struct processor * last_proc = kernelTLS.this_processor;
256 #endif
257
[9f575ea]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 );
[c7a900a]265 __cfactx_switch( &thrd_src->context, &proc_cor->context );
[9f575ea]266 #if defined( __i386 ) || defined( __x86_64 )
267 __x87_load;
268 #endif
269 *__volatile_errno() = local_errno;
[8fcbb4c]270 }
[deca0f5]271
[29cb302]272 #if !defined(__CFA_NO_STATISTICS__)
273 if(last_proc != kernelTLS.this_processor) {
274 __tls_stats()->ready.threads.migration++;
275 }
276 #endif
277
[3381ed7]278 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[210b8b3]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 );
[c84e80a]281}
282
[8def349]283//-----------------------------------------------------------------------------
284// Scheduler routines
[14a61b5]285// KERNEL ONLY
[9b1dcc2]286void __schedule_thread( struct __processor_id_t * id, $thread * thrd ) {
[6a490b2]287 /* paranoid */ verify( thrd );
288 /* paranoid */ verify( thrd->state != Halted );
[9f575ea]289 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[3381ed7]290 /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
[504a7dc]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 );
[ff79d5e]293 /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
[504a7dc]294 "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
[3381ed7]295 /* paranoid */ #endif
[6a490b2]296 /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
[9f575ea]297
[ae7be7a]298 if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
[6b4cdd3]299
[9b1dcc2]300 ready_schedule_lock ( id );
[504a7dc]301 push( thrd->curr_cluster, thrd );
[b798713]302
[68f36f4]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
[9b1dcc2]311 ready_schedule_unlock( id );
[1c273d0]312
[9f575ea]313 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]314}
315
[14a61b5]316// KERNEL ONLY
[ac2b598]317static $thread * __next_thread(cluster * this) with( *this ) {
[3381ed7]318 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[7768b8d]319
[9b1dcc2]320 ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor );
[6a490b2]321 $thread * head = pop( this );
[9b1dcc2]322 ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor );
[7768b8d]323
[3381ed7]324 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[db6f06a]325 return head;
[eb2e723]326}
327
[64a7146]328// KERNEL ONLY
329static 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
[2d8f7b0]340// KERNEL ONLY unpark with out disabling interrupts
[9b1dcc2]341void __unpark( struct __processor_id_t * id, $thread * thrd __cfaabi_dbg_ctx_param2 ) {
[ae66348]342 // record activity
343 __cfaabi_dbg_record_thrd( *thrd, false, caller );
344
[ff79d5e]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:
[3381ed7]349 // Wake won the race, the thread will reschedule/rerun itself
350 break;
[ff79d5e]351 case 0:
[3381ed7]352 /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
[ff79d5e]353 /* paranoid */ verify( thrd->state == Blocked );
[0b33412]354
[3381ed7]355 // Wake lost the race,
[9b1dcc2]356 __schedule_thread( id, thrd );
[3381ed7]357 break;
358 default:
359 // This makes no sense, something is wrong abort
360 abort();
[de6319f]361 }
[db6f06a]362}
363
[2d8f7b0]364void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) {
365 if( !thrd ) return;
[db6f06a]366
[82ff5845]367 disable_interrupts();
[9b1dcc2]368 __unpark( (__processor_id_t*)kernelTLS.this_processor, thrd __cfaabi_dbg_ctx_fwd2 );
[36982fc]369 enable_interrupts( __cfaabi_dbg_ctx );
[eb2e723]370}
371
[ae66348]372void park( __cfaabi_dbg_ctx_param ) {
[3381ed7]373 /* paranoid */ verify( kernelTLS.preemption_state.enabled );
[82ff5845]374 disable_interrupts();
[3381ed7]375 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
376 /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION );
[0b33412]377
[ae66348]378 // record activity
379 __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller );
[0b33412]380
[82c948c]381 returnToKernel();
[0b33412]382
[3381ed7]383 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[36982fc]384 enable_interrupts( __cfaabi_dbg_ctx );
[3381ed7]385 /* paranoid */ verify( kernelTLS.preemption_state.enabled );
[0c78741]386
387}
[09800e9]388
[3381ed7]389// KERNEL ONLY
[b0c7419]390void __leave_thread() {
[3381ed7]391 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[09800e9]392 returnToKernel();
[b0c7419]393 abort();
[09800e9]394}
395
[14a61b5]396// KERNEL ONLY
[3381ed7]397bool force_yield( __Preemption_Reason reason ) {
398 /* paranoid */ verify( kernelTLS.preemption_state.enabled );
[09800e9]399 disable_interrupts();
[3381ed7]400 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
[09800e9]401
[ac2b598]402 $thread * thrd = kernelTLS.this_thread;
[ff79d5e]403 /* paranoid */ verify(thrd->state == Active);
[3381ed7]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;
[504a7dc]410 if(thrd->link.next == 0p) {
[3381ed7]411 preempted = true;
412 thrd->preempted = reason;
413 returnToKernel();
[de6319f]414 }
[f2b12406]415
[3381ed7]416 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
417 enable_interrupts_noPoll();
418 /* paranoid */ verify( kernelTLS.preemption_state.enabled );
[f2b12406]419
[3381ed7]420 return preempted;
[f2b12406]421}
422
[14a61b5]423//=============================================================================================
[92e7631]424// Kernel Idle Sleep
[14a61b5]425//=============================================================================================
[64a7146]426// Wake a thread from the front if there are any
427static bool __wake_one(struct __processor_id_t * id, cluster * this) {
428 /* paranoid */ verify( ready_schedule_islocked( id ) );
[14a61b5]429
[64a7146]430 // Check if there is a sleeping processor
431 processor * p = pop(this->idles);
[14a61b5]432
[64a7146]433 // If no one is sleeping, we are done
434 if( 0p == p ) return false;
[14a61b5]435
[64a7146]436 // We found a processor, wake it up
437 post( p->idle );
[14a61b5]438
[64a7146]439 return true;
440}
[14a61b5]441
[64a7146]442// Unconditionnaly wake a thread
[e660761]443bool __wake_proc(processor * this) {
[64a7146]444 __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
[14a61b5]445
[64a7146]446 disable_interrupts();
447 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
448 bool ret = post( this->idle );
449 enable_interrupts( __cfaabi_dbg_ctx );
[92e7631]450
[64a7146]451 return ret;
[92e7631]452}
453
[64a7146]454static void __halt(processor * this) with( *this ) {
455 if( do_terminate ) return;
[8e16177]456
[68f36f4]457 #if !defined(__CFA_NO_STATISTICS__)
458 __tls_stats()->ready.sleep.halts++;
459 #endif
[64a7146]460 // Push self to queue
461 push(cltr->idles, *this);
[8834751]462
[64a7146]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 );
[68f36f4]470 #if !defined(__CFA_NO_STATISTICS__)
471 __tls_stats()->ready.sleep.cancels++;
472 #endif
[64a7146]473 }
[8e16177]474
[c34ebf2]475 #if !defined(__CFA_NO_STATISTICS__)
476 if(this->print_halts) {
[f2b18d01]477 __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl());
[c34ebf2]478 }
479 #endif
480
[64a7146]481 wait( idle );
[c34ebf2]482
483 #if !defined(__CFA_NO_STATISTICS__)
484 if(this->print_halts) {
[f2b18d01]485 __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl());
[c34ebf2]486 }
487 #endif
[6b4cdd3]488}
489
[dbe9b08]490//=============================================================================================
491// Unexpected Terminating logic
492//=============================================================================================
[ea7d2b0]493static __spinlock_t kernel_abort_lock;
[9d944b2]494static bool kernel_abort_called = false;
495
[afd550c]496void * kernel_abort(void) __attribute__ ((__nothrow__)) {
[9d944b2]497 // abort cannot be recursively entered by the same or different processors because all signal handlers return when
498 // the globalAbort flag is true.
[36982fc]499 lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
[9d944b2]500
501 // first task to abort ?
[de94a60]502 if ( kernel_abort_called ) { // not first task to abort ?
[ea7d2b0]503 unlock( kernel_abort_lock );
[1c273d0]504
[9d944b2]505 sigset_t mask;
506 sigemptyset( &mask );
[de94a60]507 sigaddset( &mask, SIGALRM ); // block SIGALRM signals
[8a13c47]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
[de94a60]511 }
512 else {
513 kernel_abort_called = true;
514 unlock( kernel_abort_lock );
[9d944b2]515 }
516
[14a61b5]517 return kernelTLS.this_thread;
[9d944b2]518}
519
520void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
[b81fd95]521 $thread * thrd = ( $thread * ) kernel_data;
[9d944b2]522
[de94a60]523 if(thrd) {
524 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
[1c40091]525 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
[de94a60]526
[212c2187]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 );
[1c40091]529 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
[de94a60]530 }
531 else {
[1c40091]532 __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
[de94a60]533 }
[1c273d0]534 }
[9d944b2]535 else {
[de94a60]536 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
[1c40091]537 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
[9d944b2]538 }
539}
540
[2b8bc41]541int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
[14a61b5]542 return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
[2b8bc41]543}
544
[de94a60]545static __spinlock_t kernel_debug_lock;
546
[9d944b2]547extern "C" {
[1c40091]548 void __cfaabi_bits_acquire() {
[36982fc]549 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
[9d944b2]550 }
551
[1c40091]552 void __cfaabi_bits_release() {
[ea7d2b0]553 unlock( kernel_debug_lock );
[9d944b2]554 }
[8118303]555}
556
[fa21ac9]557//=============================================================================================
558// Kernel Utilities
559//=============================================================================================
[bd98b58]560//-----------------------------------------------------------------------------
561// Locks
[242a902]562void ?{}( semaphore & this, int count = 1 ) {
563 (this.lock){};
564 this.count = count;
565 (this.waiting){};
[db6f06a]566}
[242a902]567void ^?{}(semaphore & this) {}
[db6f06a]568
[71c8b7e]569bool P(semaphore & this) with( this ){
[65deb18]570 lock( lock __cfaabi_dbg_ctx2 );
571 count -= 1;
572 if ( count < 0 ) {
[bdeba0b]573 // queue current task
[14a61b5]574 append( waiting, kernelTLS.this_thread );
[bdeba0b]575
576 // atomically release spin lock and block
[3381ed7]577 unlock( lock );
[ae66348]578 park( __cfaabi_dbg_ctx );
[71c8b7e]579 return true;
[8def349]580 }
[4e6fb8e]581 else {
[65deb18]582 unlock( lock );
[71c8b7e]583 return false;
[4e6fb8e]584 }
[bd98b58]585}
586
[f0ce5f4]587bool V(semaphore & this) with( this ) {
[ac2b598]588 $thread * thrd = 0p;
[65deb18]589 lock( lock __cfaabi_dbg_ctx2 );
590 count += 1;
591 if ( count <= 0 ) {
[bdeba0b]592 // remove task at head of waiting list
[65deb18]593 thrd = pop_head( waiting );
[bd98b58]594 }
[bdeba0b]595
[65deb18]596 unlock( lock );
[bdeba0b]597
598 // make new owner
[ae66348]599 unpark( thrd __cfaabi_dbg_ctx2 );
[f0ce5f4]600
[d384787]601 return thrd != 0p;
602}
603
604bool 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
[f0ce5f4]615 return thrd != 0p;
[bd98b58]616}
617
[de94a60]618//-----------------------------------------------------------------------------
619// Debug
620__cfaabi_dbg_debug_do(
[1997b4e]621 extern "C" {
[ae66348]622 void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
[1997b4e]623 this.prev_name = prev_name;
624 this.prev_thrd = kernelTLS.this_thread;
625 }
[ae66348]626
627 void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) {
628 if(park) {
[f0ce5f4]629 this.park_caller = prev_name;
630 this.park_stale = false;
[ae66348]631 }
632 else {
[f0ce5f4]633 this.unpark_caller = prev_name;
634 this.unpark_stale = false;
[ae66348]635 }
636 }
[9181f1d]637 }
[f7d6bb0]638)
[2026bb6]639
640//-----------------------------------------------------------------------------
641// Debug
[8c50aed]642bool threading_enabled(void) __attribute__((const)) {
[2026bb6]643 return true;
644}
[c34ebf2]645
646//-----------------------------------------------------------------------------
647// Statistics
648#if !defined(__CFA_NO_STATISTICS__)
649 void print_halts( processor & this ) {
650 this.print_halts = true;
651 }
652#endif
[8118303]653// Local Variables: //
654// mode: c //
655// tab-width: 4 //
656// End: //
Note: See TracBrowser for help on using the repository browser.