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

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

Some cleanu[ in the kernel, notably phasing out the system processor

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