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