source: src/libcfa/concurrency/kernel.c @ 85b1deb

aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerwith_gc
Last change on this file since 85b1deb was 85b1deb, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Fix processor halting

  • Property mode set to 100644
File size: 23.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// kernel.c --
8//
9// Author           : Thierry Delisle
10// Created On       : Tue Jan 17 12:27:26 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Mon Apr  9 16:11:46 2018
13// Update Count     : 24
14//
15
16//C Includes
17#include <stddef.h>
18#include <errno.h>
19#include <string.h>
20extern "C" {
21#include <stdio.h>
22#include <fenv.h>
23#include <sys/resource.h>
24#include <signal.h>
25#include <unistd.h>
26}
27
28//CFA Includes
29#include "time"
30#include "kernel_private.h"
31#include "preemption.h"
32#include "startup.h"
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
39void kernel_startup(void)  __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
40void 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 = {
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
135void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) {
136        (this.__cor){ info };
137        this.proc = proc;
138}
139
140void ?{}(processor & this, const char * name, cluster & cltr) with( this ) {
141        this.name = name;
142        this.cltr = &cltr;
143        terminated{ 0 };
144        do_terminate = false;
145        preemption_alarm = NULL;
146        pending_preemption = false;
147        runner.proc = &this;
148
149        idleLock{};
150
151        start( &this );
152}
153
154void ^?{}(processor & this) with( this ){
155        if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) {
156                __cfaabi_dbg_print_safe("Kernel : core %p signaling termination\n", &this);
157
158                __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED);
159                wake( &this );
160
161                P( terminated );
162                verify( kernelTLS.this_processor != &this);
163        }
164
165        pthread_join( kernel_thread, NULL );
166}
167
168void ?{}(cluster & this, const char * name, Duration preemption_rate) with( this ) {
169        this.name = name;
170        this.preemption_rate = preemption_rate;
171        ready_queue{};
172        ready_queue_lock{};
173
174        procs{ __get };
175        idles{ __get };
176        threads{ __get };
177
178        doregister(this);
179}
180
181void ^?{}(cluster & this) {
182        unregister(this);
183}
184
185//=============================================================================================
186// Kernel Scheduling logic
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
241void 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
258void returnToKernel() {
259        coroutine_desc * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
260        coroutine_desc * thrd_cor = kernelTLS.this_thread->curr_cor = kernelTLS.this_coroutine;
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
267void 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
301void * CtxInvokeProcessor(void * arg) {
302        processor * proc = (processor *) arg;
303        kernelTLS.this_processor = proc;
304        kernelTLS.this_coroutine = NULL;
305        kernelTLS.this_thread    = NULL;
306        kernelTLS.preemption_state.[enabled, disable_count] = [false, 1];
307        // SKULLDUGGERY: We want to create a context for the processor coroutine
308        // which is needed for the 2-step context switch. However, there is no reason
309        // to waste the perfectly valid stack create by pthread.
310        current_stack_info_t info;
311        machine_context_t ctx;
312        info.context = &ctx;
313        (proc->runner){ proc, &info };
314
315        __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.base);
316
317        //Set global state
318        kernelTLS.this_coroutine = get_coroutine(proc->runner);
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
338void 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 = kernelTLS.this_coroutine;
349        coroutine_desc * dst = get_coroutine(this->runner);
350
351        verify( ! kernelTLS.preemption_state.enabled );
352
353        create_stack(&dst->stack, dst->stack.size);
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        // set new coroutine that task is executing
365        kernelTLS.this_coroutine = dst;
366
367        // SKULLDUGGERY normally interrupts are enable before leaving a coroutine ctxswitch.
368        // Therefore, when first creating a coroutine, interrupts are enable before calling the main.
369        // This is consistent with thread creation. However, when creating the main processor coroutine,
370        // we wan't interrupts to be disabled. Therefore, we double-disable interrupts here so they will
371        // stay disabled.
372        disable_interrupts();
373
374        // context switch to specified coroutine
375        assert( src->stack.context );
376        CtxSwitch( src->stack.context, dst->stack.context );
377        // when CtxSwitch returns we are back in the src coroutine
378
379        // set state of new coroutine to active
380        src->state = Active;
381
382        verify( ! kernelTLS.preemption_state.enabled );
383}
384
385//-----------------------------------------------------------------------------
386// Scheduler routines
387
388// KERNEL ONLY
389void ScheduleThread( thread_desc * thrd ) {
390        verify( thrd );
391        verify( thrd->self_cor.state != Halted );
392
393        verify( ! kernelTLS.preemption_state.enabled );
394
395        verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
396
397        with( *thrd->curr_cluster ) {
398                lock  ( ready_queue_lock __cfaabi_dbg_ctx2 );
399                bool was_empty = !(ready_queue != 0);
400                append( ready_queue, thrd );
401                unlock( ready_queue_lock );
402
403                if(was_empty) {
404                        lock      (proc_list_lock __cfaabi_dbg_ctx2);
405                        if(idles) {
406                                wake_fast(idles.head);
407                        }
408                        unlock    (proc_list_lock);
409                }
410                else if( struct processor * idle = idles.head ) {
411                        wake_fast(idle);
412                }
413
414        }
415
416        verify( ! kernelTLS.preemption_state.enabled );
417}
418
419// KERNEL ONLY
420thread_desc * nextThread(cluster * this) with( *this ) {
421        verify( ! kernelTLS.preemption_state.enabled );
422        lock( ready_queue_lock __cfaabi_dbg_ctx2 );
423        thread_desc * head = pop_head( ready_queue );
424        unlock( ready_queue_lock );
425        verify( ! kernelTLS.preemption_state.enabled );
426        return head;
427}
428
429void BlockInternal() {
430        disable_interrupts();
431        verify( ! kernelTLS.preemption_state.enabled );
432        returnToKernel();
433        verify( ! kernelTLS.preemption_state.enabled );
434        enable_interrupts( __cfaabi_dbg_ctx );
435}
436
437void BlockInternal( __spinlock_t * lock ) {
438        disable_interrupts();
439        with( *kernelTLS.this_processor ) {
440                finish.action_code = Release;
441                finish.lock        = lock;
442        }
443
444        verify( ! kernelTLS.preemption_state.enabled );
445        returnToKernel();
446        verify( ! kernelTLS.preemption_state.enabled );
447
448        enable_interrupts( __cfaabi_dbg_ctx );
449}
450
451void BlockInternal( thread_desc * thrd ) {
452        disable_interrupts();
453        with( * kernelTLS.this_processor ) {
454                finish.action_code = Schedule;
455                finish.thrd        = thrd;
456        }
457
458        verify( ! kernelTLS.preemption_state.enabled );
459        returnToKernel();
460        verify( ! kernelTLS.preemption_state.enabled );
461
462        enable_interrupts( __cfaabi_dbg_ctx );
463}
464
465void BlockInternal( __spinlock_t * lock, thread_desc * thrd ) {
466        assert(thrd);
467        disable_interrupts();
468        with( * kernelTLS.this_processor ) {
469                finish.action_code = Release_Schedule;
470                finish.lock        = lock;
471                finish.thrd        = thrd;
472        }
473
474        verify( ! kernelTLS.preemption_state.enabled );
475        returnToKernel();
476        verify( ! kernelTLS.preemption_state.enabled );
477
478        enable_interrupts( __cfaabi_dbg_ctx );
479}
480
481void BlockInternal(__spinlock_t * locks [], unsigned short count) {
482        disable_interrupts();
483        with( * kernelTLS.this_processor ) {
484                finish.action_code = Release_Multi;
485                finish.locks       = locks;
486                finish.lock_count  = count;
487        }
488
489        verify( ! kernelTLS.preemption_state.enabled );
490        returnToKernel();
491        verify( ! kernelTLS.preemption_state.enabled );
492
493        enable_interrupts( __cfaabi_dbg_ctx );
494}
495
496void BlockInternal(__spinlock_t * locks [], unsigned short lock_count, thread_desc * thrds [], unsigned short thrd_count) {
497        disable_interrupts();
498        with( *kernelTLS.this_processor ) {
499                finish.action_code = Release_Multi_Schedule;
500                finish.locks       = locks;
501                finish.lock_count  = lock_count;
502                finish.thrds       = thrds;
503                finish.thrd_count  = thrd_count;
504        }
505
506        verify( ! kernelTLS.preemption_state.enabled );
507        returnToKernel();
508        verify( ! kernelTLS.preemption_state.enabled );
509
510        enable_interrupts( __cfaabi_dbg_ctx );
511}
512
513void BlockInternal(__finish_callback_fptr_t callback) {
514        disable_interrupts();
515        with( *kernelTLS.this_processor ) {
516                finish.action_code = Callback;
517                finish.callback    = callback;
518        }
519
520        verify( ! kernelTLS.preemption_state.enabled );
521        returnToKernel();
522        verify( ! kernelTLS.preemption_state.enabled );
523
524        enable_interrupts( __cfaabi_dbg_ctx );
525}
526
527// KERNEL ONLY
528void LeaveThread(__spinlock_t * lock, thread_desc * thrd) {
529        verify( ! kernelTLS.preemption_state.enabled );
530        with( * kernelTLS.this_processor ) {
531                finish.action_code = thrd ? Release_Schedule : Release;
532                finish.lock        = lock;
533                finish.thrd        = thrd;
534        }
535
536        returnToKernel();
537}
538
539//=============================================================================================
540// Kernel Setup logic
541//=============================================================================================
542//-----------------------------------------------------------------------------
543// Kernel boot procedures
544void kernel_startup(void) {
545        verify( ! kernelTLS.preemption_state.enabled );
546        __cfaabi_dbg_print_safe("Kernel : Starting\n");
547
548        __cfa_dbg_global_clusters.list{ __get };
549        __cfa_dbg_global_clusters.lock{};
550
551        // Initialize the main cluster
552        mainCluster = (cluster *)&storage_mainCluster;
553        (*mainCluster){"Main Cluster"};
554
555        __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
556
557        // Start by initializing the main thread
558        // SKULLDUGGERY: the mainThread steals the process main thread
559        // which will then be scheduled by the mainProcessor normally
560        mainThread = (thread_desc *)&storage_mainThread;
561        current_stack_info_t info;
562        (*mainThread){ &info };
563
564        __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
565
566
567
568        // Construct the processor context of the main processor
569        void ?{}(processorCtx_t & this, processor * proc) {
570                (this.__cor){ "Processor" };
571                this.__cor.starter = NULL;
572                this.proc = proc;
573        }
574
575        void ?{}(processor & this) with( this ) {
576                name = "Main Processor";
577                cltr = mainCluster;
578                terminated{ 0 };
579                do_terminate = false;
580                preemption_alarm = NULL;
581                pending_preemption = false;
582                kernel_thread = pthread_self();
583
584                runner{ &this };
585                __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
586        }
587
588        // Initialize the main processor and the main processor ctx
589        // (the coroutine that contains the processing control flow)
590        mainProcessor = (processor *)&storage_mainProcessor;
591        (*mainProcessor){};
592
593        //initialize the global state variables
594        kernelTLS.this_processor = mainProcessor;
595        kernelTLS.this_thread    = mainThread;
596        kernelTLS.this_coroutine = &mainThread->self_cor;
597
598        // Enable preemption
599        kernel_start_preemption();
600
601        // Add the main thread to the ready queue
602        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
603        ScheduleThread(mainThread);
604
605        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
606        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
607        // mainThread is on the ready queue when this call is made.
608        kernel_first_resume( kernelTLS.this_processor );
609
610
611
612        // THE SYSTEM IS NOW COMPLETELY RUNNING
613        __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
614
615        verify( ! kernelTLS.preemption_state.enabled );
616        enable_interrupts( __cfaabi_dbg_ctx );
617        verify( TL_GET( preemption_state.enabled ) );
618}
619
620void kernel_shutdown(void) {
621        __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
622
623        verify( TL_GET( preemption_state.enabled ) );
624        disable_interrupts();
625        verify( ! kernelTLS.preemption_state.enabled );
626
627        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
628        // When its coroutine terminates, it return control to the mainThread
629        // which is currently here
630        __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE);
631        returnToKernel();
632        mainThread->self_cor.state = Halted;
633
634        // THE SYSTEM IS NOW COMPLETELY STOPPED
635
636        // Disable preemption
637        kernel_stop_preemption();
638
639        // Destroy the main processor and its context in reverse order of construction
640        // These were manually constructed so we need manually destroy them
641        ^(mainProcessor->runner){};
642        ^(mainProcessor){};
643
644        // Final step, destroy the main thread since it is no longer needed
645        // Since we provided a stack to this taxk it will not destroy anything
646        ^(mainThread){};
647
648        ^(__cfa_dbg_global_clusters.list){};
649        ^(__cfa_dbg_global_clusters.lock){};
650
651        __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
652}
653
654//=============================================================================================
655// Kernel Quiescing
656//=============================================================================================
657
658void halt(processor * this) with( *this ) {
659        // verify( ! __atomic_load_n(&do_terminate, __ATOMIC_SEQ_CST) );
660
661        with( *cltr ) {
662                lock      (proc_list_lock __cfaabi_dbg_ctx2);
663                remove    (procs, *this);
664                push_front(idles, *this);
665                unlock    (proc_list_lock);
666        }
667
668        __cfaabi_dbg_print_safe("Kernel : Processor %p ready to sleep\n", this);
669
670        wait( idleLock );
671
672        __cfaabi_dbg_print_safe("Kernel : Processor %p woke up and ready to run\n", this);
673
674        with( *cltr ) {
675                lock      (proc_list_lock __cfaabi_dbg_ctx2);
676                remove    (idles, *this);
677                push_front(procs, *this);
678                unlock    (proc_list_lock);
679        }
680}
681
682//=============================================================================================
683// Unexpected Terminating logic
684//=============================================================================================
685
686
687static __spinlock_t kernel_abort_lock;
688static bool kernel_abort_called = false;
689
690void * kernel_abort(void) __attribute__ ((__nothrow__)) {
691        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
692        // the globalAbort flag is true.
693        lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
694
695        // first task to abort ?
696        if ( kernel_abort_called ) {                    // not first task to abort ?
697                unlock( kernel_abort_lock );
698
699                sigset_t mask;
700                sigemptyset( &mask );
701                sigaddset( &mask, SIGALRM );            // block SIGALRM signals
702                sigsuspend( &mask );                    // block the processor to prevent further damage during abort
703                _exit( EXIT_FAILURE );                  // if processor unblocks before it is killed, terminate it
704        }
705        else {
706                kernel_abort_called = true;
707                unlock( kernel_abort_lock );
708        }
709
710        return kernelTLS.this_thread;
711}
712
713void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
714        thread_desc * thrd = kernel_data;
715
716        if(thrd) {
717                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
718                __cfaabi_dbg_bits_write( abort_text, len );
719
720                if ( get_coroutine(thrd) != kernelTLS.this_coroutine ) {
721                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", kernelTLS.this_coroutine->name, kernelTLS.this_coroutine );
722                        __cfaabi_dbg_bits_write( abort_text, len );
723                }
724                else {
725                        __cfaabi_dbg_bits_write( ".\n", 2 );
726                }
727        }
728        else {
729                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
730                __cfaabi_dbg_bits_write( abort_text, len );
731        }
732}
733
734int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
735        return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
736}
737
738static __spinlock_t kernel_debug_lock;
739
740extern "C" {
741        void __cfaabi_dbg_bits_acquire() {
742                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
743        }
744
745        void __cfaabi_dbg_bits_release() {
746                unlock( kernel_debug_lock );
747        }
748}
749
750//=============================================================================================
751// Kernel Utilities
752//=============================================================================================
753//-----------------------------------------------------------------------------
754// Locks
755void  ?{}( semaphore & this, int count = 1 ) {
756        (this.lock){};
757        this.count = count;
758        (this.waiting){};
759}
760void ^?{}(semaphore & this) {}
761
762void P(semaphore & this) with( this ){
763        lock( lock __cfaabi_dbg_ctx2 );
764        count -= 1;
765        if ( count < 0 ) {
766                // queue current task
767                append( waiting, kernelTLS.this_thread );
768
769                // atomically release spin lock and block
770                BlockInternal( &lock );
771        }
772        else {
773            unlock( lock );
774        }
775}
776
777void V(semaphore & this) with( this ) {
778        thread_desc * thrd = NULL;
779        lock( lock __cfaabi_dbg_ctx2 );
780        count += 1;
781        if ( count <= 0 ) {
782                // remove task at head of waiting list
783                thrd = pop_head( waiting );
784        }
785
786        unlock( lock );
787
788        // make new owner
789        WakeThread( thrd );
790}
791
792//-----------------------------------------------------------------------------
793// Global Queues
794void doregister( cluster     & cltr ) {
795        lock      ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
796        push_front( __cfa_dbg_global_clusters.list, cltr );
797        unlock    ( __cfa_dbg_global_clusters.lock );
798}
799
800void unregister( cluster     & cltr ) {
801        lock  ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2);
802        remove( __cfa_dbg_global_clusters.list, cltr );
803        unlock( __cfa_dbg_global_clusters.lock );
804}
805
806void doregister( cluster * cltr, thread_desc & thrd ) {
807        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
808        push_front(cltr->threads, thrd);
809        unlock    (cltr->thread_list_lock);
810}
811
812void unregister( cluster * cltr, thread_desc & thrd ) {
813        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
814        remove(cltr->threads, thrd );
815        unlock(cltr->thread_list_lock);
816}
817
818void doregister( cluster * cltr, processor * proc ) {
819        lock      (cltr->proc_list_lock __cfaabi_dbg_ctx2);
820        push_front(cltr->procs, *proc);
821        unlock    (cltr->proc_list_lock);
822}
823
824void unregister( cluster * cltr, processor * proc ) {
825        lock  (cltr->proc_list_lock __cfaabi_dbg_ctx2);
826        remove(cltr->procs, *proc );
827        unlock(cltr->proc_list_lock);
828}
829
830//-----------------------------------------------------------------------------
831// Debug
832__cfaabi_dbg_debug_do(
833        void __cfaabi_dbg_record(__spinlock_t & this, const char * prev_name) {
834                this.prev_name = prev_name;
835                this.prev_thrd = kernelTLS.this_thread;
836        }
837)
838// Local Variables: //
839// mode: c //
840// tab-width: 4 //
841// End: //
Note: See TracBrowser for help on using the repository browser.