source: libcfa/src/concurrency/kernel.cfa @ 8c01e1b

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

UserStack? flag on coroutines is now folded into the storage pointer

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