source: libcfa/src/concurrency/kernel.cfa @ 7a8f5246

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

Use initial exec for thread local storage

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