source: src/libcfa/concurrency/kernel.c@ 957453d

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 957453d was 9cc0472, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Fix error with volatile on pointer

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