[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 |
|
---|
[0c92c9f] | 17 | //Start and stop routine for the kernel, declared first to make sure they run first
|
---|
| 18 | void kernel_startup(void) __attribute__((constructor(101)));
|
---|
| 19 | void kernel_shutdown(void) __attribute__((destructor(101)));
|
---|
| 20 |
|
---|
[8118303] | 21 | //Header
|
---|
[75f3522] | 22 | #include "kernel_private.h"
|
---|
[8118303] | 23 |
|
---|
| 24 | //C Includes
|
---|
[c84e80a] | 25 | #include <stddef.h>
|
---|
[eb2e723] | 26 | extern "C" {
|
---|
[8fcbb4c] | 27 | #include <fenv.h>
|
---|
[eb2e723] | 28 | #include <sys/resource.h>
|
---|
| 29 | }
|
---|
[8118303] | 30 |
|
---|
| 31 | //CFA Includes
|
---|
| 32 | #include "libhdr.h"
|
---|
| 33 |
|
---|
| 34 | //Private includes
|
---|
| 35 | #define __CFA_INVOKE_PRIVATE__
|
---|
| 36 | #include "invoke.h"
|
---|
| 37 |
|
---|
[8def349] | 38 | //-----------------------------------------------------------------------------
|
---|
| 39 | // Kernel storage
|
---|
| 40 | #define KERNEL_STORAGE(T,X) static char X##_storage[sizeof(T)]
|
---|
| 41 |
|
---|
| 42 | KERNEL_STORAGE(processorCtx_t, systemProcessorCtx);
|
---|
| 43 | KERNEL_STORAGE(cluster, systemCluster);
|
---|
| 44 | KERNEL_STORAGE(processor, systemProcessor);
|
---|
[e15df4c] | 45 | KERNEL_STORAGE(thread, mainThread);
|
---|
[8def349] | 46 | KERNEL_STORAGE(machine_context_t, mainThread_context);
|
---|
| 47 |
|
---|
[bd98b58] | 48 | cluster * systemCluster;
|
---|
[eb2e723] | 49 | processor * systemProcessor;
|
---|
[e15df4c] | 50 | thread * mainThread;
|
---|
[eb2e723] | 51 |
|
---|
[bd98b58] | 52 | //-----------------------------------------------------------------------------
|
---|
| 53 | // Global state
|
---|
| 54 |
|
---|
[8def349] | 55 | thread_local processor * this_processor;
|
---|
| 56 |
|
---|
| 57 | processor * get_this_processor() {
|
---|
| 58 | return this_processor;
|
---|
| 59 | }
|
---|
[c84e80a] | 60 |
|
---|
[bd98b58] | 61 | coroutine * this_coroutine(void) {
|
---|
| 62 | return this_processor->current_coroutine;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
[e15df4c] | 65 | thread * this_thread(void) {
|
---|
[bd98b58] | 66 | return this_processor->current_thread;
|
---|
[c84e80a] | 67 | }
|
---|
| 68 |
|
---|
| 69 | //-----------------------------------------------------------------------------
|
---|
[8def349] | 70 | // Main thread construction
|
---|
| 71 | struct current_stack_info_t {
|
---|
| 72 | machine_context_t ctx;
|
---|
| 73 | unsigned int size; // size of stack
|
---|
| 74 | void *base; // base of stack
|
---|
| 75 | void *storage; // pointer to stack
|
---|
| 76 | void *limit; // stack grows towards stack limit
|
---|
| 77 | void *context; // address of cfa_context_t
|
---|
| 78 | void *top; // address of top of storage
|
---|
[c84e80a] | 79 | };
|
---|
| 80 |
|
---|
[8def349] | 81 | void ?{}( current_stack_info_t * this ) {
|
---|
| 82 | CtxGet( &this->ctx );
|
---|
| 83 | this->base = this->ctx.FP;
|
---|
| 84 | this->storage = this->ctx.SP;
|
---|
| 85 |
|
---|
| 86 | rlimit r;
|
---|
[132fad4] | 87 | getrlimit( RLIMIT_STACK, &r);
|
---|
[8def349] | 88 | this->size = r.rlim_cur;
|
---|
| 89 |
|
---|
| 90 | this->limit = (void *)(((intptr_t)this->base) - this->size);
|
---|
| 91 | this->context = &mainThread_context_storage;
|
---|
| 92 | this->top = this->base;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void ?{}( coStack_t * this, current_stack_info_t * info) {
|
---|
| 96 | this->size = info->size;
|
---|
| 97 | this->storage = info->storage;
|
---|
| 98 | this->limit = info->limit;
|
---|
| 99 | this->base = info->base;
|
---|
| 100 | this->context = info->context;
|
---|
| 101 | this->top = info->top;
|
---|
| 102 | this->userStack = true;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | void ?{}( coroutine * this, current_stack_info_t * info) {
|
---|
| 106 | (&this->stack){ info };
|
---|
| 107 | this->name = "Main Thread";
|
---|
| 108 | this->errno_ = 0;
|
---|
[ee897e4b] | 109 | this->state = Start;
|
---|
[8def349] | 110 | }
|
---|
| 111 |
|
---|
[e15df4c] | 112 | void ?{}( thread * this, current_stack_info_t * info) {
|
---|
[8def349] | 113 | (&this->c){ info };
|
---|
| 114 | }
|
---|
[c84e80a] | 115 |
|
---|
[8def349] | 116 | //-----------------------------------------------------------------------------
|
---|
| 117 | // Processor coroutine
|
---|
[eb2e723] | 118 | void ?{}(processorCtx_t * this, processor * proc) {
|
---|
| 119 | (&this->c){};
|
---|
[c84e80a] | 120 | this->proc = proc;
|
---|
[8fcbb4c] | 121 | proc->runner = this;
|
---|
[8def349] | 122 | }
|
---|
| 123 |
|
---|
| 124 | void ?{}(processorCtx_t * this, processor * proc, current_stack_info_t * info) {
|
---|
| 125 | (&this->c){ info };
|
---|
| 126 | this->proc = proc;
|
---|
[8fcbb4c] | 127 | proc->runner = this;
|
---|
[8def349] | 128 | }
|
---|
| 129 |
|
---|
| 130 | void ?{}(processor * this) {
|
---|
| 131 | this{ systemCluster };
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void ?{}(processor * this, cluster * cltr) {
|
---|
| 135 | this->cltr = cltr;
|
---|
| 136 | this->current_coroutine = NULL;
|
---|
| 137 | this->current_thread = NULL;
|
---|
[db6f06a] | 138 | (&this->terminated){};
|
---|
| 139 | this->is_terminated = false;
|
---|
[8def349] | 140 |
|
---|
| 141 | start( this );
|
---|
[c84e80a] | 142 | }
|
---|
| 143 |
|
---|
[8fcbb4c] | 144 | void ?{}(processor * this, cluster * cltr, processorCtx_t * runner) {
|
---|
[8def349] | 145 | this->cltr = cltr;
|
---|
| 146 | this->current_coroutine = NULL;
|
---|
| 147 | this->current_thread = NULL;
|
---|
[db6f06a] | 148 | (&this->terminated){};
|
---|
| 149 | this->is_terminated = false;
|
---|
[8def349] | 150 |
|
---|
[8fcbb4c] | 151 | this->runner = runner;
|
---|
| 152 | LIB_DEBUG_PRINTF("Kernel : constructing processor context %p\n", runner);
|
---|
| 153 | runner{ this };
|
---|
[8def349] | 154 | }
|
---|
| 155 |
|
---|
| 156 | void ^?{}(processor * this) {
|
---|
[db6f06a] | 157 | if( ! this->is_terminated ) {
|
---|
[8def349] | 158 | LIB_DEBUG_PRINTF("Kernel : core %p signaling termination\n", this);
|
---|
[db6f06a] | 159 | this->is_terminated = true;
|
---|
| 160 | wait( &this->terminated );
|
---|
[8def349] | 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | void ?{}(cluster * this) {
|
---|
| 165 | ( &this->ready_queue ){};
|
---|
[eafb094] | 166 | ( &this->lock ){};
|
---|
[8def349] | 167 | }
|
---|
| 168 |
|
---|
| 169 | void ^?{}(cluster * this) {
|
---|
[8fcbb4c] | 170 |
|
---|
[c84e80a] | 171 | }
|
---|
| 172 |
|
---|
[75f3522] | 173 | //=============================================================================================
|
---|
| 174 | // Kernel Scheduling logic
|
---|
| 175 | //=============================================================================================
|
---|
[8fcbb4c] | 176 | //Main of the processor contexts
|
---|
| 177 | void main(processorCtx_t * runner) {
|
---|
| 178 | processor * this = runner->proc;
|
---|
[eb2e723] | 179 | LIB_DEBUG_PRINTF("Kernel : core %p starting\n", this);
|
---|
[8118303] | 180 |
|
---|
[e15df4c] | 181 | thread * readyThread = NULL;
|
---|
[db6f06a] | 182 | for( unsigned int spin_count = 0; ! this->is_terminated; spin_count++ )
|
---|
[75f3522] | 183 | {
|
---|
[bd98b58] | 184 | readyThread = nextThread( this->cltr );
|
---|
[8118303] | 185 |
|
---|
[75f3522] | 186 | if(readyThread)
|
---|
| 187 | {
|
---|
| 188 | runThread(this, readyThread);
|
---|
| 189 |
|
---|
| 190 | //Some actions need to be taken from the kernel
|
---|
[db6f06a] | 191 | finishRunning(this);
|
---|
[75f3522] | 192 |
|
---|
[c84e80a] | 193 | spin_count = 0;
|
---|
[75f3522] | 194 | }
|
---|
| 195 | else
|
---|
| 196 | {
|
---|
[c84e80a] | 197 | spin(this, &spin_count);
|
---|
| 198 | }
|
---|
| 199 | }
|
---|
[8118303] | 200 |
|
---|
[8def349] | 201 | LIB_DEBUG_PRINTF("Kernel : core %p unlocking thread\n", this);
|
---|
[db6f06a] | 202 | signal( &this->terminated );
|
---|
[c84e80a] | 203 | LIB_DEBUG_PRINTF("Kernel : core %p terminated\n", this);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[75f3522] | 206 | // runThread runs a thread by context switching
|
---|
[0c92c9f] | 207 | // from the processor coroutine to the target thread
|
---|
[75f3522] | 208 | void runThread(processor * this, thread * dst) {
|
---|
| 209 | coroutine * proc_cor = get_coroutine(this->runner);
|
---|
| 210 | coroutine * thrd_cor = get_coroutine(dst);
|
---|
| 211 |
|
---|
| 212 | //Reset the terminating actions here
|
---|
[db6f06a] | 213 | this->finish.action_code = No_Action;
|
---|
[8fcbb4c] | 214 |
|
---|
[75f3522] | 215 | //Update global state
|
---|
| 216 | this->current_thread = dst;
|
---|
| 217 |
|
---|
| 218 | // Context Switch to the thread
|
---|
| 219 | ThreadCtxSwitch(proc_cor, thrd_cor);
|
---|
| 220 | // when ThreadCtxSwitch returns we are back in the processor coroutine
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | // Once a thread has finished running, some of
|
---|
| 224 | // its final actions must be executed from the kernel
|
---|
[db6f06a] | 225 | void finishRunning(processor * this) {
|
---|
| 226 | if( this->finish.action_code == Release ) {
|
---|
| 227 | unlock( this->finish.lock );
|
---|
| 228 | }
|
---|
| 229 | else if( this->finish.action_code == Schedule ) {
|
---|
| 230 | ScheduleThread( this->finish.thrd );
|
---|
| 231 | }
|
---|
| 232 | else if( this->finish.action_code == Release_Schedule ) {
|
---|
| 233 | unlock( this->finish.lock );
|
---|
| 234 | ScheduleThread( this->finish.thrd );
|
---|
| 235 | }
|
---|
| 236 | else {
|
---|
| 237 | assert(this->finish.action_code == No_Action);
|
---|
[8fcbb4c] | 238 | }
|
---|
[c84e80a] | 239 | }
|
---|
| 240 |
|
---|
[0c92c9f] | 241 | // Handles spinning logic
|
---|
| 242 | // TODO : find some strategy to put cores to sleep after some time
|
---|
[c84e80a] | 243 | void spin(processor * this, unsigned int * spin_count) {
|
---|
| 244 | (*spin_count)++;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
[0c92c9f] | 247 | // Context invoker for processors
|
---|
| 248 | // This is the entry point for processors (kernel threads)
|
---|
| 249 | // It effectively constructs a coroutine by stealing the pthread stack
|
---|
[8def349] | 250 | void * CtxInvokeProcessor(void * arg) {
|
---|
| 251 | processor * proc = (processor *) arg;
|
---|
| 252 | this_processor = proc;
|
---|
| 253 | // SKULLDUGGERY: We want to create a context for the processor coroutine
|
---|
| 254 | // which is needed for the 2-step context switch. However, there is no reason
|
---|
| 255 | // to waste the perfectly valid stack create by pthread.
|
---|
| 256 | current_stack_info_t info;
|
---|
| 257 | machine_context_t ctx;
|
---|
| 258 | info.context = &ctx;
|
---|
| 259 | processorCtx_t proc_cor_storage = { proc, &info };
|
---|
| 260 |
|
---|
[8fcbb4c] | 261 | LIB_DEBUG_PRINTF("Coroutine : created stack %p\n", proc_cor_storage.c.stack.base);
|
---|
| 262 |
|
---|
[0c92c9f] | 263 | //Set global state
|
---|
[8fcbb4c] | 264 | proc->current_coroutine = &proc->runner->c;
|
---|
[8def349] | 265 | proc->current_thread = NULL;
|
---|
| 266 |
|
---|
| 267 | //We now have a proper context from which to schedule threads
|
---|
[8fcbb4c] | 268 | LIB_DEBUG_PRINTF("Kernel : core %p created (%p, %p)\n", proc, proc->runner, &ctx);
|
---|
[8def349] | 269 |
|
---|
| 270 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't
|
---|
| 271 | // resume it to start it like it normally would, it will just context switch
|
---|
| 272 | // back to here. Instead directly call the main since we already are on the
|
---|
| 273 | // appropriate stack.
|
---|
| 274 | proc_cor_storage.c.state = Active;
|
---|
| 275 | main( &proc_cor_storage );
|
---|
[ee897e4b] | 276 | proc_cor_storage.c.state = Halted;
|
---|
[8def349] | 277 |
|
---|
[0c92c9f] | 278 | // Main routine of the core returned, the core is now fully terminated
|
---|
[8fcbb4c] | 279 | LIB_DEBUG_PRINTF("Kernel : core %p main ended (%p)\n", proc, proc->runner);
|
---|
[8def349] | 280 |
|
---|
| 281 | return NULL;
|
---|
[c84e80a] | 282 | }
|
---|
| 283 |
|
---|
[8def349] | 284 | void start(processor * this) {
|
---|
| 285 | LIB_DEBUG_PRINTF("Kernel : Starting core %p\n", this);
|
---|
| 286 |
|
---|
[8fcbb4c] | 287 | // pthread_attr_t attributes;
|
---|
| 288 | // pthread_attr_init( &attributes );
|
---|
[eb2e723] | 289 |
|
---|
[8fcbb4c] | 290 | pthread_create( &this->kernel_thread, NULL, CtxInvokeProcessor, (void*)this );
|
---|
[eb2e723] | 291 |
|
---|
[8fcbb4c] | 292 | // pthread_attr_destroy( &attributes );
|
---|
[eb2e723] | 293 |
|
---|
[8def349] | 294 | LIB_DEBUG_PRINTF("Kernel : core %p started\n", this);
|
---|
[eb2e723] | 295 | }
|
---|
| 296 |
|
---|
[8def349] | 297 | //-----------------------------------------------------------------------------
|
---|
| 298 | // Scheduler routines
|
---|
[75f3522] | 299 | void ScheduleThread( thread * thrd ) {
|
---|
[8def349] | 300 | assertf( thrd->next == NULL, "Expected null got %p", thrd->next );
|
---|
| 301 |
|
---|
[db6f06a] | 302 | lock( &systemProcessor->cltr->lock );
|
---|
[8def349] | 303 | append( &systemProcessor->cltr->ready_queue, thrd );
|
---|
[db6f06a] | 304 | unlock( &systemProcessor->cltr->lock );
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | thread * nextThread(cluster * this) {
|
---|
| 308 | lock( &this->lock );
|
---|
| 309 | thread * head = pop_head( &this->ready_queue );
|
---|
| 310 | unlock( &this->lock );
|
---|
| 311 | return head;
|
---|
[eb2e723] | 312 | }
|
---|
| 313 |
|
---|
[75f3522] | 314 | void ScheduleInternal() {
|
---|
| 315 | suspend();
|
---|
| 316 | }
|
---|
| 317 |
|
---|
[db6f06a] | 318 | void ScheduleInternal( spinlock * lock ) {
|
---|
| 319 | get_this_processor()->finish.action_code = Release;
|
---|
| 320 | get_this_processor()->finish.lock = lock;
|
---|
| 321 | suspend();
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | void ScheduleInternal( thread * thrd ) {
|
---|
| 325 | get_this_processor()->finish.action_code = Schedule;
|
---|
| 326 | get_this_processor()->finish.thrd = thrd;
|
---|
| 327 | suspend();
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | void ScheduleInternal( spinlock * lock, thread * thrd ) {
|
---|
| 331 | get_this_processor()->finish.action_code = Release_Schedule;
|
---|
| 332 | get_this_processor()->finish.lock = lock;
|
---|
| 333 | get_this_processor()->finish.thrd = thrd;
|
---|
| 334 | suspend();
|
---|
[eb2e723] | 335 | }
|
---|
| 336 |
|
---|
| 337 | //-----------------------------------------------------------------------------
|
---|
| 338 | // Kernel boot procedures
|
---|
| 339 | void kernel_startup(void) {
|
---|
| 340 | LIB_DEBUG_PRINTF("Kernel : Starting\n");
|
---|
| 341 |
|
---|
| 342 | // Start by initializing the main thread
|
---|
[8fcbb4c] | 343 | // SKULLDUGGERY: the mainThread steals the process main thread
|
---|
| 344 | // which will then be scheduled by the systemProcessor normally
|
---|
[e15df4c] | 345 | mainThread = (thread *)&mainThread_storage;
|
---|
[8fcbb4c] | 346 | current_stack_info_t info;
|
---|
[8def349] | 347 | mainThread{ &info };
|
---|
[eb2e723] | 348 |
|
---|
[bd98b58] | 349 | // Initialize the system cluster
|
---|
| 350 | systemCluster = (cluster *)&systemCluster_storage;
|
---|
| 351 | systemCluster{};
|
---|
| 352 |
|
---|
[8def349] | 353 | // Initialize the system processor and the system processor ctx
|
---|
[eb2e723] | 354 | // (the coroutine that contains the processing control flow)
|
---|
[8def349] | 355 | systemProcessor = (processor *)&systemProcessor_storage;
|
---|
| 356 | systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage };
|
---|
[eb2e723] | 357 |
|
---|
[dcb42b8] | 358 | // Add the main thread to the ready queue
|
---|
| 359 | // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread
|
---|
[75f3522] | 360 | ScheduleThread(mainThread);
|
---|
[eb2e723] | 361 |
|
---|
[dcb42b8] | 362 | //initialize the global state variables
|
---|
[bd98b58] | 363 | this_processor = systemProcessor;
|
---|
| 364 | this_processor->current_thread = mainThread;
|
---|
| 365 | this_processor->current_coroutine = &mainThread->c;
|
---|
[eb2e723] | 366 |
|
---|
[dcb42b8] | 367 | // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX
|
---|
| 368 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that
|
---|
| 369 | // mainThread is on the ready queue when this call is made.
|
---|
[8fcbb4c] | 370 | resume(systemProcessor->runner);
|
---|
[eb2e723] | 371 |
|
---|
[dcb42b8] | 372 |
|
---|
| 373 |
|
---|
| 374 | // THE SYSTEM IS NOW COMPLETELY RUNNING
|
---|
[eb2e723] | 375 | LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n");
|
---|
| 376 | }
|
---|
| 377 |
|
---|
[dcb42b8] | 378 | void kernel_shutdown(void) {
|
---|
| 379 | LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n");
|
---|
[eb2e723] | 380 |
|
---|
[dcb42b8] | 381 | // SKULLDUGGERY: Notify the systemProcessor it needs to terminates.
|
---|
| 382 | // When its coroutine terminates, it return control to the mainThread
|
---|
| 383 | // which is currently here
|
---|
[db6f06a] | 384 | systemProcessor->is_terminated = true;
|
---|
[eb2e723] | 385 | suspend();
|
---|
| 386 |
|
---|
[dcb42b8] | 387 | // THE SYSTEM IS NOW COMPLETELY STOPPED
|
---|
[eb2e723] | 388 |
|
---|
[dcb42b8] | 389 | // Destroy the system processor and its context in reverse order of construction
|
---|
| 390 | // These were manually constructed so we need manually destroy them
|
---|
[8fcbb4c] | 391 | ^(systemProcessor->runner){};
|
---|
[eb2e723] | 392 | ^(systemProcessor){};
|
---|
| 393 |
|
---|
[dcb42b8] | 394 | // Final step, destroy the main thread since it is no longer needed
|
---|
| 395 | // Since we provided a stack to this taxk it will not destroy anything
|
---|
[eb2e723] | 396 | ^(mainThread){};
|
---|
| 397 |
|
---|
| 398 | LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n");
|
---|
[8118303] | 399 | }
|
---|
| 400 |
|
---|
[bd98b58] | 401 | //-----------------------------------------------------------------------------
|
---|
| 402 | // Locks
|
---|
[db6f06a] | 403 | void ?{}( spinlock * this ) {
|
---|
| 404 | this->lock = 0;
|
---|
[bd98b58] | 405 | }
|
---|
[db6f06a] | 406 | void ^?{}( spinlock * this ) {
|
---|
[bd98b58] | 407 |
|
---|
[db6f06a] | 408 | }
|
---|
| 409 |
|
---|
| 410 | void lock( spinlock * this ) {
|
---|
| 411 | for ( unsigned int i = 1;; i += 1 ) {
|
---|
| 412 | if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break;
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
[bd98b58] | 415 |
|
---|
[db6f06a] | 416 | void unlock( spinlock * this ) {
|
---|
| 417 | __sync_lock_release_4( &this->lock );
|
---|
[bd98b58] | 418 | }
|
---|
| 419 |
|
---|
[db6f06a] | 420 | void ?{}( signal_once * this ) {
|
---|
| 421 | this->condition = false;
|
---|
| 422 | }
|
---|
| 423 | void ^?{}( signal_once * this ) {
|
---|
| 424 |
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | void wait( signal_once * this ) {
|
---|
| 428 | lock( &this->lock );
|
---|
| 429 | if( !this->condition ) {
|
---|
[8def349] | 430 | append( &this->blocked, this_thread() );
|
---|
[db6f06a] | 431 | ScheduleInternal( &this->lock );
|
---|
| 432 | lock( &this->lock );
|
---|
[8def349] | 433 | }
|
---|
[db6f06a] | 434 | unlock( &this->lock );
|
---|
[bd98b58] | 435 | }
|
---|
| 436 |
|
---|
[db6f06a] | 437 | void signal( signal_once * this ) {
|
---|
| 438 | lock( &this->lock );
|
---|
| 439 | {
|
---|
| 440 | this->condition = true;
|
---|
| 441 |
|
---|
| 442 | thread * it;
|
---|
| 443 | while( it = pop_head( &this->blocked) ) {
|
---|
| 444 | ScheduleThread( it );
|
---|
| 445 | }
|
---|
[bd98b58] | 446 | }
|
---|
[db6f06a] | 447 | unlock( &this->lock );
|
---|
[bd98b58] | 448 | }
|
---|
| 449 |
|
---|
| 450 | //-----------------------------------------------------------------------------
|
---|
| 451 | // Queues
|
---|
| 452 | void ?{}( simple_thread_list * this ) {
|
---|
| 453 | this->head = NULL;
|
---|
| 454 | this->tail = &this->head;
|
---|
| 455 | }
|
---|
| 456 |
|
---|
[e15df4c] | 457 | void append( simple_thread_list * this, thread * t ) {
|
---|
[f07e037] | 458 | assert(this->tail != NULL);
|
---|
[bd98b58] | 459 | *this->tail = t;
|
---|
| 460 | this->tail = &t->next;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
[e15df4c] | 463 | thread * pop_head( simple_thread_list * this ) {
|
---|
| 464 | thread * head = this->head;
|
---|
[bd98b58] | 465 | if( head ) {
|
---|
| 466 | this->head = head->next;
|
---|
| 467 | if( !head->next ) {
|
---|
| 468 | this->tail = &this->head;
|
---|
| 469 | }
|
---|
| 470 | head->next = NULL;
|
---|
| 471 | }
|
---|
| 472 | return head;
|
---|
| 473 | }
|
---|
[8118303] | 474 | // Local Variables: //
|
---|
| 475 | // mode: c //
|
---|
| 476 | // tab-width: 4 //
|
---|
| 477 | // End: //
|
---|