source: src/libcfa/concurrency/kernel.c@ 1a18423

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

Updated tests to use verify instead of assert

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