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

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

Kernel shoud now drop preemptions during other preemptions

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