source: src/libcfa/concurrency/kernel.c@ 38d70ab

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 38d70ab was 6b0b624, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

change #ifndef to #pragma once

  • Property mode set to 100644
File size: 19.0 KB
RevLine 
[8118303]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
[75f3522]10// Created On : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Fri Jul 21 22:33:18 2017
13// Update Count : 2
[8118303]14//
15
[2ac095d]16#include "libhdr.h"
[8118303]17
18//C Includes
[c84e80a]19#include <stddef.h>
[eb2e723]20extern "C" {
[9d944b2]21#include <stdio.h>
[8fcbb4c]22#include <fenv.h>
[eb2e723]23#include <sys/resource.h>
[9d944b2]24#include <signal.h>
25#include <unistd.h>
[eb2e723]26}
[8118303]27
28//CFA Includes
[2ac095d]29#include "kernel_private.h"
[c81ebf9]30#include "preemption.h"
[2ac095d]31#include "startup.h"
[8118303]32
33//Private includes
34#define __CFA_INVOKE_PRIVATE__
35#include "invoke.h"
36
[2ac095d]37//Start and stop routine for the kernel, declared first to make sure they run first
38void kernel_startup(void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) ));
39void kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) ));
40
[8def349]41//-----------------------------------------------------------------------------
42// Kernel storage
[969b3fe]43KERNEL_STORAGE(cluster, mainCluster);
44KERNEL_STORAGE(processor, mainProcessor);
45KERNEL_STORAGE(processorCtx_t, mainProcessorCtx);
46KERNEL_STORAGE(thread_desc, mainThread);
[f2b12406]47KERNEL_STORAGE(machine_context_t, mainThreadCtx);
[8def349]48
[969b3fe]49cluster * mainCluster;
50processor * mainProcessor;
[348006f]51thread_desc * mainThread;
[eb2e723]52
[bd98b58]53//-----------------------------------------------------------------------------
54// Global state
55
[9cc0472]56thread_local coroutine_desc * volatile this_coroutine;
57thread_local thread_desc * volatile this_thread;
58thread_local processor * volatile this_processor;
[969b3fe]59
[d6ff3ff]60volatile thread_local bool preemption_in_progress = 0;
[1c273d0]61volatile thread_local unsigned short disable_preempt_count = 1;
[c84e80a]62
63//-----------------------------------------------------------------------------
[8def349]64// Main thread construction
65struct current_stack_info_t {
[1c273d0]66 machine_context_t ctx;
[8def349]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
[c84e80a]73};
74
[8def349]75void ?{}( current_stack_info_t * this ) {
[f32e53e]76 CtxGet( this->ctx );
[8def349]77 this->base = this->ctx.FP;
78 this->storage = this->ctx.SP;
79
80 rlimit r;
[132fad4]81 getrlimit( RLIMIT_STACK, &r);
[8def349]82 this->size = r.rlim_cur;
83
84 this->limit = (void *)(((intptr_t)this->base) - this->size);
[969b3fe]85 this->context = &storage_mainThreadCtx;
[8def349]86 this->top = this->base;
87}
88
89void ?{}( coStack_t * this, current_stack_info_t * info) {
90 this->size = info->size;
91 this->storage = info->storage;
92 this->limit = info->limit;
93 this->base = info->base;
94 this->context = info->context;
95 this->top = info->top;
96 this->userStack = true;
97}
98
[c3acb841]99void ?{}( coroutine_desc * this, current_stack_info_t * info) {
[1c273d0]100 (&this->stack){ info };
[8def349]101 this->name = "Main Thread";
102 this->errno_ = 0;
[ee897e4b]103 this->state = Start;
[8def349]104}
105
[348006f]106void ?{}( thread_desc * this, current_stack_info_t * info) {
[17af7d1]107 (&this->cor){ info };
[8def349]108}
[c84e80a]109
[8def349]110//-----------------------------------------------------------------------------
111// Processor coroutine
[eb2e723]112void ?{}(processorCtx_t * this, processor * proc) {
[fa21ac9]113 (&this->__cor){ "Processor" };
[c84e80a]114 this->proc = proc;
[8fcbb4c]115 proc->runner = this;
[8def349]116}
117
118void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) {
[17af7d1]119 (&this->__cor){ info };
[8def349]120 this->proc = proc;
[8fcbb4c]121 proc->runner = this;
[8def349]122}
123
124void ?{}(processor * this) {
[969b3fe]125 this{ mainCluster };
[8def349]126}
127
128void ?{}(processor * this, cluster * cltr) {
129 this->cltr = cltr;
[bdeba0b]130 (&this->terminated){ 0 };
[e60e0dc]131 this->do_terminate = false;
[c81ebf9]132 this->preemption_alarm = NULL;
133 this->pending_preemption = false;
[8def349]134
135 start( this );
[c84e80a]136}
137
[8fcbb4c]138void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
[8def349]139 this->cltr = cltr;
[bdeba0b]140 (&this->terminated){ 0 };
[e60e0dc]141 this->do_terminate = false;
[82ff5845]142 this->preemption_alarm = NULL;
[c81ebf9]143 this->pending_preemption = false;
[82ff5845]144 this->kernel_thread = pthread_self();
[8def349]145
[8fcbb4c]146 this->runner = runner;
[969b3fe]147 LIB_DEBUG_PRINT_SAFE("Kernel : constructing main processor context %p\n", runner);
[8fcbb4c]148 runner{ this };
[8def349]149}
150
151void ^?{}(processor * this) {
[e60e0dc]152 if( ! this->do_terminate ) {
[9d944b2]153 LIB_DEBUG_PRINT_SAFE("Kernel : core %p signaling termination\n", this);
[e60e0dc]154 this->do_terminate = true;
[bdeba0b]155 P( &this->terminated );
156 pthread_join( this->kernel_thread, NULL );
[8def349]157 }
158}
159
160void ?{}(cluster * this) {
161 ( &this->ready_queue ){};
[e60e0dc]162 ( &this->ready_queue_lock ){};
163
164 this->preemption = default_preemption();
[8def349]165}
166
167void ^?{}(cluster * this) {
[1c273d0]168
[c84e80a]169}
170
[75f3522]171//=============================================================================================
172// Kernel Scheduling logic
173//=============================================================================================
[8fcbb4c]174//Main of the processor contexts
175void main(processorCtx_t * runner) {
176 processor * this = runner->proc;
[c81ebf9]177
[9d944b2]178 LIB_DEBUG_PRINT_SAFE("Kernel : core %p starting\n", this);
[8118303]179
[75f3522]180 {
[c81ebf9]181 // Setup preemption data
182 preemption_scope scope = { this };
183
184 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
[8118303]185
[c81ebf9]186 thread_desc * readyThread = NULL;
[e60e0dc]187 for( unsigned int spin_count = 0; ! this->do_terminate; spin_count++ )
[75f3522]188 {
[c81ebf9]189 readyThread = nextThread( this->cltr );
[75f3522]190
[c81ebf9]191 if(readyThread)
192 {
[0b33412]193 verify( disable_preempt_count > 0 );
[4e6fb8e]194
[c81ebf9]195 runThread(this, readyThread);
[75f3522]196
[0b33412]197 verify( disable_preempt_count > 0 );
[4e6fb8e]198
[c81ebf9]199 //Some actions need to be taken from the kernel
200 finishRunning(this);
201
202 spin_count = 0;
203 }
204 else
205 {
206 spin(this, &spin_count);
207 }
208 }
209
210 LIB_DEBUG_PRINT_SAFE("Kernel : core %p stopping\n", this);
[c84e80a]211 }
[8118303]212
[bdeba0b]213 V( &this->terminated );
214
[9d944b2]215 LIB_DEBUG_PRINT_SAFE("Kernel : core %p terminated\n", this);
[c84e80a]216}
217
[1c273d0]218// runThread runs a thread by context switching
219// from the processor coroutine to the target thread
[348006f]220void runThread(processor * this, thread_desc * dst) {
[c3acb841]221 coroutine_desc * proc_cor = get_coroutine(this->runner);
222 coroutine_desc * thrd_cor = get_coroutine(dst);
[1c273d0]223
[75f3522]224 //Reset the terminating actions here
[db6f06a]225 this->finish.action_code = No_Action;
[8fcbb4c]226
[75f3522]227 //Update global state
[1c273d0]228 this_thread = dst;
[75f3522]229
230 // Context Switch to the thread
231 ThreadCtxSwitch(proc_cor, thrd_cor);
232 // when ThreadCtxSwitch returns we are back in the processor coroutine
233}
234
[1c273d0]235// Once a thread has finished running, some of
[75f3522]236// its final actions must be executed from the kernel
[db6f06a]237void finishRunning(processor * this) {
238 if( this->finish.action_code == Release ) {
239 unlock( this->finish.lock );
240 }
241 else if( this->finish.action_code == Schedule ) {
242 ScheduleThread( this->finish.thrd );
243 }
244 else if( this->finish.action_code == Release_Schedule ) {
[1c273d0]245 unlock( this->finish.lock );
[db6f06a]246 ScheduleThread( this->finish.thrd );
247 }
[0c78741]248 else if( this->finish.action_code == Release_Multi ) {
249 for(int i = 0; i < this->finish.lock_count; i++) {
250 unlock( this->finish.locks[i] );
251 }
252 }
253 else if( this->finish.action_code == Release_Multi_Schedule ) {
254 for(int i = 0; i < this->finish.lock_count; i++) {
255 unlock( this->finish.locks[i] );
256 }
257 for(int i = 0; i < this->finish.thrd_count; i++) {
258 ScheduleThread( this->finish.thrds[i] );
259 }
260 }
[db6f06a]261 else {
262 assert(this->finish.action_code == No_Action);
[8fcbb4c]263 }
[c84e80a]264}
265
[0c92c9f]266// Handles spinning logic
267// TODO : find some strategy to put cores to sleep after some time
[c84e80a]268void spin(processor * this, unsigned int * spin_count) {
269 (*spin_count)++;
270}
271
[0c92c9f]272// Context invoker for processors
273// This is the entry point for processors (kernel threads)
274// It effectively constructs a coroutine by stealing the pthread stack
[8def349]275void * CtxInvokeProcessor(void * arg) {
276 processor * proc = (processor *) arg;
277 this_processor = proc;
[1c273d0]278 this_coroutine = NULL;
279 this_thread = NULL;
[4e6fb8e]280 disable_preempt_count = 1;
[8def349]281 // SKULLDUGGERY: We want to create a context for the processor coroutine
282 // which is needed for the 2-step context switch. However, there is no reason
[1c273d0]283 // to waste the perfectly valid stack create by pthread.
[8def349]284 current_stack_info_t info;
285 machine_context_t ctx;
286 info.context = &ctx;
287 processorCtx_t proc_cor_storage = { proc, &info };
288
[9d944b2]289 LIB_DEBUG_PRINT_SAFE("Coroutine : created stack %p\n", proc_cor_storage.__cor.stack.base);
[8fcbb4c]290
[0c92c9f]291 //Set global state
[1c273d0]292 this_coroutine = &proc->runner->__cor;
293 this_thread = NULL;
[8def349]294
295 //We now have a proper context from which to schedule threads
[9d944b2]296 LIB_DEBUG_PRINT_SAFE("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
[8def349]297
[1c273d0]298 // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
299 // resume it to start it like it normally would, it will just context switch
300 // back to here. Instead directly call the main since we already are on the
[8def349]301 // appropriate stack.
[17af7d1]302 proc_cor_storage.__cor.state = Active;
[4aa2fb2]303 main( &proc_cor_storage );
304 proc_cor_storage.__cor.state = Halted;
[8def349]305
[0c92c9f]306 // Main routine of the core returned, the core is now fully terminated
[1c273d0]307 LIB_DEBUG_PRINT_SAFE("Kernel : core %p main ended (%p)\n", proc, proc->runner);
[8def349]308
309 return NULL;
[c84e80a]310}
311
[8def349]312void start(processor * this) {
[9d944b2]313 LIB_DEBUG_PRINT_SAFE("Kernel : Starting core %p\n", this);
[82ff5845]314
[8fcbb4c]315 pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
[eb2e723]316
[1c273d0]317 LIB_DEBUG_PRINT_SAFE("Kernel : core %p started\n", this);
[eb2e723]318}
319
[8def349]320//-----------------------------------------------------------------------------
321// Scheduler routines
[348006f]322void ScheduleThread( thread_desc * thrd ) {
[1c273d0]323 // if( !thrd ) return;
324 assert( thrd );
325 assert( thrd->cor.state != Halted );
326
327 verify( disable_preempt_count > 0 );
[690f13c]328
[4aa2fb2]329 verifyf( thrd->next == NULL, "Expected null got %p", thrd->next );
[1c273d0]330
[969b3fe]331 lock( &this_processor->cltr->ready_queue_lock DEBUG_CTX2 );
332 append( &this_processor->cltr->ready_queue, thrd );
333 unlock( &this_processor->cltr->ready_queue_lock );
[1c273d0]334
335 verify( disable_preempt_count > 0 );
[db6f06a]336}
337
[348006f]338thread_desc * nextThread(cluster * this) {
[1c273d0]339 verify( disable_preempt_count > 0 );
[e60e0dc]340 lock( &this->ready_queue_lock DEBUG_CTX2 );
[348006f]341 thread_desc * head = pop_head( &this->ready_queue );
[e60e0dc]342 unlock( &this->ready_queue_lock );
[1c273d0]343 verify( disable_preempt_count > 0 );
[db6f06a]344 return head;
[eb2e723]345}
346
[82ff5845]347void BlockInternal() {
348 disable_interrupts();
[0b33412]349 verify( disable_preempt_count > 0 );
[75f3522]350 suspend();
[0b33412]351 verify( disable_preempt_count > 0 );
[2ac095d]352 enable_interrupts( DEBUG_CTX );
[75f3522]353}
354
[82ff5845]355void BlockInternal( spinlock * lock ) {
356 disable_interrupts();
[89a3df5]357 this_processor->finish.action_code = Release;
358 this_processor->finish.lock = lock;
[0b33412]359
360 verify( disable_preempt_count > 0 );
[db6f06a]361 suspend();
[0b33412]362 verify( disable_preempt_count > 0 );
363
[2ac095d]364 enable_interrupts( DEBUG_CTX );
[db6f06a]365}
366
[82ff5845]367void BlockInternal( thread_desc * thrd ) {
368 disable_interrupts();
[1c273d0]369 assert( thrd->cor.state != Halted );
[89a3df5]370 this_processor->finish.action_code = Schedule;
371 this_processor->finish.thrd = thrd;
[0b33412]372
373 verify( disable_preempt_count > 0 );
[db6f06a]374 suspend();
[0b33412]375 verify( disable_preempt_count > 0 );
376
[2ac095d]377 enable_interrupts( DEBUG_CTX );
[db6f06a]378}
379
[82ff5845]380void BlockInternal( spinlock * lock, thread_desc * thrd ) {
381 disable_interrupts();
[89a3df5]382 this_processor->finish.action_code = Release_Schedule;
383 this_processor->finish.lock = lock;
384 this_processor->finish.thrd = thrd;
[0b33412]385
386 verify( disable_preempt_count > 0 );
[db6f06a]387 suspend();
[0b33412]388 verify( disable_preempt_count > 0 );
389
[2ac095d]390 enable_interrupts( DEBUG_CTX );
[eb2e723]391}
392
[82ff5845]393void BlockInternal(spinlock ** locks, unsigned short count) {
394 disable_interrupts();
[0c78741]395 this_processor->finish.action_code = Release_Multi;
396 this_processor->finish.locks = locks;
397 this_processor->finish.lock_count = count;
[0b33412]398
399 verify( disable_preempt_count > 0 );
[0c78741]400 suspend();
[0b33412]401 verify( disable_preempt_count > 0 );
402
[2ac095d]403 enable_interrupts( DEBUG_CTX );
[0c78741]404}
405
[82ff5845]406void BlockInternal(spinlock ** locks, unsigned short lock_count, thread_desc ** thrds, unsigned short thrd_count) {
407 disable_interrupts();
[0c78741]408 this_processor->finish.action_code = Release_Multi_Schedule;
409 this_processor->finish.locks = locks;
410 this_processor->finish.lock_count = lock_count;
411 this_processor->finish.thrds = thrds;
412 this_processor->finish.thrd_count = thrd_count;
[0b33412]413
414 verify( disable_preempt_count > 0 );
[0c78741]415 suspend();
[0b33412]416 verify( disable_preempt_count > 0 );
417
[2ac095d]418 enable_interrupts( DEBUG_CTX );
[0c78741]419}
420
[f2b12406]421void LeaveThread(spinlock * lock, thread_desc * thrd) {
422 verify( disable_preempt_count > 0 );
423 this_processor->finish.action_code = thrd ? Release_Schedule : Release;
424 this_processor->finish.lock = lock;
425 this_processor->finish.thrd = thrd;
426
427 suspend();
428}
429
[fa21ac9]430//=============================================================================================
431// Kernel Setup logic
432//=============================================================================================
[eb2e723]433//-----------------------------------------------------------------------------
434// Kernel boot procedures
435void kernel_startup(void) {
[1c273d0]436 LIB_DEBUG_PRINT_SAFE("Kernel : Starting\n");
[eb2e723]437
438 // Start by initializing the main thread
[1c273d0]439 // SKULLDUGGERY: the mainThread steals the process main thread
[969b3fe]440 // which will then be scheduled by the mainProcessor normally
441 mainThread = (thread_desc *)&storage_mainThread;
[8fcbb4c]442 current_stack_info_t info;
[8def349]443 mainThread{ &info };
[eb2e723]444
[fa21ac9]445 LIB_DEBUG_PRINT_SAFE("Kernel : Main thread ready\n");
446
[969b3fe]447 // Initialize the main cluster
448 mainCluster = (cluster *)&storage_mainCluster;
449 mainCluster{};
[bd98b58]450
[969b3fe]451 LIB_DEBUG_PRINT_SAFE("Kernel : main cluster ready\n");
[fa21ac9]452
[969b3fe]453 // Initialize the main processor and the main processor ctx
[eb2e723]454 // (the coroutine that contains the processing control flow)
[969b3fe]455 mainProcessor = (processor *)&storage_mainProcessor;
456 mainProcessor{ mainCluster, (processorCtx_t *)&storage_mainProcessorCtx };
[eb2e723]457
[dcb42b8]458 //initialize the global state variables
[969b3fe]459 this_processor = mainProcessor;
[1c273d0]460 this_thread = mainThread;
461 this_coroutine = &mainThread->cor;
[eb2e723]462
[82ff5845]463 // Enable preemption
464 kernel_start_preemption();
465
[969b3fe]466 // Add the main thread to the ready queue
467 // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread
468 ScheduleThread(mainThread);
469
470 // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX
[dcb42b8]471 // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
[1c273d0]472 // mainThread is on the ready queue when this call is made.
[969b3fe]473 resume( mainProcessor->runner );
[eb2e723]474
[dcb42b8]475
476
477 // THE SYSTEM IS NOW COMPLETELY RUNNING
[9d944b2]478 LIB_DEBUG_PRINT_SAFE("Kernel : Started\n--------------------------------------------------\n\n");
[82ff5845]479
[2ac095d]480 enable_interrupts( DEBUG_CTX );
[eb2e723]481}
482
[dcb42b8]483void kernel_shutdown(void) {
[9d944b2]484 LIB_DEBUG_PRINT_SAFE("\n--------------------------------------------------\nKernel : Shutting down\n");
[eb2e723]485
[4e6fb8e]486 disable_interrupts();
487
[969b3fe]488 // SKULLDUGGERY: Notify the mainProcessor it needs to terminates.
[dcb42b8]489 // When its coroutine terminates, it return control to the mainThread
490 // which is currently here
[969b3fe]491 mainProcessor->do_terminate = true;
[eb2e723]492 suspend();
493
[dcb42b8]494 // THE SYSTEM IS NOW COMPLETELY STOPPED
[eb2e723]495
[82ff5845]496 // Disable preemption
497 kernel_stop_preemption();
498
[969b3fe]499 // Destroy the main processor and its context in reverse order of construction
[dcb42b8]500 // These were manually constructed so we need manually destroy them
[969b3fe]501 ^(mainProcessor->runner){};
502 ^(mainProcessor){};
[eb2e723]503
[dcb42b8]504 // Final step, destroy the main thread since it is no longer needed
505 // Since we provided a stack to this taxk it will not destroy anything
[eb2e723]506 ^(mainThread){};
507
[1c273d0]508 LIB_DEBUG_PRINT_SAFE("Kernel : Shutdown complete\n");
[9d944b2]509}
510
511static spinlock kernel_abort_lock;
512static spinlock kernel_debug_lock;
513static bool kernel_abort_called = false;
514
515void * kernel_abort (void) __attribute__ ((__nothrow__)) {
516 // abort cannot be recursively entered by the same or different processors because all signal handlers return when
517 // the globalAbort flag is true.
[2ac095d]518 lock( &kernel_abort_lock DEBUG_CTX2 );
[9d944b2]519
520 // first task to abort ?
521 if ( !kernel_abort_called ) { // not first task to abort ?
522 kernel_abort_called = true;
523 unlock( &kernel_abort_lock );
[1c273d0]524 }
[9d944b2]525 else {
526 unlock( &kernel_abort_lock );
[1c273d0]527
[9d944b2]528 sigset_t mask;
529 sigemptyset( &mask );
530 sigaddset( &mask, SIGALRM ); // block SIGALRM signals
531 sigaddset( &mask, SIGUSR1 ); // block SIGUSR1 signals
532 sigsuspend( &mask ); // block the processor to prevent further damage during abort
[1c273d0]533 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
[9d944b2]534 }
535
[1c273d0]536 return this_thread;
[9d944b2]537}
538
539void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
540 thread_desc * thrd = kernel_data;
541
542 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing task %.256s (%p)", thrd->cor.name, thrd );
543 __lib_debug_write( STDERR_FILENO, abort_text, len );
544
[1c273d0]545 if ( thrd != this_coroutine ) {
546 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", this_coroutine->name, this_coroutine );
[9d944b2]547 __lib_debug_write( STDERR_FILENO, abort_text, len );
[1c273d0]548 }
[9d944b2]549 else {
550 __lib_debug_write( STDERR_FILENO, ".\n", 2 );
551 }
552}
553
554extern "C" {
555 void __lib_debug_acquire() {
[2ac095d]556 lock( &kernel_debug_lock DEBUG_CTX2 );
[9d944b2]557 }
558
559 void __lib_debug_release() {
[2ac095d]560 unlock( &kernel_debug_lock );
[9d944b2]561 }
[8118303]562}
563
[fa21ac9]564//=============================================================================================
565// Kernel Utilities
566//=============================================================================================
[bd98b58]567//-----------------------------------------------------------------------------
568// Locks
[db6f06a]569void ?{}( spinlock * this ) {
570 this->lock = 0;
[bd98b58]571}
[db6f06a]572void ^?{}( spinlock * this ) {
[bd98b58]573
[db6f06a]574}
575
[2ac095d]576bool try_lock( spinlock * this DEBUG_CTX_PARAM2 ) {
[b227f68]577 return this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0;
[c81ebf9]578}
579
[2ac095d]580void lock( spinlock * this DEBUG_CTX_PARAM2 ) {
[db6f06a]581 for ( unsigned int i = 1;; i += 1 ) {
[b227f68]582 if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
[db6f06a]583 }
[b227f68]584 LIB_DEBUG_DO(
585 this->prev_name = caller;
586 this->prev_thrd = this_thread;
587 )
[db6f06a]588}
[bd98b58]589
[b227f68]590void lock_yield( spinlock * this DEBUG_CTX_PARAM2 ) {
591 for ( unsigned int i = 1;; i += 1 ) {
592 if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) { break; }
593 yield();
594 }
595 LIB_DEBUG_DO(
596 this->prev_name = caller;
597 this->prev_thrd = this_thread;
598 )
599}
600
601
[db6f06a]602void unlock( spinlock * this ) {
603 __sync_lock_release_4( &this->lock );
[bd98b58]604}
605
[bdeba0b]606void ?{}( semaphore * this, int count = 1 ) {
607 (&this->lock){};
608 this->count = count;
609 (&this->waiting){};
[db6f06a]610}
[bdeba0b]611void ^?{}(semaphore * this) {}
[db6f06a]612
[bdeba0b]613void P(semaphore * this) {
[2ac095d]614 lock( &this->lock DEBUG_CTX2 );
[bdeba0b]615 this->count -= 1;
616 if ( this->count < 0 ) {
617 // queue current task
618 append( &this->waiting, (thread_desc *)this_thread );
619
620 // atomically release spin lock and block
[82ff5845]621 BlockInternal( &this->lock );
[8def349]622 }
[4e6fb8e]623 else {
[bdeba0b]624 unlock( &this->lock );
[4e6fb8e]625 }
[bd98b58]626}
627
[bdeba0b]628void V(semaphore * this) {
629 thread_desc * thrd = NULL;
[2ac095d]630 lock( &this->lock DEBUG_CTX2 );
[bdeba0b]631 this->count += 1;
632 if ( this->count <= 0 ) {
633 // remove task at head of waiting list
634 thrd = pop_head( &this->waiting );
[bd98b58]635 }
[bdeba0b]636
[db6f06a]637 unlock( &this->lock );
[bdeba0b]638
639 // make new owner
640 WakeThread( thrd );
[bd98b58]641}
642
643//-----------------------------------------------------------------------------
644// Queues
[5ea06d6]645void ?{}( __thread_queue_t * this ) {
[bd98b58]646 this->head = NULL;
647 this->tail = &this->head;
648}
649
[5ea06d6]650void append( __thread_queue_t * this, thread_desc * t ) {
[4aa2fb2]651 verify(this->tail != NULL);
[bd98b58]652 *this->tail = t;
653 this->tail = &t->next;
654}
655
[5ea06d6]656thread_desc * pop_head( __thread_queue_t * this ) {
[348006f]657 thread_desc * head = this->head;
[bd98b58]658 if( head ) {
659 this->head = head->next;
660 if( !head->next ) {
661 this->tail = &this->head;
662 }
663 head->next = NULL;
[1c273d0]664 }
[bd98b58]665 return head;
666}
[690f13c]667
[0c78741]668void ?{}( __condition_stack_t * this ) {
[690f13c]669 this->top = NULL;
670}
671
[0c78741]672void push( __condition_stack_t * this, __condition_criterion_t * t ) {
[4aa2fb2]673 verify( !t->next );
[690f13c]674 t->next = this->top;
675 this->top = t;
676}
677
[0c78741]678__condition_criterion_t * pop( __condition_stack_t * this ) {
679 __condition_criterion_t * top = this->top;
[690f13c]680 if( top ) {
681 this->top = top->next;
682 top->next = NULL;
[1c273d0]683 }
[690f13c]684 return top;
685}
[6b0b624]686
[8118303]687// Local Variables: //
688// mode: c //
689// tab-width: 4 //
690// End: //
Note: See TracBrowser for help on using the repository browser.