source: src/libcfa/concurrency/kernel.c @ de94a60

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

some more work on kernel doubly linked lists and fixed segfault in abort message

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