source: src/libcfa/concurrency/kernel.c @ 639991a

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

removed workaround for tuple references

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