[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 ) { |
---|
[690f13c] | 296 | if( !thrd ) return; |
---|
| 297 | |
---|
[8def349] | 298 | assertf( thrd->next == NULL, "Expected null got %p", thrd->next ); |
---|
| 299 | |
---|
[db6f06a] | 300 | lock( &systemProcessor->cltr->lock ); |
---|
[8def349] | 301 | append( &systemProcessor->cltr->ready_queue, thrd ); |
---|
[db6f06a] | 302 | unlock( &systemProcessor->cltr->lock ); |
---|
| 303 | } |
---|
| 304 | |
---|
[348006f] | 305 | thread_desc * nextThread(cluster * this) { |
---|
[db6f06a] | 306 | lock( &this->lock ); |
---|
[348006f] | 307 | thread_desc * head = pop_head( &this->ready_queue ); |
---|
[db6f06a] | 308 | unlock( &this->lock ); |
---|
| 309 | return head; |
---|
[eb2e723] | 310 | } |
---|
| 311 | |
---|
[75f3522] | 312 | void ScheduleInternal() { |
---|
| 313 | suspend(); |
---|
| 314 | } |
---|
| 315 | |
---|
[db6f06a] | 316 | void ScheduleInternal( spinlock * lock ) { |
---|
[89a3df5] | 317 | this_processor->finish.action_code = Release; |
---|
| 318 | this_processor->finish.lock = lock; |
---|
[db6f06a] | 319 | suspend(); |
---|
| 320 | } |
---|
| 321 | |
---|
[348006f] | 322 | void ScheduleInternal( thread_desc * thrd ) { |
---|
[89a3df5] | 323 | this_processor->finish.action_code = Schedule; |
---|
| 324 | this_processor->finish.thrd = thrd; |
---|
[db6f06a] | 325 | suspend(); |
---|
| 326 | } |
---|
| 327 | |
---|
[348006f] | 328 | void ScheduleInternal( spinlock * lock, thread_desc * thrd ) { |
---|
[89a3df5] | 329 | this_processor->finish.action_code = Release_Schedule; |
---|
| 330 | this_processor->finish.lock = lock; |
---|
| 331 | this_processor->finish.thrd = thrd; |
---|
[db6f06a] | 332 | suspend(); |
---|
[eb2e723] | 333 | } |
---|
| 334 | |
---|
| 335 | //----------------------------------------------------------------------------- |
---|
| 336 | // Kernel boot procedures |
---|
| 337 | void kernel_startup(void) { |
---|
| 338 | LIB_DEBUG_PRINTF("Kernel : Starting\n"); |
---|
| 339 | |
---|
| 340 | // Start by initializing the main thread |
---|
[8fcbb4c] | 341 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
| 342 | // which will then be scheduled by the systemProcessor normally |
---|
[348006f] | 343 | mainThread = (thread_desc *)&mainThread_storage; |
---|
[8fcbb4c] | 344 | current_stack_info_t info; |
---|
[8def349] | 345 | mainThread{ &info }; |
---|
[eb2e723] | 346 | |
---|
[bd98b58] | 347 | // Initialize the system cluster |
---|
| 348 | systemCluster = (cluster *)&systemCluster_storage; |
---|
| 349 | systemCluster{}; |
---|
| 350 | |
---|
[8def349] | 351 | // Initialize the system processor and the system processor ctx |
---|
[eb2e723] | 352 | // (the coroutine that contains the processing control flow) |
---|
[8def349] | 353 | systemProcessor = (processor *)&systemProcessor_storage; |
---|
| 354 | systemProcessor{ systemCluster, (processorCtx_t *)&systemProcessorCtx_storage }; |
---|
[eb2e723] | 355 | |
---|
[dcb42b8] | 356 | // Add the main thread to the ready queue |
---|
| 357 | // once resume is called on systemProcessor->ctx the mainThread needs to be scheduled like any normal thread |
---|
[75f3522] | 358 | ScheduleThread(mainThread); |
---|
[eb2e723] | 359 | |
---|
[dcb42b8] | 360 | //initialize the global state variables |
---|
[bd98b58] | 361 | this_processor = systemProcessor; |
---|
| 362 | this_processor->current_thread = mainThread; |
---|
[17af7d1] | 363 | this_processor->current_coroutine = &mainThread->cor; |
---|
[eb2e723] | 364 | |
---|
[dcb42b8] | 365 | // SKULLDUGGERY: Force a context switch to the system processor to set the main thread's context to the current UNIX |
---|
| 366 | // context. Hence, the main thread does not begin through CtxInvokeThread, like all other threads. The trick here is that |
---|
| 367 | // mainThread is on the ready queue when this call is made. |
---|
[8fcbb4c] | 368 | resume(systemProcessor->runner); |
---|
[eb2e723] | 369 | |
---|
[dcb42b8] | 370 | |
---|
| 371 | |
---|
| 372 | // THE SYSTEM IS NOW COMPLETELY RUNNING |
---|
[eb2e723] | 373 | LIB_DEBUG_PRINTF("Kernel : Started\n--------------------------------------------------\n\n"); |
---|
| 374 | } |
---|
| 375 | |
---|
[dcb42b8] | 376 | void kernel_shutdown(void) { |
---|
| 377 | LIB_DEBUG_PRINTF("\n--------------------------------------------------\nKernel : Shutting down\n"); |
---|
[eb2e723] | 378 | |
---|
[dcb42b8] | 379 | // SKULLDUGGERY: Notify the systemProcessor it needs to terminates. |
---|
| 380 | // When its coroutine terminates, it return control to the mainThread |
---|
| 381 | // which is currently here |
---|
[db6f06a] | 382 | systemProcessor->is_terminated = true; |
---|
[eb2e723] | 383 | suspend(); |
---|
| 384 | |
---|
[dcb42b8] | 385 | // THE SYSTEM IS NOW COMPLETELY STOPPED |
---|
[eb2e723] | 386 | |
---|
[dcb42b8] | 387 | // Destroy the system processor and its context in reverse order of construction |
---|
| 388 | // These were manually constructed so we need manually destroy them |
---|
[8fcbb4c] | 389 | ^(systemProcessor->runner){}; |
---|
[eb2e723] | 390 | ^(systemProcessor){}; |
---|
| 391 | |
---|
[dcb42b8] | 392 | // Final step, destroy the main thread since it is no longer needed |
---|
| 393 | // Since we provided a stack to this taxk it will not destroy anything |
---|
[eb2e723] | 394 | ^(mainThread){}; |
---|
| 395 | |
---|
| 396 | LIB_DEBUG_PRINTF("Kernel : Shutdown complete\n"); |
---|
[8118303] | 397 | } |
---|
| 398 | |
---|
[bd98b58] | 399 | //----------------------------------------------------------------------------- |
---|
| 400 | // Locks |
---|
[db6f06a] | 401 | void ?{}( spinlock * this ) { |
---|
| 402 | this->lock = 0; |
---|
[bd98b58] | 403 | } |
---|
[db6f06a] | 404 | void ^?{}( spinlock * this ) { |
---|
[bd98b58] | 405 | |
---|
[db6f06a] | 406 | } |
---|
| 407 | |
---|
| 408 | void lock( spinlock * this ) { |
---|
| 409 | for ( unsigned int i = 1;; i += 1 ) { |
---|
| 410 | if ( this->lock == 0 && __sync_lock_test_and_set_4( &this->lock, 1 ) == 0 ) break; |
---|
| 411 | } |
---|
| 412 | } |
---|
[bd98b58] | 413 | |
---|
[db6f06a] | 414 | void unlock( spinlock * this ) { |
---|
| 415 | __sync_lock_release_4( &this->lock ); |
---|
[bd98b58] | 416 | } |
---|
| 417 | |
---|
[db6f06a] | 418 | void ?{}( signal_once * this ) { |
---|
| 419 | this->condition = false; |
---|
| 420 | } |
---|
| 421 | void ^?{}( signal_once * this ) { |
---|
| 422 | |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | void wait( signal_once * this ) { |
---|
| 426 | lock( &this->lock ); |
---|
| 427 | if( !this->condition ) { |
---|
[8def349] | 428 | append( &this->blocked, this_thread() ); |
---|
[db6f06a] | 429 | ScheduleInternal( &this->lock ); |
---|
| 430 | lock( &this->lock ); |
---|
[8def349] | 431 | } |
---|
[db6f06a] | 432 | unlock( &this->lock ); |
---|
[bd98b58] | 433 | } |
---|
| 434 | |
---|
[db6f06a] | 435 | void signal( signal_once * this ) { |
---|
| 436 | lock( &this->lock ); |
---|
| 437 | { |
---|
| 438 | this->condition = true; |
---|
| 439 | |
---|
[348006f] | 440 | thread_desc * it; |
---|
[db6f06a] | 441 | while( it = pop_head( &this->blocked) ) { |
---|
| 442 | ScheduleThread( it ); |
---|
| 443 | } |
---|
[bd98b58] | 444 | } |
---|
[db6f06a] | 445 | unlock( &this->lock ); |
---|
[bd98b58] | 446 | } |
---|
| 447 | |
---|
| 448 | //----------------------------------------------------------------------------- |
---|
| 449 | // Queues |
---|
| 450 | void ?{}( simple_thread_list * this ) { |
---|
| 451 | this->head = NULL; |
---|
| 452 | this->tail = &this->head; |
---|
| 453 | } |
---|
| 454 | |
---|
[348006f] | 455 | void append( simple_thread_list * this, thread_desc * t ) { |
---|
[f07e037] | 456 | assert(this->tail != NULL); |
---|
[bd98b58] | 457 | *this->tail = t; |
---|
| 458 | this->tail = &t->next; |
---|
| 459 | } |
---|
| 460 | |
---|
[348006f] | 461 | thread_desc * pop_head( simple_thread_list * this ) { |
---|
| 462 | thread_desc * head = this->head; |
---|
[bd98b58] | 463 | if( head ) { |
---|
| 464 | this->head = head->next; |
---|
| 465 | if( !head->next ) { |
---|
| 466 | this->tail = &this->head; |
---|
| 467 | } |
---|
| 468 | head->next = NULL; |
---|
| 469 | } |
---|
| 470 | return head; |
---|
| 471 | } |
---|
[690f13c] | 472 | |
---|
| 473 | void ?{}( simple_thread_stack * this ) { |
---|
| 474 | this->top = NULL; |
---|
| 475 | } |
---|
| 476 | |
---|
| 477 | void push( simple_thread_stack * this, thread_desc * t ) { |
---|
| 478 | assert(t->next != NULL); |
---|
| 479 | t->next = this->top; |
---|
| 480 | this->top = t; |
---|
| 481 | } |
---|
| 482 | |
---|
| 483 | thread_desc * pop( simple_thread_stack * this ) { |
---|
| 484 | thread_desc * top = this->top; |
---|
| 485 | if( top ) { |
---|
| 486 | this->top = top->next; |
---|
| 487 | top->next = NULL; |
---|
| 488 | } |
---|
| 489 | return top; |
---|
| 490 | } |
---|
[8118303] | 491 | // Local Variables: // |
---|
| 492 | // mode: c // |
---|
| 493 | // tab-width: 4 // |
---|
| 494 | // End: // |
---|