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

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e8e457e was e8e457e, checked in by tdelisle <tdelisle@…>, 5 years ago

Thread context is now distinct from coroutine context

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