source: libcfa/src/concurrency/kernel.cfa@ 5c1a531

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 5c1a531 was 5c1a531, checked in by tdelisle <tdelisle@…>, 6 years ago

Fixed errno virtualization and enabled preemption during coroutine context switch

  • Property mode set to 100644
File size: 24.4 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// 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 : Mon Apr 9 16:11:46 2018
13// Update Count : 24
14//
15
16//C Includes
17#include <stddef.h>
18#include <errno.h>
19#include <string.h>
20extern "C" {
21#include <stdio.h>
22#include <fenv.h>
23#include <sys/resource.h>
24#include <signal.h>
25#include <unistd.h>
26}
27
28//CFA Includes
29#include "time.hfa"
30#include "kernel_private.hfa"
31#include "preemption.hfa"
32#include "startup.hfa"
33
34//Private includes
35#define __CFA_INVOKE_PRIVATE__
36#include "invoke.h"
37
38//Start and stop routine for the kernel, declared first to make sure they run first
39static void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
40static void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
41
42//-----------------------------------------------------------------------------
43// Kernel storage
44KERNEL_STORAGE(cluster, mainCluster);
45KERNEL_STORAGE(processor, mainProcessor);
46KERNEL_STORAGE(thread_desc, mainThread);
47KERNEL_STORAGE(__stack_t, mainThreadCtx);
48
49cluster * mainCluster;
50processor * mainProcessor;
51thread_desc * mainThread;
52
53extern "C" {
54struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters;
55}
56
57size_t __page_size = 0;
58
59//-----------------------------------------------------------------------------
60// Global state
61thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = {
62 NULL,
63 NULL,
64 { 1, false, false }
65};
66
67//-----------------------------------------------------------------------------
68// Struct to steal stack
69struct current_stack_info_t {
70 __stack_t * storage; // pointer to stack object
71 void *base; // base of stack
72 void *limit; // stack grows towards stack limit
73 void *context; // address of cfa_context_t
74};
75
76void ?{}( current_stack_info_t & this ) {
77 __stack_context_t ctx;
78 CtxGet( ctx );
79 this.base = ctx.FP;
80
81 rlimit r;
82 getrlimit( RLIMIT_STACK, &r);
83 size_t size = r.rlim_cur;
84
85 this.limit = (void *)(((intptr_t)this.base) - size);
86 this.context = &storage_mainThreadCtx;
87}
88
89//-----------------------------------------------------------------------------
90// Main thread construction
91
92void ?{}( coroutine_desc & this, current_stack_info_t * info) with( this ) {
93 stack.storage = info->storage;
94 with(*stack.storage) {
95 limit = info->limit;
96 base = info->base;
97 }
98 *((intptr_t*)&stack.storage) |= 0x1;
99 name = "Main Thread";
100 state = Start;
101 starter = NULL;
102 last = NULL;
103 cancellation = NULL;
104}
105
106void ?{}( thread_desc & this, current_stack_info_t * info) with( this ) {
107 state = Start;
108 self_cor{ info };
109 curr_cor = &self_cor;
110 curr_cluster = mainCluster;
111 self_mon.owner = &this;
112 self_mon.recursion = 1;
113 self_mon_p = &self_mon;
114 next = NULL;
115
116 node.next = NULL;
117 node.prev = NULL;
118 doregister(curr_cluster, this);
119
120 monitors{ &self_mon_p, 1, (fptr_t)0 };
121}
122
123//-----------------------------------------------------------------------------
124// Processor coroutine
125void ?{}(processorCtx_t & this) {
126
127}
128
129// Construct the processor context of non-main processors
130static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
131 (this.__cor){ info };
132 this.proc = proc;
133}
134
135static void start(processor * this);
136void ?{}(processor & this, const char * name, cluster & cltr) with( this ) {
137 this.name = name;
138 this.cltr = &cltr;
139 terminated{ 0 };
140 do_terminate = false;
141 preemption_alarm = NULL;
142 pending_preemption = false;
143 runner.proc = &this;
144
145 idleLock{};
146
147 start( &this );
148}
149
150void ^?{}(processor & this) with( this ){
151 if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
152 __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
153
154 __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
155 wake( &this );
156
157 P( terminated );
158 verify( kernelTLS.this_processor != &this);
159 }
160
161 pthread_join( kernel_thread, NULL );
162}
163
164void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
165 this.name = name;
166 this.preemption_rate = preemption_rate;
167 ready_queue{};
168 ready_queue_lock{};
169
170 procs{ __get };
171 idles{ __get };
172 threads{ __get };
173
174 doregister(this);
175}
176
177void ^?{}(cluster & this) {
178 unregister(this);
179}
180
181//=============================================================================================
182// Kernel Scheduling logic
183//=============================================================================================
184static void runThread(processor * this, thread_desc * dst);
185static void finishRunning(processor * this);
186static void halt(processor * this);
187
188//Main of the processor contexts
189void main(processorCtx_t & runner) {
190 processor * this = runner.proc;
191 verify(this);
192
193 __cfaabi_dbg_print_safe("Kernel : core %p starting\n", this);
194
195 doregister(this->cltr, this);
196
197 {
198 // Setup preemption data
199 preemption_scope scope = { this };
200
201 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
202
203 thread_desc * readyThread = NULL;
204 for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ )
205 {
206 readyThread = nextThread( this->cltr );
207
208 if(readyThread)
209 {
210 verify( ! kernelTLS.preemption_state.enabled );
211
212 runThread(this, readyThread);
213
214 verify( ! kernelTLS.preemption_state.enabled );
215
216 //Some actions need to be taken from the kernel
217 finishRunning(this);
218
219 spin_count = 0;
220 }
221 else
222 {
223 // spin(this, &spin_count);
224 halt(this);
225 }
226 }
227
228 __cfaabi_dbg_print_safe("Kernel : core %p stopping\n", this);
229 }
230
231 unregister(this->cltr, this);
232
233 V( this->terminated );
234
235 __cfaabi_dbg_print_safe("Kernel : core %p terminated\n", this);
236}
237
238static int * __volatile_errno() __attribute__((noinline));
239static int * __volatile_errno() { asm(""); return &errno; }
240
241// KERNEL ONLY
242// runThread runs a thread by context switching
243// from the processor coroutine to the target thread
244static void runThread(processor * this, thread_desc * thrd_dst) {
245 coroutine_desc * proc_cor = get_coroutine(this->runner);
246
247 // Reset the terminating actions here
248 this->finish.action_code = No_Action;
249
250 // Update global state
251 kernelTLS.this_thread = thrd_dst;
252
253 // set state of processor coroutine to inactive and the thread to active
254 proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
255 thrd_dst->state = Active;
256
257 // set context switch to the thread that the processor is executing
258 verify( thrd_dst->context.SP );
259 CtxSwitch( &proc_cor->context, &thrd_dst->context );
260 // when CtxSwitch returns we are back in the processor coroutine
261
262 // set state of processor coroutine to active and the thread to inactive
263 thrd_dst->state = thrd_dst->state == Halted ? Halted : Inactive;
264 proc_cor->state = Active;
265}
266
267// KERNEL_ONLY
268static void returnToKernel() {
269 coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
270 thread_desc * thrd_src = kernelTLS.this_thread;
271
272 // set state of current coroutine to inactive
273 thrd_src->state = thrd_src->state == Halted ? Halted : Inactive;
274 proc_cor->state = Active;
275 int local_errno = *__volatile_errno();
276
277 // set new coroutine that the processor is executing
278 // and context switch to it
279 verify( proc_cor->context.SP );
280 CtxSwitch( &thrd_src->context, &proc_cor->context );
281
282 // set state of new coroutine to active
283 proc_cor->state = proc_cor->state == Halted ? Halted : Inactive;
284 thrd_src->state = Active;
285 *__volatile_errno() = local_errno;
286}
287
288// KERNEL_ONLY
289// Once a thread has finished running, some of
290// its final actions must be executed from the kernel
291static void finishRunning(processor * this) with( this->finish ) {
292 verify( ! kernelTLS.preemption_state.enabled );
293 choose( action_code ) {
294 case No_Action:
295 break;
296 case Release:
297 unlock( *lock );
298 case Schedule:
299 ScheduleThread( thrd );
300 case Release_Schedule:
301 unlock( *lock );
302 ScheduleThread( thrd );
303 case Release_Multi:
304 for(int i = 0; i < lock_count; i++) {
305 unlock( *locks[i] );
306 }
307 case Release_Multi_Schedule:
308 for(int i = 0; i < lock_count; i++) {
309 unlock( *locks[i] );
310 }
311 for(int i = 0; i < thrd_count; i++) {
312 ScheduleThread( thrds[i] );
313 }
314 case Callback:
315 callback();
316 default:
317 abort("KERNEL ERROR: Unexpected action to run after thread");
318 }
319}
320
321// KERNEL_ONLY
322// Context invoker for processors
323// This is the entry point for processors (kernel threads)
324// It effectively constructs a coroutine by stealing the pthread stack
325static void * CtxInvokeProcessor(void * arg) {
326 processor * proc = (processor *) arg;
327 kernelTLS.this_processor = proc;
328 kernelTLS.this_thread = NULL;
329 kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
330 // SKULLDUGGERY: We want to create a context for the processor coroutine
331 // which is needed for the 2-step context switch. However, there is no reason
332 // to waste the perfectly valid stack create by pthread.
333 current_stack_info_t info;
334 __stack_t ctx;
335 info.storage = &ctx;
336 (proc->runner){ proc, &info };
337
338 __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage);
339
340 //Set global state
341 kernelTLS.this_thread = NULL;
342
343 //We now have a proper context from which to schedule threads
344 __cfaabi_dbg_print_safe("Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx);
345
346 // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
347 // resume it to start it like it normally would, it will just context switch
348 // back to here. Instead directly call the main since we already are on the
349 // appropriate stack.
350 get_coroutine(proc->runner)->state = Active;
351 main( proc->runner );
352 get_coroutine(proc->runner)->state = Halted;
353
354 // Main routine of the core returned, the core is now fully terminated
355 __cfaabi_dbg_print_safe("Kernel : core %p main ended (%p)\n", proc, &proc->runner);
356
357 return NULL;
358}
359
360static void start(processor * this) {
361 __cfaabi_dbg_print_safe("Kernel : Starting core %p\n", this);
362
363 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
364
365 __cfaabi_dbg_print_safe("Kernel : core %p started\n", this);
366}
367
368// KERNEL_ONLY
369void kernel_first_resume( processor * this ) {
370 thread_desc * src = mainThread;
371 coroutine_desc * dst = get_coroutine(this->runner);
372
373 verify( ! kernelTLS.preemption_state.enabled );
374
375 __stack_prepare( &dst->stack, 65000 );
376 CtxStart(&this->runner, CtxInvokeCoroutine);
377
378 verify( ! kernelTLS.preemption_state.enabled );
379
380 dst->last = &src->self_cor;
381 dst->starter = dst->starter ? dst->starter : &src->self_cor;
382
383 // set state of current coroutine to inactive
384 src->state = src->state == Halted ? Halted : Inactive;
385
386 // context switch to specified coroutine
387 verify( dst->context.SP );
388 CtxSwitch( &src->context, &dst->context );
389 // when CtxSwitch returns we are back in the src coroutine
390
391 // set state of new coroutine to active
392 src->state = Active;
393
394 verify( ! kernelTLS.preemption_state.enabled );
395}
396
397// KERNEL_ONLY
398void kernel_last_resume( processor * this ) {
399 coroutine_desc * src = &mainThread->self_cor;
400 coroutine_desc * dst = get_coroutine(this->runner);
401
402 verify( ! kernelTLS.preemption_state.enabled );
403 verify( dst->starter == src );
404 verify( dst->context.SP );
405
406 // context switch to the processor
407 CtxSwitch( &src->context, &dst->context );
408}
409
410//-----------------------------------------------------------------------------
411// Scheduler routines
412
413// KERNEL ONLY
414void ScheduleThread( thread_desc * thrd ) {
415 verify( thrd );
416 verify( thrd->state != Halted );
417
418 verify( ! kernelTLS.preemption_state.enabled );
419
420 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
421
422 with( *thrd->curr_cluster ) {
423 lock ( ready_queue_lock __cfaabi_dbg_ctx2 );
424 bool was_empty = !(ready_queue != 0);
425 append( ready_queue, thrd );
426 unlock( ready_queue_lock );
427
428 if(was_empty) {
429 lock (proc_list_lock __cfaabi_dbg_ctx2);
430 if(idles) {
431 wake_fast(idles.head);
432 }
433 unlock (proc_list_lock);
434 }
435 else if( struct processor * idle = idles.head ) {
436 wake_fast(idle);
437 }
438
439 }
440
441 verify( ! kernelTLS.preemption_state.enabled );
442}
443
444// KERNEL ONLY
445thread_desc * nextThread(cluster * this) with( *this ) {
446 verify( ! kernelTLS.preemption_state.enabled );
447 lock( ready_queue_lock __cfaabi_dbg_ctx2 );
448 thread_desc * head = pop_head( ready_queue );
449 unlock( ready_queue_lock );
450 verify( ! kernelTLS.preemption_state.enabled );
451 return head;
452}
453
454void BlockInternal() {
455 disable_interrupts();
456 verify( ! kernelTLS.preemption_state.enabled );
457 returnToKernel();
458 verify( ! kernelTLS.preemption_state.enabled );
459 enable_interrupts( __cfaabi_dbg_ctx );
460}
461
462void BlockInternal( __spinlock_t * lock ) {
463 disable_interrupts();
464 with( *kernelTLS.this_processor ) {
465 finish.action_code = Release;
466 finish.lock = lock;
467 }
468
469 verify( ! kernelTLS.preemption_state.enabled );
470 returnToKernel();
471 verify( ! kernelTLS.preemption_state.enabled );
472
473 enable_interrupts( __cfaabi_dbg_ctx );
474}
475
476void BlockInternal( thread_desc * thrd ) {
477 disable_interrupts();
478 with( * kernelTLS.this_processor ) {
479 finish.action_code = Schedule;
480 finish.thrd = thrd;
481 }
482
483 verify( ! kernelTLS.preemption_state.enabled );
484 returnToKernel();
485 verify( ! kernelTLS.preemption_state.enabled );
486
487 enable_interrupts( __cfaabi_dbg_ctx );
488}
489
490void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
491 assert(thrd);
492 disable_interrupts();
493 with( * kernelTLS.this_processor ) {
494 finish.action_code = Release_Schedule;
495 finish.lock = lock;
496 finish.thrd = thrd;
497 }
498
499 verify( ! kernelTLS.preemption_state.enabled );
500 returnToKernel();
501 verify( ! kernelTLS.preemption_state.enabled );
502
503 enable_interrupts( __cfaabi_dbg_ctx );
504}
505
506void BlockInternal(__spinlock_t * locks [], unsigned short count) {
507 disable_interrupts();
508 with( * kernelTLS.this_processor ) {
509 finish.action_code = Release_Multi;
510 finish.locks = locks;
511 finish.lock_count = count;
512 }
513
514 verify( ! kernelTLS.preemption_state.enabled );
515 returnToKernel();
516 verify( ! kernelTLS.preemption_state.enabled );
517
518 enable_interrupts( __cfaabi_dbg_ctx );
519}
520
521void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
522 disable_interrupts();
523 with( *kernelTLS.this_processor ) {
524 finish.action_code = Release_Multi_Schedule;
525 finish.locks = locks;
526 finish.lock_count = lock_count;
527 finish.thrds = thrds;
528 finish.thrd_count = thrd_count;
529 }
530
531 verify( ! kernelTLS.preemption_state.enabled );
532 returnToKernel();
533 verify( ! kernelTLS.preemption_state.enabled );
534
535 enable_interrupts( __cfaabi_dbg_ctx );
536}
537
538void BlockInternal(__finish_callback_fptr_t callback) {
539 disable_interrupts();
540 with( *kernelTLS.this_processor ) {
541 finish.action_code = Callback;
542 finish.callback = callback;
543 }
544
545 verify( ! kernelTLS.preemption_state.enabled );
546 returnToKernel();
547 verify( ! kernelTLS.preemption_state.enabled );
548
549 enable_interrupts( __cfaabi_dbg_ctx );
550}
551
552// KERNEL ONLY
553void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
554 verify( ! kernelTLS.preemption_state.enabled );
555 with( * kernelTLS.this_processor ) {
556 finish.action_code = thrd ? Release_Schedule : Release;
557 finish.lock = lock;
558 finish.thrd = thrd;
559 }
560
561 returnToKernel();
562}
563
564//=============================================================================================
565// Kernel Setup logic
566//=============================================================================================
567//-----------------------------------------------------------------------------
568// Kernel boot procedures
569static void kernel_startup(void) {
570 verify( ! kernelTLS.preemption_state.enabled );
571 __cfaabi_dbg_print_safe("Kernel : Starting\n");
572
573 __page_size = sysconf( _SC_PAGESIZE );
574
575 __cfa_dbg_global_clusters.list{ __get };
576 __cfa_dbg_global_clusters.lock{};
577
578 // Initialize the main cluster
579 mainCluster = (cluster *)&storage_mainCluster;
580 (*mainCluster){"Main Cluster"};
581
582 __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
583
584 // Start by initializing the main thread
585 // SKULLDUGGERY: the mainThread steals the process main thread
586 // which will then be scheduled by the mainProcessor normally
587 mainThread = (thread_desc *)&storage_mainThread;
588 current_stack_info_t info;
589 info.storage = (__stack_t*)&storage_mainThreadCtx;
590 (*mainThread){ &info };
591
592 __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
593
594
595
596 // Construct the processor context of the main processor
597 void ?{}(processorCtx_t & this, processor * proc) {
598 (this.__cor){ "Processor" };
599 this.__cor.starter = NULL;
600 this.proc = proc;
601 }
602
603 void ?{}(processor & this) with( this ) {
604 name = "Main Processor";
605 cltr = mainCluster;
606 terminated{ 0 };
607 do_terminate = false;
608 preemption_alarm = NULL;
609 pending_preemption = false;
610 kernel_thread = pthread_self();
611
612 runner{ &this };
613 __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
614 }
615
616 // Initialize the main processor and the main processor ctx
617 // (the coroutine that contains the processing control flow)
618 mainProcessor = (processor *)&storage_mainProcessor;
619 (*mainProcessor){};
620
621 //initialize the global state variables
622 kernelTLS.this_processor = mainProcessor;
623 kernelTLS.this_thread = mainThread;
624
625 // Enable preemption
626 kernel_start_preemption();
627
628 // Add the main thread to the ready queue
629 // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
630 ScheduleThread(mainThread);
631
632 // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
633 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
634 // mainThread is on the ready queue when this call is made.
635 kernel_first_resume( kernelTLS.this_processor );
636
637
638
639 // THE SYSTEM IS NOW COMPLETELY RUNNING
640 __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
641
642 verify( ! kernelTLS.preemption_state.enabled );
643 enable_interrupts( __cfaabi_dbg_ctx );
644 verify( TL_GET( preemption_state.enabled ) );
645}
646
647static void kernel_shutdown(void) {
648 __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
649
650 verify( TL_GET( preemption_state.enabled ) );
651 disable_interrupts();
652 verify( ! kernelTLS.preemption_state.enabled );
653
654 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
655 // When its coroutine terminates, it return control to the mainThread
656 // which is currently here
657 __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
658 kernel_last_resume( kernelTLS.this_processor );
659 mainThread->self_cor.state = Halted;
660
661 // THE SYSTEM IS NOW COMPLETELY STOPPED
662
663 // Disable preemption
664 kernel_stop_preemption();
665
666 // Destroy the main processor and its context in reverse order of construction
667 // These were manually constructed so we need manually destroy them
668 ^(mainProcessor->runner){};
669 ^(mainProcessor){};
670
671 // Final step, destroy the main thread since it is no longer needed
672 // Since we provided a stack to this taxk it will not destroy anything
673 ^(mainThread){};
674
675 ^(__cfa_dbg_global_clusters.list){};
676 ^(__cfa_dbg_global_clusters.lock){};
677
678 __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
679}
680
681//=============================================================================================
682// Kernel Quiescing
683//=============================================================================================
684static void halt(processor * this) with( *this ) {
685 // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
686
687 with( *cltr ) {
688 lock (proc_list_lock __cfaabi_dbg_ctx2);
689 remove (procs, *this);
690 push_front(idles, *this);
691 unlock (proc_list_lock);
692 }
693
694 __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
695
696 wait( idleLock );
697
698 __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
699
700 with( *cltr ) {
701 lock (proc_list_lock __cfaabi_dbg_ctx2);
702 remove (idles, *this);
703 push_front(procs, *this);
704 unlock (proc_list_lock);
705 }
706}
707
708//=============================================================================================
709// Unexpected Terminating logic
710//=============================================================================================
711static __spinlock_t kernel_abort_lock;
712static bool kernel_abort_called = false;
713
714void * kernel_abort(void) __attribute__ ((__nothrow__)) {
715 // abort cannot be recursively entered by the same or different processors because all signal handlers return when
716 // the globalAbort flag is true.
717 lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
718
719 // first task to abort ?
720 if ( kernel_abort_called ) { // not first task to abort ?
721 unlock( kernel_abort_lock );
722
723 sigset_t mask;
724 sigemptyset( &mask );
725 sigaddset( &mask, SIGALRM ); // block SIGALRM signals
726 sigsuspend( &mask ); // block the processor to prevent further damage during abort
727 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
728 }
729 else {
730 kernel_abort_called = true;
731 unlock( kernel_abort_lock );
732 }
733
734 return kernelTLS.this_thread;
735}
736
737void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
738 thread_desc * thrd = kernel_data;
739
740 if(thrd) {
741 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
742 __cfaabi_dbg_bits_write( abort_text, len );
743
744 if ( &thrd->self_cor != thrd->curr_cor ) {
745 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
746 __cfaabi_dbg_bits_write( abort_text, len );
747 }
748 else {
749 __cfaabi_dbg_bits_write( ".\n", 2 );
750 }
751 }
752 else {
753 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
754 __cfaabi_dbg_bits_write( abort_text, len );
755 }
756}
757
758int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
759 return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
760}
761
762static __spinlock_t kernel_debug_lock;
763
764extern "C" {
765 void __cfaabi_dbg_bits_acquire() {
766 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
767 }
768
769 void __cfaabi_dbg_bits_release() {
770 unlock( kernel_debug_lock );
771 }
772}
773
774//=============================================================================================
775// Kernel Utilities
776//=============================================================================================
777//-----------------------------------------------------------------------------
778// Locks
779void ?{}( semaphore & this, int count = 1 ) {
780 (this.lock){};
781 this.count = count;
782 (this.waiting){};
783}
784void ^?{}(semaphore & this) {}
785
786void P(semaphore & this) with( this ){
787 lock( lock __cfaabi_dbg_ctx2 );
788 count -= 1;
789 if ( count < 0 ) {
790 // queue current task
791 append( waiting, kernelTLS.this_thread );
792
793 // atomically release spin lock and block
794 BlockInternal( &lock );
795 }
796 else {
797 unlock( lock );
798 }
799}
800
801void V(semaphore & this) with( this ) {
802 thread_desc * thrd = NULL;
803 lock( lock __cfaabi_dbg_ctx2 );
804 count += 1;
805 if ( count <= 0 ) {
806 // remove task at head of waiting list
807 thrd = pop_head( waiting );
808 }
809
810 unlock( lock );
811
812 // make new owner
813 WakeThread( thrd );
814}
815
816//-----------------------------------------------------------------------------
817// Global Queues
818void doregister( cluster & cltr ) {
819 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
820 push_front( __cfa_dbg_global_clusters.list, cltr );
821 unlock ( __cfa_dbg_global_clusters.lock );
822}
823
824void unregister( cluster & cltr ) {
825 lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
826 remove( __cfa_dbg_global_clusters.list, cltr );
827 unlock( __cfa_dbg_global_clusters.lock );
828}
829
830void doregister( cluster * cltr, thread_desc & thrd ) {
831 lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
832 push_front(cltr->threads, thrd);
833 unlock (cltr->thread_list_lock);
834}
835
836void unregister( cluster * cltr, thread_desc & thrd ) {
837 lock (cltr->thread_list_lock __cfaabi_dbg_ctx2);
838 remove(cltr->threads, thrd );
839 unlock(cltr->thread_list_lock);
840}
841
842void doregister( cluster * cltr, processor * proc ) {
843 lock (cltr->proc_list_lock __cfaabi_dbg_ctx2);
844 push_front(cltr->procs, *proc);
845 unlock (cltr->proc_list_lock);
846}
847
848void unregister( cluster * cltr, processor * proc ) {
849 lock (cltr->proc_list_lock __cfaabi_dbg_ctx2);
850 remove(cltr->procs, *proc );
851 unlock(cltr->proc_list_lock);
852}
853
854//-----------------------------------------------------------------------------
855// Debug
856__cfaabi_dbg_debug_do(
857 extern "C" {
858 void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
859 this.prev_name = prev_name;
860 this.prev_thrd = kernelTLS.this_thread;
861 }
862 }
863)
864// Local Variables: //
865// mode: c //
866// tab-width: 4 //
867// End: //
Note: See TracBrowser for help on using the repository browser.