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

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

Preemption is now stable enough to push, some clean-up needed

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