source: libcfa/src/concurrency/kernel.cfa @ 69a61d2

arm-ehcleanup-dtorsjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-expr
Last change on this file since 69a61d2 was 69a61d2, checked in by tdelisle <tdelisle@…>, 3 years ago

coroutine and thread no longer store stack size

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