source: src/libcfa/concurrency/kernel.c @ 8ebbfc4

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 8ebbfc4 was a1a17a74, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Moved thread list to cluster, all concurrency object should be accessible through gdb

  • 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(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        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_clusters.list{ __get };
521        global_clusters.lock{};
522
523        // Initialize the main cluster
524        mainCluster = (cluster *)&storage_mainCluster;
525        (*mainCluster){"Main Cluster"};
526
527        __cfaabi_dbg_print_safe("Kernel : Main cluster ready\n");
528
529        // Start by initializing the main thread
530        // SKULLDUGGERY: the mainThread steals the process main thread
531        // which will then be scheduled by the mainProcessor normally
532        mainThread = (thread_desc *)&storage_mainThread;
533        current_stack_info_t info;
534        (*mainThread){ &info };
535
536        __cfaabi_dbg_print_safe("Kernel : Main thread ready\n");
537
538
539
540        // Construct the processor context of the main processor
541        void ?{}(processorCtx_t & this, processor * proc) {
542                (this.__cor){ "Processor" };
543                this.__cor.starter = NULL;
544                this.proc = proc;
545        }
546
547        void ?{}(processor & this) with( this ) {
548                name = "Main Processor";
549                cltr = mainCluster;
550                terminated{ 0 };
551                do_terminate = false;
552                preemption_alarm = NULL;
553                pending_preemption = false;
554                kernel_thread = pthread_self();
555
556                runner{ &this };
557                __cfaabi_dbg_print_safe("Kernel : constructed main processor context %p\n", &runner);
558        }
559
560        // Initialize the main processor and the main processor ctx
561        // (the coroutine that contains the processing control flow)
562        mainProcessor = (processor *)&storage_mainProcessor;
563        (*mainProcessor){};
564
565        //initialize the global state variables
566        kernelTLS.this_processor = mainProcessor;
567        kernelTLS.this_thread    = mainThread;
568        kernelTLS.this_coroutine = &mainThread->self_cor;
569
570        // Enable preemption
571        kernel_start_preemption();
572
573        // Add the main thread to the ready queue
574        // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
575        ScheduleThread(mainThread);
576
577        // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
578        // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
579        // mainThread is on the ready queue when this call is made.
580        kernel_first_resume( kernelTLS.this_processor );
581
582
583
584        // THE SYSTEM IS NOW COMPLETELY RUNNING
585        __cfaabi_dbg_print_safe("Kernel : Started\n--------------------------------------------------\n\n");
586
587        verify( ! kernelTLS.preemption_state.enabled );
588        enable_interrupts( __cfaabi_dbg_ctx );
589        verify( TL_GET( preemption_state.enabled ) );
590}
591
592void kernel_shutdown(void) {
593        __cfaabi_dbg_print_safe("\n--------------------------------------------------\nKernel : Shutting down\n");
594
595        verify( TL_GET( preemption_state.enabled ) );
596        disable_interrupts();
597        verify( ! kernelTLS.preemption_state.enabled );
598
599        // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
600        // When its coroutine terminates, it return control to the mainThread
601        // which is currently here
602        mainProcessor->do_terminate = true;
603        returnToKernel();
604
605        // THE SYSTEM IS NOW COMPLETELY STOPPED
606
607        // Disable preemption
608        kernel_stop_preemption();
609
610        // Destroy the main processor and its context in reverse order of construction
611        // These were manually constructed so we need manually destroy them
612        ^(mainProcessor->runner){};
613        ^(mainProcessor){};
614
615        // Final step, destroy the main thread since it is no longer needed
616        // Since we provided a stack to this taxk it will not destroy anything
617        ^(mainThread){};
618
619        ^(global_clusters.list){};
620        ^(global_clusters.lock){};
621
622        __cfaabi_dbg_print_safe("Kernel : Shutdown complete\n");
623}
624
625//=============================================================================================
626// Kernel Quiescing
627//=============================================================================================
628
629// void halt(processor * this) with( this ) {
630//      pthread_mutex_lock( &idle.lock );
631
632
633
634//      // SKULLDUGGERY: Even if spurious wake-up is a thing
635//      // spuriously waking up a kernel thread is not a big deal
636//      // if it is very rare.
637//      pthread_cond_wait( &idle.cond, &idle.lock);
638//      pthread_mutex_unlock( &idle.lock );
639// }
640
641// void wake(processor * this) with( this ) {
642//      pthread_mutex_lock  (&idle.lock);
643//      pthread_cond_signal (&idle.cond);
644//      pthread_mutex_unlock(&idle.lock);
645// }
646
647//=============================================================================================
648// Unexpected Terminating logic
649//=============================================================================================
650
651
652static __spinlock_t kernel_abort_lock;
653static bool kernel_abort_called = false;
654
655void * kernel_abort(void) __attribute__ ((__nothrow__)) {
656        // abort cannot be recursively entered by the same or different processors because all signal handlers return when
657        // the globalAbort flag is true.
658        lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
659
660        // first task to abort ?
661        if ( kernel_abort_called ) {                    // not first task to abort ?
662                unlock( kernel_abort_lock );
663
664                sigset_t mask;
665                sigemptyset( &mask );
666                sigaddset( &mask, SIGALRM );            // block SIGALRM signals
667                sigsuspend( &mask );                    // block the processor to prevent further damage during abort
668                _exit( EXIT_FAILURE );                  // if processor unblocks before it is killed, terminate it
669        }
670        else {
671                kernel_abort_called = true;
672                unlock( kernel_abort_lock );
673        }
674
675        return kernelTLS.this_thread;
676}
677
678void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
679        thread_desc * thrd = kernel_data;
680
681        if(thrd) {
682                int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
683                __cfaabi_dbg_bits_write( abort_text, len );
684
685                if ( get_coroutine(thrd) != kernelTLS.this_coroutine ) {
686                        len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", kernelTLS.this_coroutine->name, kernelTLS.this_coroutine );
687                        __cfaabi_dbg_bits_write( abort_text, len );
688                }
689                else {
690                        __cfaabi_dbg_bits_write( ".\n", 2 );
691                }
692        }
693        else {
694                int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
695                __cfaabi_dbg_bits_write( abort_text, len );
696        }
697}
698
699int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
700        return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
701}
702
703static __spinlock_t kernel_debug_lock;
704
705extern "C" {
706        void __cfaabi_dbg_bits_acquire() {
707                lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
708        }
709
710        void __cfaabi_dbg_bits_release() {
711                unlock( kernel_debug_lock );
712        }
713}
714
715//=============================================================================================
716// Kernel Utilities
717//=============================================================================================
718//-----------------------------------------------------------------------------
719// Locks
720void  ?{}( semaphore & this, int count = 1 ) {
721        (this.lock){};
722        this.count = count;
723        (this.waiting){};
724}
725void ^?{}(semaphore & this) {}
726
727void P(semaphore & this) with( this ){
728        lock( lock __cfaabi_dbg_ctx2 );
729        count -= 1;
730        if ( count < 0 ) {
731                // queue current task
732                append( waiting, kernelTLS.this_thread );
733
734                // atomically release spin lock and block
735                BlockInternal( &lock );
736        }
737        else {
738            unlock( lock );
739        }
740}
741
742void V(semaphore & this) with( this ) {
743        thread_desc * thrd = NULL;
744        lock( lock __cfaabi_dbg_ctx2 );
745        count += 1;
746        if ( count <= 0 ) {
747                // remove task at head of waiting list
748                thrd = pop_head( waiting );
749        }
750
751        unlock( lock );
752
753        // make new owner
754        WakeThread( thrd );
755}
756
757//-----------------------------------------------------------------------------
758// Global Queues
759void doregister( cluster     & cltr ) {
760        lock      ( global_clusters.lock __cfaabi_dbg_ctx2);
761        push_front( global_clusters.list, cltr );
762        unlock    ( global_clusters.lock );
763}
764
765void unregister( cluster     & cltr ) {
766        lock  ( global_clusters.lock __cfaabi_dbg_ctx2);
767        remove( global_clusters.list, cltr );
768        unlock( global_clusters.lock );
769}
770
771void doregister( cluster * cltr, thread_desc & thrd ) {
772        lock      (cltr->thread_list_lock __cfaabi_dbg_ctx2);
773        push_front(cltr->threads, thrd);
774        unlock    (cltr->thread_list_lock);
775}
776
777void unregister( cluster * cltr, thread_desc & thrd ) {
778        lock  (cltr->thread_list_lock __cfaabi_dbg_ctx2);
779        remove(cltr->threads, thrd );
780        unlock(cltr->thread_list_lock);
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.