source: src/libcfa/concurrency/kernel.c @ 14a61b5

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

Ground work for quiescing processors and update/remove TL_GET/TL_SETs. Still work to be done in both cases

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