source: src/libcfa/concurrency/kernel.c @ 09800e9

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 09800e9 was 09800e9, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

First draft at locks in Cforall

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