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

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

Commented some debug messages.
Monitors now yield when spinning.
Debug mode saves more information about previous locks and interrupts

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