[8118303] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // kernel.c -- |
---|
| 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
[75f3522] | 10 | // Created On : Tue Jan 17 12:27:26 2017 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[ada0246d] | 12 | // Last Modified On : Tue May 26 22:05:19 2020 |
---|
| 13 | // Update Count : 59 |
---|
[8118303] | 14 | // |
---|
| 15 | |
---|
[2026bb6] | 16 | #define __cforall_thread__ |
---|
[4069faad] | 17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__ |
---|
[2026bb6] | 18 | |
---|
[8118303] | 19 | //C Includes |
---|
[c84e80a] | 20 | #include <stddef.h> |
---|
[214e8da] | 21 | #include <errno.h> |
---|
[ea8b2f7] | 22 | #include <string.h> |
---|
[9d944b2] | 23 | #include <stdio.h> |
---|
[8fcbb4c] | 24 | #include <fenv.h> |
---|
[58b6d1b] | 25 | #include <signal.h> |
---|
[9d944b2] | 26 | #include <unistd.h> |
---|
[27f5f71] | 27 | #include <limits.h> // PTHREAD_STACK_MIN |
---|
[1a3040c] | 28 | #include <sys/mman.h> // mprotect |
---|
[ada0246d] | 29 | extern "C" { |
---|
| 30 | #include <sys/resource.h> |
---|
[eb2e723] | 31 | } |
---|
[8118303] | 32 | |
---|
| 33 | //CFA Includes |
---|
[58b6d1b] | 34 | #include "time.hfa" |
---|
[73abe95] | 35 | #include "kernel_private.hfa" |
---|
| 36 | #include "preemption.hfa" |
---|
| 37 | #include "startup.hfa" |
---|
[8118303] | 38 | |
---|
| 39 | //Private includes |
---|
| 40 | #define __CFA_INVOKE_PRIVATE__ |
---|
| 41 | #include "invoke.h" |
---|
| 42 | |
---|
[4069faad] | 43 | |
---|
[deca0f5] | 44 | //----------------------------------------------------------------------------- |
---|
| 45 | // Some assembly required |
---|
[1805b1b] | 46 | #if defined( __i386 ) |
---|
[deca0f5] | 47 | #define CtxGet( ctx ) \ |
---|
| 48 | __asm__ volatile ( \ |
---|
| 49 | "movl %%esp,%0\n"\ |
---|
| 50 | "movl %%ebp,%1\n"\ |
---|
| 51 | : "=rm" (ctx.SP),\ |
---|
| 52 | "=rm" (ctx.FP) \ |
---|
| 53 | ) |
---|
| 54 | |
---|
| 55 | // mxcr : SSE Status and Control bits (control bits are preserved across function calls) |
---|
| 56 | // fcw : X87 FPU control word (preserved across function calls) |
---|
| 57 | #define __x87_store \ |
---|
| 58 | uint32_t __mxcr; \ |
---|
| 59 | uint16_t __fcw; \ |
---|
| 60 | __asm__ volatile ( \ |
---|
| 61 | "stmxcsr %0\n" \ |
---|
| 62 | "fnstcw %1\n" \ |
---|
| 63 | : "=m" (__mxcr),\ |
---|
| 64 | "=m" (__fcw) \ |
---|
| 65 | ) |
---|
| 66 | |
---|
| 67 | #define __x87_load \ |
---|
| 68 | __asm__ volatile ( \ |
---|
| 69 | "fldcw %1\n" \ |
---|
| 70 | "ldmxcsr %0\n" \ |
---|
| 71 | ::"m" (__mxcr),\ |
---|
| 72 | "m" (__fcw) \ |
---|
| 73 | ) |
---|
| 74 | |
---|
| 75 | #elif defined( __x86_64 ) |
---|
| 76 | #define CtxGet( ctx ) \ |
---|
| 77 | __asm__ volatile ( \ |
---|
| 78 | "movq %%rsp,%0\n"\ |
---|
| 79 | "movq %%rbp,%1\n"\ |
---|
| 80 | : "=rm" (ctx.SP),\ |
---|
| 81 | "=rm" (ctx.FP) \ |
---|
| 82 | ) |
---|
| 83 | |
---|
| 84 | #define __x87_store \ |
---|
| 85 | uint32_t __mxcr; \ |
---|
| 86 | uint16_t __fcw; \ |
---|
| 87 | __asm__ volatile ( \ |
---|
| 88 | "stmxcsr %0\n" \ |
---|
| 89 | "fnstcw %1\n" \ |
---|
| 90 | : "=m" (__mxcr),\ |
---|
| 91 | "=m" (__fcw) \ |
---|
| 92 | ) |
---|
| 93 | |
---|
| 94 | #define __x87_load \ |
---|
| 95 | __asm__ volatile ( \ |
---|
| 96 | "fldcw %1\n" \ |
---|
| 97 | "ldmxcsr %0\n" \ |
---|
| 98 | :: "m" (__mxcr),\ |
---|
| 99 | "m" (__fcw) \ |
---|
| 100 | ) |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | #elif defined( __ARM_ARCH ) |
---|
| 104 | #define CtxGet( ctx ) __asm__ ( \ |
---|
| 105 | "mov %0,%%sp\n" \ |
---|
| 106 | "mov %1,%%r11\n" \ |
---|
| 107 | : "=rm" (ctx.SP), "=rm" (ctx.FP) ) |
---|
| 108 | #else |
---|
| 109 | #error unknown hardware architecture |
---|
| 110 | #endif |
---|
| 111 | |
---|
| 112 | //----------------------------------------------------------------------------- |
---|
[2ac095d] | 113 | //Start and stop routine for the kernel, declared first to make sure they run first |
---|
[8c50aed] | 114 | static void __kernel_startup (void) __attribute__(( constructor( STARTUP_PRIORITY_KERNEL ) )); |
---|
| 115 | static void __kernel_shutdown(void) __attribute__(( destructor ( STARTUP_PRIORITY_KERNEL ) )); |
---|
[2ac095d] | 116 | |
---|
[92e7631] | 117 | //----------------------------------------------------------------------------- |
---|
| 118 | // Kernel Scheduling logic |
---|
| 119 | static $thread * __next_thread(cluster * this); |
---|
[64a7146] | 120 | static bool __has_next_thread(cluster * this); |
---|
[92e7631] | 121 | static void __run_thread(processor * this, $thread * dst); |
---|
| 122 | static bool __wake_proc(processor *); |
---|
[64a7146] | 123 | static bool __wake_one(struct __processor_id_t * id, cluster * cltr); |
---|
| 124 | static void __halt(processor * this); |
---|
[2ac095d] | 125 | |
---|
[8def349] | 126 | //----------------------------------------------------------------------------- |
---|
| 127 | // Kernel storage |
---|
[b388ee81] | 128 | KERNEL_STORAGE(cluster, mainCluster); |
---|
| 129 | KERNEL_STORAGE(processor, mainProcessor); |
---|
| 130 | KERNEL_STORAGE($thread, mainThread); |
---|
| 131 | KERNEL_STORAGE(__stack_t, mainThreadCtx); |
---|
| 132 | KERNEL_STORAGE(__scheduler_RWLock_t, __scheduler_lock); |
---|
[8834751] | 133 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 134 | KERNEL_STORAGE(__stats_t, mainProcStats); |
---|
| 135 | #endif |
---|
[8def349] | 136 | |
---|
[b388ee81] | 137 | cluster * mainCluster; |
---|
| 138 | processor * mainProcessor; |
---|
| 139 | $thread * mainThread; |
---|
| 140 | __scheduler_RWLock_t * __scheduler_lock; |
---|
[eb2e723] | 141 | |
---|
[ea8b2f7] | 142 | extern "C" { |
---|
[1805b1b] | 143 | struct { __dllist_t(cluster) list; __spinlock_t lock; } __cfa_dbg_global_clusters; |
---|
[ea8b2f7] | 144 | } |
---|
[de94a60] | 145 | |
---|
[b2f6113] | 146 | size_t __page_size = 0; |
---|
| 147 | |
---|
[bd98b58] | 148 | //----------------------------------------------------------------------------- |
---|
| 149 | // Global state |
---|
[afc2427] | 150 | thread_local struct KernelThreadData kernelTLS __attribute__ ((tls_model ( "initial-exec" ))) = { |
---|
[1805b1b] | 151 | NULL, // cannot use 0p |
---|
[b10affd] | 152 | NULL, |
---|
[8834751] | 153 | NULL, |
---|
[21184e3] | 154 | { 1, false, false }, |
---|
| 155 | 6u //this should be seeded better but due to a bug calling rdtsc doesn't work |
---|
[b10affd] | 156 | }; |
---|
[c84e80a] | 157 | |
---|
| 158 | //----------------------------------------------------------------------------- |
---|
[de6319f] | 159 | // Struct to steal stack |
---|
[8def349] | 160 | struct current_stack_info_t { |
---|
[1805b1b] | 161 | __stack_t * storage; // pointer to stack object |
---|
| 162 | void * base; // base of stack |
---|
| 163 | void * limit; // stack grows towards stack limit |
---|
| 164 | void * context; // address of cfa_context_t |
---|
[c84e80a] | 165 | }; |
---|
| 166 | |
---|
[242a902] | 167 | void ?{}( current_stack_info_t & this ) { |
---|
[b2f6113] | 168 | __stack_context_t ctx; |
---|
| 169 | CtxGet( ctx ); |
---|
| 170 | this.base = ctx.FP; |
---|
[8def349] | 171 | |
---|
| 172 | rlimit r; |
---|
[132fad4] | 173 | getrlimit( RLIMIT_STACK, &r); |
---|
[69a61d2] | 174 | size_t size = r.rlim_cur; |
---|
[8def349] | 175 | |
---|
[69a61d2] | 176 | this.limit = (void *)(((intptr_t)this.base) - size); |
---|
[9236060] | 177 | this.context = &storage_mainThreadCtx; |
---|
[8def349] | 178 | } |
---|
| 179 | |
---|
[de6319f] | 180 | //----------------------------------------------------------------------------- |
---|
| 181 | // Main thread construction |
---|
[8def349] | 182 | |
---|
[ac2b598] | 183 | void ?{}( $coroutine & this, current_stack_info_t * info) with( this ) { |
---|
[b2f6113] | 184 | stack.storage = info->storage; |
---|
| 185 | with(*stack.storage) { |
---|
| 186 | limit = info->limit; |
---|
| 187 | base = info->base; |
---|
| 188 | } |
---|
[ffe2fad] | 189 | __attribute__((may_alias)) intptr_t * istorage = (intptr_t*) &stack.storage; |
---|
| 190 | *istorage |= 0x1; |
---|
[65deb18] | 191 | name = "Main Thread"; |
---|
| 192 | state = Start; |
---|
[1805b1b] | 193 | starter = 0p; |
---|
| 194 | last = 0p; |
---|
| 195 | cancellation = 0p; |
---|
[8def349] | 196 | } |
---|
| 197 | |
---|
[ac2b598] | 198 | void ?{}( $thread & this, current_stack_info_t * info) with( this ) { |
---|
[ff79d5e] | 199 | ticket = 1; |
---|
[e8e457e] | 200 | state = Start; |
---|
[65deb18] | 201 | self_cor{ info }; |
---|
[82c948c] | 202 | curr_cor = &self_cor; |
---|
[de6319f] | 203 | curr_cluster = mainCluster; |
---|
[82c948c] | 204 | self_mon.owner = &this; |
---|
| 205 | self_mon.recursion = 1; |
---|
| 206 | self_mon_p = &self_mon; |
---|
[b798713] | 207 | link.next = 0p; |
---|
| 208 | link.prev = 0p; |
---|
[de94a60] | 209 | |
---|
[1805b1b] | 210 | node.next = 0p; |
---|
| 211 | node.prev = 0p; |
---|
[a1a17a7] | 212 | doregister(curr_cluster, this); |
---|
[82c948c] | 213 | |
---|
| 214 | monitors{ &self_mon_p, 1, (fptr_t)0 }; |
---|
[8def349] | 215 | } |
---|
[c84e80a] | 216 | |
---|
[8def349] | 217 | //----------------------------------------------------------------------------- |
---|
| 218 | // Processor coroutine |
---|
[de6319f] | 219 | void ?{}(processorCtx_t & this) { |
---|
[39fea2f] | 220 | |
---|
[8def349] | 221 | } |
---|
| 222 | |
---|
[39fea2f] | 223 | // Construct the processor context of non-main processors |
---|
[c29c342] | 224 | static void ?{}(processorCtx_t & this, processor * proc, current_stack_info_t * info) { |
---|
[242a902] | 225 | (this.__cor){ info }; |
---|
| 226 | this.proc = proc; |
---|
[8def349] | 227 | } |
---|
| 228 | |
---|
[c7a900a] | 229 | static void * __invoke_processor(void * arg); |
---|
[8c50aed] | 230 | |
---|
[13c5e19] | 231 | void ?{}(processor & this, const char name[], cluster & _cltr) with( this ) { |
---|
[de6319f] | 232 | this.name = name; |
---|
[13c5e19] | 233 | this.cltr = &_cltr; |
---|
[7768b8d] | 234 | id = -1u; |
---|
[c40e7c5] | 235 | terminated{ 0 }; |
---|
[b0c7419] | 236 | destroyer = 0p; |
---|
[c40e7c5] | 237 | do_terminate = false; |
---|
[1805b1b] | 238 | preemption_alarm = 0p; |
---|
[c40e7c5] | 239 | pending_preemption = false; |
---|
[094476d] | 240 | runner.proc = &this; |
---|
[8def349] | 241 | |
---|
[c34ebf2] | 242 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[69fbc61] | 243 | print_stats = 0; |
---|
[c34ebf2] | 244 | print_halts = false; |
---|
| 245 | #endif |
---|
| 246 | |
---|
[92e7631] | 247 | idle{}; |
---|
[6b4cdd3] | 248 | |
---|
[4069faad] | 249 | __cfadbg_print_safe(runtime_core, "Kernel : Starting core %p\n", &this); |
---|
[8c50aed] | 250 | |
---|
[c7a900a] | 251 | this.stack = __create_pthread( &this.kernel_thread, __invoke_processor, (void *)&this ); |
---|
[13c5e19] | 252 | __atomic_fetch_add( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST ); |
---|
[8c50aed] | 253 | |
---|
[4069faad] | 254 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created\n", &this); |
---|
[c84e80a] | 255 | } |
---|
| 256 | |
---|
[65deb18] | 257 | void ^?{}(processor & this) with( this ){ |
---|
[ea8b2f7] | 258 | if( ! __atomic_load_n(&do_terminate, __ATOMIC_ACQUIRE) ) { |
---|
[4069faad] | 259 | __cfadbg_print_safe(runtime_core, "Kernel : core %p signaling termination\n", &this); |
---|
[85b1deb] | 260 | |
---|
| 261 | __atomic_store_n(&do_terminate, true, __ATOMIC_RELAXED); |
---|
[92e7631] | 262 | __wake_proc( &this ); |
---|
[85b1deb] | 263 | |
---|
[65deb18] | 264 | P( terminated ); |
---|
[14a61b5] | 265 | verify( kernelTLS.this_processor != &this); |
---|
[8def349] | 266 | } |
---|
[6b4cdd3] | 267 | |
---|
[c66f6cb] | 268 | int err = pthread_join( kernel_thread, 0p ); |
---|
| 269 | if( err != 0 ) abort("KERNEL ERROR: joining processor %p caused error %s\n", &this, strerror(err)); |
---|
| 270 | |
---|
[27f5f71] | 271 | free( this.stack ); |
---|
[13c5e19] | 272 | |
---|
| 273 | __atomic_fetch_sub( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST ); |
---|
[8def349] | 274 | } |
---|
| 275 | |
---|
[dd4e2d7] | 276 | void ?{}(cluster & this, const char name[], Duration preemption_rate, unsigned io_flags) with( this ) { |
---|
[de6319f] | 277 | this.name = name; |
---|
| 278 | this.preemption_rate = preemption_rate; |
---|
[13c5e19] | 279 | this.nprocessors = 0; |
---|
[65deb18] | 280 | ready_queue{}; |
---|
[de94a60] | 281 | |
---|
[038be32] | 282 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[69fbc61] | 283 | print_stats = 0; |
---|
[8834751] | 284 | stats = alloc(); |
---|
| 285 | __init_stats( stats ); |
---|
[038be32] | 286 | #endif |
---|
| 287 | |
---|
[a1a17a7] | 288 | threads{ __get }; |
---|
[de94a60] | 289 | |
---|
[b6f2b21] | 290 | __kernel_io_startup( this, io_flags, &this == mainCluster ); |
---|
[92976d9] | 291 | |
---|
[de94a60] | 292 | doregister(this); |
---|
[8def349] | 293 | } |
---|
| 294 | |
---|
[242a902] | 295 | void ^?{}(cluster & this) { |
---|
[f6660520] | 296 | __kernel_io_shutdown( this, &this == mainCluster ); |
---|
[92976d9] | 297 | |
---|
[8834751] | 298 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[69fbc61] | 299 | if( 0 != this.print_stats ) { |
---|
| 300 | __print_stats( this.stats, this.print_stats, true, this.name, (void*)&this ); |
---|
[8834751] | 301 | } |
---|
| 302 | free( this.stats ); |
---|
| 303 | #endif |
---|
| 304 | |
---|
[de94a60] | 305 | unregister(this); |
---|
[c84e80a] | 306 | } |
---|
| 307 | |
---|
[75f3522] | 308 | //============================================================================================= |
---|
| 309 | // Kernel Scheduling logic |
---|
| 310 | //============================================================================================= |
---|
[8fcbb4c] | 311 | //Main of the processor contexts |
---|
[83a071f9] | 312 | void main(processorCtx_t & runner) { |
---|
[21184e3] | 313 | // Because of a bug, we couldn't initialized the seed on construction |
---|
| 314 | // Do it here |
---|
[7768b8d] | 315 | kernelTLS.rand_seed ^= rdtscl(); |
---|
[21184e3] | 316 | |
---|
[83a071f9] | 317 | processor * this = runner.proc; |
---|
[094476d] | 318 | verify(this); |
---|
[c81ebf9] | 319 | |
---|
[4069faad] | 320 | __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this); |
---|
[8118303] | 321 | |
---|
[7768b8d] | 322 | // register the processor unless it's the main thread which is handled in the boot sequence |
---|
[b798713] | 323 | if(this != mainProcessor) { |
---|
[9b1dcc2] | 324 | this->id = doregister((__processor_id_t*)this); |
---|
[f2b18d01] | 325 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 326 | if( this->print_halts ) { |
---|
| 327 | __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this); |
---|
| 328 | } |
---|
| 329 | #endif |
---|
| 330 | |
---|
[64a7146] | 331 | // Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
| 332 | uint_fast32_t last_size = ready_mutate_lock(); |
---|
| 333 | |
---|
| 334 | // Adjust the ready queue size |
---|
| 335 | ready_queue_grow( this->cltr ); |
---|
| 336 | |
---|
| 337 | // Unlock the RWlock |
---|
| 338 | ready_mutate_unlock( last_size ); |
---|
[b798713] | 339 | } |
---|
| 340 | |
---|
[75f3522] | 341 | { |
---|
[c81ebf9] | 342 | // Setup preemption data |
---|
| 343 | preemption_scope scope = { this }; |
---|
| 344 | |
---|
[4069faad] | 345 | __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this); |
---|
[8118303] | 346 | |
---|
[ac2b598] | 347 | $thread * readyThread = 0p; |
---|
[1a3040c] | 348 | for( unsigned int spin_count = 0; ! __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST); spin_count++ ) { |
---|
[92e7631] | 349 | // Try to get the next thread |
---|
[8c50aed] | 350 | readyThread = __next_thread( this->cltr ); |
---|
[75f3522] | 351 | |
---|
[92e7631] | 352 | // Check if we actually found a thread |
---|
| 353 | if( readyThread ) { |
---|
[3381ed7] | 354 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[92e7631] | 355 | /* paranoid */ verifyf( readyThread->state == Ready || readyThread->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", readyThread->state, readyThread->preempted); |
---|
[504a7dc] | 356 | /* paranoid */ verifyf( readyThread->link.next == 0p, "Expected null got %p", readyThread->link.next ); |
---|
[04b5cef] | 357 | __builtin_prefetch( readyThread->context.SP ); |
---|
[4e6fb8e] | 358 | |
---|
[92e7631] | 359 | // We found a thread run it |
---|
[8c50aed] | 360 | __run_thread(this, readyThread); |
---|
[c81ebf9] | 361 | |
---|
[3381ed7] | 362 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[c81ebf9] | 363 | } |
---|
[64a7146] | 364 | else { |
---|
| 365 | // Block until a thread is ready |
---|
| 366 | __halt(this); |
---|
| 367 | } |
---|
[c81ebf9] | 368 | } |
---|
| 369 | |
---|
[4069faad] | 370 | __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this); |
---|
[c84e80a] | 371 | } |
---|
[8118303] | 372 | |
---|
[4cedd9f] | 373 | V( this->terminated ); |
---|
[bdeba0b] | 374 | |
---|
[7768b8d] | 375 | // unregister the processor unless it's the main thread which is handled in the boot sequence |
---|
[b798713] | 376 | if(this != mainProcessor) { |
---|
[64a7146] | 377 | // Lock the RWlock so no-one pushes/pops while we are changing the queue |
---|
| 378 | uint_fast32_t last_size = ready_mutate_lock(); |
---|
| 379 | |
---|
| 380 | // Adjust the ready queue size |
---|
| 381 | ready_queue_shrink( this->cltr ); |
---|
| 382 | |
---|
| 383 | // Make sure we aren't on the idle queue |
---|
[68f36f4] | 384 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 385 | bool removed = |
---|
| 386 | #endif |
---|
[64a7146] | 387 | unsafe_remove( this->cltr->idles, this ); |
---|
[68f36f4] | 388 | |
---|
| 389 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 390 | if(removed) __tls_stats()->ready.sleep.exits++; |
---|
| 391 | #endif |
---|
[64a7146] | 392 | |
---|
| 393 | // Unlock the RWlock |
---|
| 394 | ready_mutate_unlock( last_size ); |
---|
| 395 | |
---|
| 396 | // Finally we don't need the read_lock any more |
---|
[9b1dcc2] | 397 | unregister((__processor_id_t*)this); |
---|
[b798713] | 398 | } |
---|
[6a490b2] | 399 | else { |
---|
| 400 | // HACK : the coroutine context switch expects this_thread to be set |
---|
| 401 | // and it make sense for it to be set in all other cases except here |
---|
| 402 | // fake it |
---|
| 403 | kernelTLS.this_thread = mainThread; |
---|
| 404 | } |
---|
[7768b8d] | 405 | |
---|
[4069faad] | 406 | __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this); |
---|
[c84e80a] | 407 | } |
---|
| 408 | |
---|
[5c1a531] | 409 | static int * __volatile_errno() __attribute__((noinline)); |
---|
| 410 | static int * __volatile_errno() { asm(""); return &errno; } |
---|
| 411 | |
---|
[14a61b5] | 412 | // KERNEL ONLY |
---|
[1c273d0] | 413 | // runThread runs a thread by context switching |
---|
| 414 | // from the processor coroutine to the target thread |
---|
[ac2b598] | 415 | static void __run_thread(processor * this, $thread * thrd_dst) { |
---|
| 416 | $coroutine * proc_cor = get_coroutine(this->runner); |
---|
[8fcbb4c] | 417 | |
---|
[14a61b5] | 418 | // Update global state |
---|
[e8e457e] | 419 | kernelTLS.this_thread = thrd_dst; |
---|
| 420 | |
---|
[9f575ea] | 421 | // set state of processor coroutine to inactive |
---|
| 422 | verify(proc_cor->state == Active); |
---|
[ae7be7a] | 423 | proc_cor->state = Blocked; |
---|
[e8e457e] | 424 | |
---|
[9f575ea] | 425 | // Actually run the thread |
---|
[3381ed7] | 426 | RUNNING: while(true) { |
---|
[ff79d5e] | 427 | thrd_dst->preempted = __NO_PREEMPTION; |
---|
| 428 | thrd_dst->state = Active; |
---|
[e8e457e] | 429 | |
---|
[ae66348] | 430 | __cfaabi_dbg_debug_do( |
---|
[276ae57e] | 431 | thrd_dst->park_stale = true; |
---|
| 432 | thrd_dst->unpark_stale = true; |
---|
[ae66348] | 433 | ) |
---|
[75f3522] | 434 | |
---|
[3381ed7] | 435 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[a7b486b] | 436 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst ); |
---|
[210b8b3] | 437 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
| 438 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit) || thrd_dst->curr_cor == proc_cor, "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); // add escape condition if we are setting up the processor |
---|
[3381ed7] | 439 | |
---|
[9f575ea] | 440 | // set context switch to the thread that the processor is executing |
---|
| 441 | verify( thrd_dst->context.SP ); |
---|
[c7a900a] | 442 | __cfactx_switch( &proc_cor->context, &thrd_dst->context ); |
---|
| 443 | // when __cfactx_switch returns we are back in the processor coroutine |
---|
[9f575ea] | 444 | |
---|
[210b8b3] | 445 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) > ((uintptr_t)__get_stack(thrd_dst->curr_cor)->limit), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too large.\n", thrd_dst ); |
---|
| 446 | /* paranoid */ verifyf( ((uintptr_t)thrd_dst->context.SP) < ((uintptr_t)__get_stack(thrd_dst->curr_cor)->base ), "ERROR : Destination $thread %p has been corrupted.\n StackPointer too small.\n", thrd_dst ); |
---|
[a7b486b] | 447 | /* paranoid */ verify( kernelTLS.this_thread == thrd_dst ); |
---|
[3381ed7] | 448 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[75f3522] | 449 | |
---|
[3381ed7] | 450 | |
---|
| 451 | // We just finished running a thread, there are a few things that could have happened. |
---|
| 452 | // 1 - Regular case : the thread has blocked and now one has scheduled it yet. |
---|
| 453 | // 2 - Racy case : the thread has blocked but someone has already tried to schedule it. |
---|
| 454 | // 4 - Preempted |
---|
| 455 | // In case 1, we may have won a race so we can't write to the state again. |
---|
| 456 | // In case 2, we lost the race so we now own the thread. |
---|
| 457 | |
---|
| 458 | if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) { |
---|
| 459 | // The thread was preempted, reschedule it and reset the flag |
---|
[9b1dcc2] | 460 | __schedule_thread( (__processor_id_t*)this, thrd_dst ); |
---|
[3381ed7] | 461 | break RUNNING; |
---|
| 462 | } |
---|
[75f3522] | 463 | |
---|
[ff79d5e] | 464 | if(unlikely(thrd_dst->state == Halted)) { |
---|
| 465 | // The thread has halted, it should never be scheduled/run again |
---|
| 466 | // We may need to wake someone up here since |
---|
| 467 | unpark( this->destroyer __cfaabi_dbg_ctx2 ); |
---|
| 468 | this->destroyer = 0p; |
---|
| 469 | break RUNNING; |
---|
| 470 | } |
---|
| 471 | |
---|
| 472 | /* paranoid */ verify( thrd_dst->state == Active ); |
---|
| 473 | thrd_dst->state = Blocked; |
---|
| 474 | |
---|
[3381ed7] | 475 | // set state of processor coroutine to active and the thread to inactive |
---|
[ff79d5e] | 476 | int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST); |
---|
| 477 | __cfaabi_dbg_debug_do( thrd_dst->park_result = old_ticket; ) |
---|
| 478 | switch(old_ticket) { |
---|
| 479 | case 1: |
---|
[3381ed7] | 480 | // This is case 1, the regular case, nothing more is needed |
---|
| 481 | break RUNNING; |
---|
[ff79d5e] | 482 | case 2: |
---|
[3381ed7] | 483 | // This is case 2, the racy case, someone tried to run this thread before it finished blocking |
---|
| 484 | // In this case, just run it again. |
---|
| 485 | continue RUNNING; |
---|
| 486 | default: |
---|
| 487 | // This makes no sense, something is wrong abort |
---|
[ff79d5e] | 488 | abort(); |
---|
[3381ed7] | 489 | } |
---|
[9f575ea] | 490 | } |
---|
[e8e457e] | 491 | |
---|
[9f575ea] | 492 | // Just before returning to the processor, set the processor coroutine to active |
---|
[e8e457e] | 493 | proc_cor->state = Active; |
---|
[ae7be7a] | 494 | kernelTLS.this_thread = 0p; |
---|
[82c948c] | 495 | } |
---|
| 496 | |
---|
[14a61b5] | 497 | // KERNEL_ONLY |
---|
[b0c7419] | 498 | void returnToKernel() { |
---|
[3381ed7] | 499 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[ac2b598] | 500 | $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner); |
---|
| 501 | $thread * thrd_src = kernelTLS.this_thread; |
---|
[e8e457e] | 502 | |
---|
[29cb302] | 503 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 504 | struct processor * last_proc = kernelTLS.this_processor; |
---|
| 505 | #endif |
---|
| 506 | |
---|
[9f575ea] | 507 | // Run the thread on this processor |
---|
| 508 | { |
---|
| 509 | int local_errno = *__volatile_errno(); |
---|
| 510 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 511 | __x87_store; |
---|
| 512 | #endif |
---|
| 513 | verify( proc_cor->context.SP ); |
---|
[c7a900a] | 514 | __cfactx_switch( &thrd_src->context, &proc_cor->context ); |
---|
[9f575ea] | 515 | #if defined( __i386 ) || defined( __x86_64 ) |
---|
| 516 | __x87_load; |
---|
| 517 | #endif |
---|
| 518 | *__volatile_errno() = local_errno; |
---|
[8fcbb4c] | 519 | } |
---|
[deca0f5] | 520 | |
---|
[29cb302] | 521 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 522 | if(last_proc != kernelTLS.this_processor) { |
---|
| 523 | __tls_stats()->ready.threads.migration++; |
---|
| 524 | } |
---|
| 525 | #endif |
---|
| 526 | |
---|
[3381ed7] | 527 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[210b8b3] | 528 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) < ((uintptr_t)__get_stack(thrd_src->curr_cor)->base ), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too small.\n", thrd_src ); |
---|
| 529 | /* paranoid */ verifyf( ((uintptr_t)thrd_src->context.SP) > ((uintptr_t)__get_stack(thrd_src->curr_cor)->limit), "ERROR : Returning $thread %p has been corrupted.\n StackPointer too large.\n", thrd_src ); |
---|
[c84e80a] | 530 | } |
---|
| 531 | |
---|
[14a61b5] | 532 | // KERNEL_ONLY |
---|
[0c92c9f] | 533 | // Context invoker for processors |
---|
| 534 | // This is the entry point for processors (kernel threads) |
---|
| 535 | // It effectively constructs a coroutine by stealing the pthread stack |
---|
[c7a900a] | 536 | static void * __invoke_processor(void * arg) { |
---|
[8834751] | 537 | #if !defined( __CFA_NO_STATISTICS__ ) |
---|
| 538 | __stats_t local_stats; |
---|
| 539 | __init_stats( &local_stats ); |
---|
| 540 | kernelTLS.this_stats = &local_stats; |
---|
| 541 | #endif |
---|
| 542 | |
---|
[8def349] | 543 | processor * proc = (processor *) arg; |
---|
[14a61b5] | 544 | kernelTLS.this_processor = proc; |
---|
[1805b1b] | 545 | kernelTLS.this_thread = 0p; |
---|
[14a61b5] | 546 | kernelTLS.preemption_state.[enabled, disable_count] = [false, 1]; |
---|
[8def349] | 547 | // SKULLDUGGERY: We want to create a context for the processor coroutine |
---|
| 548 | // which is needed for the 2-step context switch. However, there is no reason |
---|
[1c273d0] | 549 | // to waste the perfectly valid stack create by pthread. |
---|
[8def349] | 550 | current_stack_info_t info; |
---|
[b2f6113] | 551 | __stack_t ctx; |
---|
| 552 | info.storage = &ctx; |
---|
[094476d] | 553 | (proc->runner){ proc, &info }; |
---|
[8def349] | 554 | |
---|
[b2f6113] | 555 | __cfaabi_dbg_print_safe("Coroutine : created stack %p\n", get_coroutine(proc->runner)->stack.storage); |
---|
[8fcbb4c] | 556 | |
---|
[0c92c9f] | 557 | //Set global state |
---|
[1805b1b] | 558 | kernelTLS.this_thread = 0p; |
---|
[8def349] | 559 | |
---|
| 560 | //We now have a proper context from which to schedule threads |
---|
[4069faad] | 561 | __cfadbg_print_safe(runtime_core, "Kernel : core %p created (%p, %p)\n", proc, &proc->runner, &ctx); |
---|
[8def349] | 562 | |
---|
[1c273d0] | 563 | // SKULLDUGGERY: Since the coroutine doesn't have its own stack, we can't |
---|
| 564 | // resume it to start it like it normally would, it will just context switch |
---|
| 565 | // back to here. Instead directly call the main since we already are on the |
---|
[8def349] | 566 | // appropriate stack. |
---|
[094476d] | 567 | get_coroutine(proc->runner)->state = Active; |
---|
| 568 | main( proc->runner ); |
---|
| 569 | get_coroutine(proc->runner)->state = Halted; |
---|
[8def349] | 570 | |
---|
[0c92c9f] | 571 | // Main routine of the core returned, the core is now fully terminated |
---|
[4069faad] | 572 | __cfadbg_print_safe(runtime_core, "Kernel : core %p main ended (%p)\n", proc, &proc->runner); |
---|
[8def349] | 573 | |
---|
[8834751] | 574 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 575 | __tally_stats(proc->cltr->stats, &local_stats); |
---|
[69fbc61] | 576 | if( 0 != proc->print_stats ) { |
---|
| 577 | __print_stats( &local_stats, proc->print_stats, true, proc->name, (void*)proc ); |
---|
[c34ebf2] | 578 | } |
---|
[8834751] | 579 | #endif |
---|
| 580 | |
---|
[1805b1b] | 581 | return 0p; |
---|
[c84e80a] | 582 | } |
---|
| 583 | |
---|
[e3fea42] | 584 | static void Abort( int ret, const char func[] ) { |
---|
[1a3040c] | 585 | if ( ret ) { // pthread routines return errno values |
---|
[1805b1b] | 586 | abort( "%s : internal error, error(%d) %s.", func, ret, strerror( ret ) ); |
---|
[27f5f71] | 587 | } // if |
---|
[1805b1b] | 588 | } // Abort |
---|
| 589 | |
---|
[8c50aed] | 590 | void * __create_pthread( pthread_t * pthread, void * (*start)(void *), void * arg ) { |
---|
[1805b1b] | 591 | pthread_attr_t attr; |
---|
| 592 | |
---|
| 593 | Abort( pthread_attr_init( &attr ), "pthread_attr_init" ); // initialize attribute |
---|
| 594 | |
---|
[27f5f71] | 595 | size_t stacksize; |
---|
[09d4b22] | 596 | // default stack size, normally defined by shell limit |
---|
[1a3040c] | 597 | Abort( pthread_attr_getstacksize( &attr, &stacksize ), "pthread_attr_getstacksize" ); |
---|
[27f5f71] | 598 | assert( stacksize >= PTHREAD_STACK_MIN ); |
---|
[1a3040c] | 599 | |
---|
[09d4b22] | 600 | void * stack; |
---|
| 601 | __cfaabi_dbg_debug_do( |
---|
| 602 | stack = memalign( __page_size, stacksize + __page_size ); |
---|
| 603 | // pthread has no mechanism to create the guard page in user supplied stack. |
---|
| 604 | if ( mprotect( stack, __page_size, PROT_NONE ) == -1 ) { |
---|
| 605 | abort( "mprotect : internal error, mprotect failure, error(%d) %s.", errno, strerror( errno ) ); |
---|
| 606 | } // if |
---|
| 607 | ); |
---|
| 608 | __cfaabi_dbg_no_debug_do( |
---|
| 609 | stack = malloc( stacksize ); |
---|
| 610 | ); |
---|
[1a3040c] | 611 | |
---|
[f80f840] | 612 | Abort( pthread_attr_setstack( &attr, stack, stacksize ), "pthread_attr_setstack" ); |
---|
[27f5f71] | 613 | |
---|
[1805b1b] | 614 | Abort( pthread_create( pthread, &attr, start, arg ), "pthread_create" ); |
---|
| 615 | return stack; |
---|
[c84e80a] | 616 | } |
---|
| 617 | |
---|
[14a61b5] | 618 | // KERNEL_ONLY |
---|
[8c50aed] | 619 | static void __kernel_first_resume( processor * this ) { |
---|
[ac2b598] | 620 | $thread * src = mainThread; |
---|
| 621 | $coroutine * dst = get_coroutine(this->runner); |
---|
[b69ea6b] | 622 | |
---|
[14a61b5] | 623 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 624 | |
---|
[09f357ec] | 625 | kernelTLS.this_thread->curr_cor = dst; |
---|
[b2f6113] | 626 | __stack_prepare( &dst->stack, 65000 ); |
---|
[c7a900a] | 627 | __cfactx_start(main, dst, this->runner, __cfactx_invoke_coroutine); |
---|
[b69ea6b] | 628 | |
---|
[14a61b5] | 629 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 630 | |
---|
[e8e457e] | 631 | dst->last = &src->self_cor; |
---|
| 632 | dst->starter = dst->starter ? dst->starter : &src->self_cor; |
---|
[b69ea6b] | 633 | |
---|
[92e7631] | 634 | // make sure the current state is still correct |
---|
| 635 | /* paranoid */ verify(src->state == Ready); |
---|
[b69ea6b] | 636 | |
---|
| 637 | // context switch to specified coroutine |
---|
[69a61d2] | 638 | verify( dst->context.SP ); |
---|
[c7a900a] | 639 | __cfactx_switch( &src->context, &dst->context ); |
---|
| 640 | // when __cfactx_switch returns we are back in the src coroutine |
---|
[b69ea6b] | 641 | |
---|
[09f357ec] | 642 | mainThread->curr_cor = &mainThread->self_cor; |
---|
| 643 | |
---|
[92e7631] | 644 | // make sure the current state has been update |
---|
| 645 | /* paranoid */ verify(src->state == Active); |
---|
[b69ea6b] | 646 | |
---|
[14a61b5] | 647 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[b69ea6b] | 648 | } |
---|
| 649 | |
---|
[e8e457e] | 650 | // KERNEL_ONLY |
---|
[8c50aed] | 651 | static void __kernel_last_resume( processor * this ) { |
---|
[ac2b598] | 652 | $coroutine * src = &mainThread->self_cor; |
---|
| 653 | $coroutine * dst = get_coroutine(this->runner); |
---|
[e8e457e] | 654 | |
---|
| 655 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 656 | verify( dst->starter == src ); |
---|
| 657 | verify( dst->context.SP ); |
---|
| 658 | |
---|
[f586539] | 659 | // SKULLDUGGERY in debug the processors check that the |
---|
| 660 | // stack is still within the limit of the stack limits after running a thread. |
---|
| 661 | // that check doesn't make sense if we context switch to the processor using the |
---|
| 662 | // coroutine semantics. Since this is a special case, use the current context |
---|
| 663 | // info to populate these fields. |
---|
| 664 | __cfaabi_dbg_debug_do( |
---|
| 665 | __stack_context_t ctx; |
---|
| 666 | CtxGet( ctx ); |
---|
| 667 | mainThread->context.SP = ctx.SP; |
---|
| 668 | mainThread->context.FP = ctx.FP; |
---|
| 669 | ) |
---|
| 670 | |
---|
[e8e457e] | 671 | // context switch to the processor |
---|
[c7a900a] | 672 | __cfactx_switch( &src->context, &dst->context ); |
---|
[e8e457e] | 673 | } |
---|
| 674 | |
---|
[8def349] | 675 | //----------------------------------------------------------------------------- |
---|
| 676 | // Scheduler routines |
---|
[14a61b5] | 677 | // KERNEL ONLY |
---|
[9b1dcc2] | 678 | void __schedule_thread( struct __processor_id_t * id, $thread * thrd ) { |
---|
[6a490b2] | 679 | /* paranoid */ verify( thrd ); |
---|
| 680 | /* paranoid */ verify( thrd->state != Halted ); |
---|
[9f575ea] | 681 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[3381ed7] | 682 | /* paranoid */ #if defined( __CFA_WITH_VERIFY__ ) |
---|
[504a7dc] | 683 | /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION, |
---|
| 684 | "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
[ff79d5e] | 685 | /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active, |
---|
[504a7dc] | 686 | "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted ); |
---|
[3381ed7] | 687 | /* paranoid */ #endif |
---|
[6a490b2] | 688 | /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next ); |
---|
[9f575ea] | 689 | |
---|
[ae7be7a] | 690 | if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready; |
---|
[6b4cdd3] | 691 | |
---|
[9b1dcc2] | 692 | ready_schedule_lock ( id ); |
---|
[504a7dc] | 693 | push( thrd->curr_cluster, thrd ); |
---|
[b798713] | 694 | |
---|
[68f36f4] | 695 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 696 | bool woke = |
---|
| 697 | #endif |
---|
| 698 | __wake_one(id, thrd->curr_cluster); |
---|
| 699 | |
---|
| 700 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 701 | if(woke) __tls_stats()->ready.sleep.wakes++; |
---|
| 702 | #endif |
---|
[9b1dcc2] | 703 | ready_schedule_unlock( id ); |
---|
[1c273d0] | 704 | |
---|
[9f575ea] | 705 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[db6f06a] | 706 | } |
---|
| 707 | |
---|
[14a61b5] | 708 | // KERNEL ONLY |
---|
[ac2b598] | 709 | static $thread * __next_thread(cluster * this) with( *this ) { |
---|
[3381ed7] | 710 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[7768b8d] | 711 | |
---|
[9b1dcc2] | 712 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor ); |
---|
[6a490b2] | 713 | $thread * head = pop( this ); |
---|
[9b1dcc2] | 714 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor ); |
---|
[7768b8d] | 715 | |
---|
[3381ed7] | 716 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[db6f06a] | 717 | return head; |
---|
[eb2e723] | 718 | } |
---|
| 719 | |
---|
[64a7146] | 720 | // KERNEL ONLY |
---|
| 721 | static bool __has_next_thread(cluster * this) with( *this ) { |
---|
| 722 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 723 | |
---|
| 724 | ready_schedule_lock ( (__processor_id_t*)kernelTLS.this_processor ); |
---|
| 725 | bool not_empty = query( this ); |
---|
| 726 | ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor ); |
---|
| 727 | |
---|
| 728 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 729 | return not_empty; |
---|
| 730 | } |
---|
| 731 | |
---|
[2d8f7b0] | 732 | // KERNEL ONLY unpark with out disabling interrupts |
---|
[9b1dcc2] | 733 | void __unpark( struct __processor_id_t * id, $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
[ae66348] | 734 | // record activity |
---|
[d47349b] | 735 | __cfaabi_dbg_debug_do( char * old_caller = thrd->unpark_caller; ) |
---|
[ae66348] | 736 | __cfaabi_dbg_record_thrd( *thrd, false, caller ); |
---|
| 737 | |
---|
[ff79d5e] | 738 | int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST); |
---|
| 739 | __cfaabi_dbg_debug_do( thrd->unpark_result = old_ticket; thrd->unpark_state = thrd->state; ) |
---|
| 740 | switch(old_ticket) { |
---|
| 741 | case 1: |
---|
[3381ed7] | 742 | // Wake won the race, the thread will reschedule/rerun itself |
---|
| 743 | break; |
---|
[ff79d5e] | 744 | case 0: |
---|
[3381ed7] | 745 | /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION ); |
---|
[ff79d5e] | 746 | /* paranoid */ verify( thrd->state == Blocked ); |
---|
[0b33412] | 747 | |
---|
[3381ed7] | 748 | // Wake lost the race, |
---|
[9b1dcc2] | 749 | __schedule_thread( id, thrd ); |
---|
[3381ed7] | 750 | break; |
---|
| 751 | default: |
---|
| 752 | // This makes no sense, something is wrong abort |
---|
| 753 | abort(); |
---|
[de6319f] | 754 | } |
---|
[db6f06a] | 755 | } |
---|
| 756 | |
---|
[2d8f7b0] | 757 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) { |
---|
| 758 | if( !thrd ) return; |
---|
[db6f06a] | 759 | |
---|
[82ff5845] | 760 | disable_interrupts(); |
---|
[9b1dcc2] | 761 | __unpark( (__processor_id_t*)kernelTLS.this_processor, thrd __cfaabi_dbg_ctx_fwd2 ); |
---|
[36982fc] | 762 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[eb2e723] | 763 | } |
---|
| 764 | |
---|
[ae66348] | 765 | void park( __cfaabi_dbg_ctx_param ) { |
---|
[3381ed7] | 766 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[82ff5845] | 767 | disable_interrupts(); |
---|
[3381ed7] | 768 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 769 | /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION ); |
---|
[0b33412] | 770 | |
---|
[ae66348] | 771 | // record activity |
---|
| 772 | __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller ); |
---|
[0b33412] | 773 | |
---|
[82c948c] | 774 | returnToKernel(); |
---|
[0b33412] | 775 | |
---|
[3381ed7] | 776 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 777 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[3381ed7] | 778 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[0c78741] | 779 | |
---|
| 780 | } |
---|
[09800e9] | 781 | |
---|
[3381ed7] | 782 | // KERNEL ONLY |
---|
[b0c7419] | 783 | void __leave_thread() { |
---|
[3381ed7] | 784 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 785 | returnToKernel(); |
---|
[b0c7419] | 786 | abort(); |
---|
[09800e9] | 787 | } |
---|
| 788 | |
---|
[14a61b5] | 789 | // KERNEL ONLY |
---|
[3381ed7] | 790 | bool force_yield( __Preemption_Reason reason ) { |
---|
| 791 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 792 | disable_interrupts(); |
---|
[3381ed7] | 793 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[09800e9] | 794 | |
---|
[ac2b598] | 795 | $thread * thrd = kernelTLS.this_thread; |
---|
[ff79d5e] | 796 | /* paranoid */ verify(thrd->state == Active); |
---|
[3381ed7] | 797 | |
---|
| 798 | // SKULLDUGGERY: It is possible that we are preempting this thread just before |
---|
| 799 | // it was going to park itself. If that is the case and it is already using the |
---|
| 800 | // intrusive fields then we can't use them to preempt the thread |
---|
| 801 | // If that is the case, abandon the preemption. |
---|
| 802 | bool preempted = false; |
---|
[504a7dc] | 803 | if(thrd->link.next == 0p) { |
---|
[3381ed7] | 804 | preempted = true; |
---|
| 805 | thrd->preempted = reason; |
---|
| 806 | returnToKernel(); |
---|
[de6319f] | 807 | } |
---|
[f2b12406] | 808 | |
---|
[3381ed7] | 809 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 810 | enable_interrupts_noPoll(); |
---|
| 811 | /* paranoid */ verify( kernelTLS.preemption_state.enabled ); |
---|
[f2b12406] | 812 | |
---|
[3381ed7] | 813 | return preempted; |
---|
[f2b12406] | 814 | } |
---|
| 815 | |
---|
[fa21ac9] | 816 | //============================================================================================= |
---|
| 817 | // Kernel Setup logic |
---|
| 818 | //============================================================================================= |
---|
[eb2e723] | 819 | //----------------------------------------------------------------------------- |
---|
| 820 | // Kernel boot procedures |
---|
[8c50aed] | 821 | static void __kernel_startup(void) { |
---|
[14a61b5] | 822 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[4069faad] | 823 | __cfadbg_print_safe(runtime_core, "Kernel : Starting\n"); |
---|
[eb2e723] | 824 | |
---|
[b2f6113] | 825 | __page_size = sysconf( _SC_PAGESIZE ); |
---|
| 826 | |
---|
[ea8b2f7] | 827 | __cfa_dbg_global_clusters.list{ __get }; |
---|
| 828 | __cfa_dbg_global_clusters.lock{}; |
---|
[de94a60] | 829 | |
---|
[b388ee81] | 830 | // Initialize the global scheduler lock |
---|
| 831 | __scheduler_lock = (__scheduler_RWLock_t*)&storage___scheduler_lock; |
---|
| 832 | (*__scheduler_lock){}; |
---|
| 833 | |
---|
[de6319f] | 834 | // Initialize the main cluster |
---|
| 835 | mainCluster = (cluster *)&storage_mainCluster; |
---|
| 836 | (*mainCluster){"Main Cluster"}; |
---|
| 837 | |
---|
[4069faad] | 838 | __cfadbg_print_safe(runtime_core, "Kernel : Main cluster ready\n"); |
---|
[de6319f] | 839 | |
---|
[eb2e723] | 840 | // Start by initializing the main thread |
---|
[1c273d0] | 841 | // SKULLDUGGERY: the mainThread steals the process main thread |
---|
[969b3fe] | 842 | // which will then be scheduled by the mainProcessor normally |
---|
[ac2b598] | 843 | mainThread = ($thread *)&storage_mainThread; |
---|
[8fcbb4c] | 844 | current_stack_info_t info; |
---|
[b2f6113] | 845 | info.storage = (__stack_t*)&storage_mainThreadCtx; |
---|
[83a071f9] | 846 | (*mainThread){ &info }; |
---|
[eb2e723] | 847 | |
---|
[4069faad] | 848 | __cfadbg_print_safe(runtime_core, "Kernel : Main thread ready\n"); |
---|
[fa21ac9] | 849 | |
---|
[bd98b58] | 850 | |
---|
[de6319f] | 851 | |
---|
| 852 | // Construct the processor context of the main processor |
---|
| 853 | void ?{}(processorCtx_t & this, processor * proc) { |
---|
| 854 | (this.__cor){ "Processor" }; |
---|
[1805b1b] | 855 | this.__cor.starter = 0p; |
---|
[de6319f] | 856 | this.proc = proc; |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | void ?{}(processor & this) with( this ) { |
---|
| 860 | name = "Main Processor"; |
---|
| 861 | cltr = mainCluster; |
---|
| 862 | terminated{ 0 }; |
---|
| 863 | do_terminate = false; |
---|
[1805b1b] | 864 | preemption_alarm = 0p; |
---|
[de6319f] | 865 | pending_preemption = false; |
---|
| 866 | kernel_thread = pthread_self(); |
---|
[7768b8d] | 867 | id = -1u; |
---|
[de6319f] | 868 | |
---|
[c34ebf2] | 869 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 870 | print_stats = false; |
---|
| 871 | print_halts = false; |
---|
| 872 | #endif |
---|
| 873 | |
---|
[de6319f] | 874 | runner{ &this }; |
---|
[4069faad] | 875 | __cfadbg_print_safe(runtime_core, "Kernel : constructed main processor context %p\n", &runner); |
---|
[13c5e19] | 876 | |
---|
| 877 | __atomic_fetch_add( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST ); |
---|
[de6319f] | 878 | } |
---|
[fa21ac9] | 879 | |
---|
[969b3fe] | 880 | // Initialize the main processor and the main processor ctx |
---|
[eb2e723] | 881 | // (the coroutine that contains the processing control flow) |
---|
[969b3fe] | 882 | mainProcessor = (processor *)&storage_mainProcessor; |
---|
[de6319f] | 883 | (*mainProcessor){}; |
---|
[eb2e723] | 884 | |
---|
[9b1dcc2] | 885 | mainProcessor->id = doregister( (__processor_id_t*)mainProcessor); |
---|
[7768b8d] | 886 | |
---|
[dcb42b8] | 887 | //initialize the global state variables |
---|
[14a61b5] | 888 | kernelTLS.this_processor = mainProcessor; |
---|
| 889 | kernelTLS.this_thread = mainThread; |
---|
[eb2e723] | 890 | |
---|
[8834751] | 891 | #if !defined( __CFA_NO_STATISTICS__ ) |
---|
| 892 | kernelTLS.this_stats = (__stats_t *)& storage_mainProcStats; |
---|
| 893 | __init_stats( kernelTLS.this_stats ); |
---|
| 894 | #endif |
---|
| 895 | |
---|
[82ff5845] | 896 | // Enable preemption |
---|
| 897 | kernel_start_preemption(); |
---|
| 898 | |
---|
[969b3fe] | 899 | // Add the main thread to the ready queue |
---|
| 900 | // once resume is called on mainProcessor->runner the mainThread needs to be scheduled like any normal thread |
---|
[9b1dcc2] | 901 | __schedule_thread((__processor_id_t *)mainProcessor, mainThread); |
---|
[969b3fe] | 902 | |
---|
| 903 | // SKULLDUGGERY: Force a context switch to the main processor to set the main thread's context to the current UNIX |
---|
[c7a900a] | 904 | // context. Hence, the main thread does not begin through __cfactx_invoke_thread, like all other threads. The trick here is that |
---|
[1c273d0] | 905 | // mainThread is on the ready queue when this call is made. |
---|
[8c50aed] | 906 | __kernel_first_resume( kernelTLS.this_processor ); |
---|
[dcb42b8] | 907 | |
---|
| 908 | |
---|
| 909 | // THE SYSTEM IS NOW COMPLETELY RUNNING |
---|
[f6660520] | 910 | |
---|
| 911 | |
---|
| 912 | // Now that the system is up, finish creating systems that need threading |
---|
| 913 | __kernel_io_finish_start( *mainCluster ); |
---|
| 914 | |
---|
| 915 | |
---|
| 916 | __cfadbg_print_safe(runtime_core, "Kernel : Started\n--------------------------------------------------\n\n"); |
---|
[82ff5845] | 917 | |
---|
[14a61b5] | 918 | verify( ! kernelTLS.preemption_state.enabled ); |
---|
[36982fc] | 919 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[afd550c] | 920 | verify( TL_GET( preemption_state.enabled ) ); |
---|
[eb2e723] | 921 | } |
---|
| 922 | |
---|
[8c50aed] | 923 | static void __kernel_shutdown(void) { |
---|
[f6660520] | 924 | //Before we start shutting things down, wait for systems that need threading to shutdown |
---|
| 925 | __kernel_io_prepare_stop( *mainCluster ); |
---|
[eb2e723] | 926 | |
---|
[92e7631] | 927 | /* paranoid */ verify( TL_GET( preemption_state.enabled ) ); |
---|
[4e6fb8e] | 928 | disable_interrupts(); |
---|
[92e7631] | 929 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
[4e6fb8e] | 930 | |
---|
[f6660520] | 931 | __cfadbg_print_safe(runtime_core, "\n--------------------------------------------------\nKernel : Shutting down\n"); |
---|
[4e6fb8e] | 932 | |
---|
[969b3fe] | 933 | // SKULLDUGGERY: Notify the mainProcessor it needs to terminates. |
---|
[dcb42b8] | 934 | // When its coroutine terminates, it return control to the mainThread |
---|
| 935 | // which is currently here |
---|
[ea8b2f7] | 936 | __atomic_store_n(&mainProcessor->do_terminate, true, __ATOMIC_RELEASE); |
---|
[8c50aed] | 937 | __kernel_last_resume( kernelTLS.this_processor ); |
---|
[ea8b2f7] | 938 | mainThread->self_cor.state = Halted; |
---|
[eb2e723] | 939 | |
---|
[dcb42b8] | 940 | // THE SYSTEM IS NOW COMPLETELY STOPPED |
---|
[eb2e723] | 941 | |
---|
[82ff5845] | 942 | // Disable preemption |
---|
| 943 | kernel_stop_preemption(); |
---|
| 944 | |
---|
[9b1dcc2] | 945 | unregister((__processor_id_t*)mainProcessor); |
---|
[7768b8d] | 946 | |
---|
[969b3fe] | 947 | // Destroy the main processor and its context in reverse order of construction |
---|
[dcb42b8] | 948 | // These were manually constructed so we need manually destroy them |
---|
[c66f6cb] | 949 | void ^?{}(processor & this) with( this ){ |
---|
| 950 | /* paranoid */ verify( this.do_terminate == true ); |
---|
[13c5e19] | 951 | __atomic_fetch_sub( &cltr->nprocessors, 1u, __ATOMIC_SEQ_CST ); |
---|
[b798713] | 952 | __cfaabi_dbg_print_safe("Kernel : destroyed main processor context %p\n", &runner); |
---|
| 953 | } |
---|
| 954 | |
---|
[7768b8d] | 955 | ^(*mainProcessor){}; |
---|
[eb2e723] | 956 | |
---|
[dcb42b8] | 957 | // Final step, destroy the main thread since it is no longer needed |
---|
[6a490b2] | 958 | |
---|
[dcb42b8] | 959 | // Since we provided a stack to this taxk it will not destroy anything |
---|
[210b8b3] | 960 | /* paranoid */ verify(mainThread->self_cor.stack.storage == (__stack_t*)(((uintptr_t)&storage_mainThreadCtx)| 0x1)); |
---|
[7768b8d] | 961 | ^(*mainThread){}; |
---|
| 962 | |
---|
| 963 | ^(*mainCluster){}; |
---|
[eb2e723] | 964 | |
---|
[b388ee81] | 965 | ^(*__scheduler_lock){}; |
---|
| 966 | |
---|
[ea8b2f7] | 967 | ^(__cfa_dbg_global_clusters.list){}; |
---|
| 968 | ^(__cfa_dbg_global_clusters.lock){}; |
---|
[a1a17a7] | 969 | |
---|
[4069faad] | 970 | __cfadbg_print_safe(runtime_core, "Kernel : Shutdown complete\n"); |
---|
[9d944b2] | 971 | } |
---|
| 972 | |
---|
[14a61b5] | 973 | //============================================================================================= |
---|
[92e7631] | 974 | // Kernel Idle Sleep |
---|
[14a61b5] | 975 | //============================================================================================= |
---|
[64a7146] | 976 | // Wake a thread from the front if there are any |
---|
| 977 | static bool __wake_one(struct __processor_id_t * id, cluster * this) { |
---|
| 978 | /* paranoid */ verify( ready_schedule_islocked( id ) ); |
---|
[14a61b5] | 979 | |
---|
[64a7146] | 980 | // Check if there is a sleeping processor |
---|
| 981 | processor * p = pop(this->idles); |
---|
[14a61b5] | 982 | |
---|
[64a7146] | 983 | // If no one is sleeping, we are done |
---|
| 984 | if( 0p == p ) return false; |
---|
[14a61b5] | 985 | |
---|
[64a7146] | 986 | // We found a processor, wake it up |
---|
| 987 | post( p->idle ); |
---|
[14a61b5] | 988 | |
---|
[64a7146] | 989 | return true; |
---|
| 990 | } |
---|
[14a61b5] | 991 | |
---|
[64a7146] | 992 | // Unconditionnaly wake a thread |
---|
| 993 | static bool __wake_proc(processor * this) { |
---|
| 994 | __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this); |
---|
[14a61b5] | 995 | |
---|
[64a7146] | 996 | disable_interrupts(); |
---|
| 997 | /* paranoid */ verify( ! kernelTLS.preemption_state.enabled ); |
---|
| 998 | bool ret = post( this->idle ); |
---|
| 999 | enable_interrupts( __cfaabi_dbg_ctx ); |
---|
[92e7631] | 1000 | |
---|
[64a7146] | 1001 | return ret; |
---|
[92e7631] | 1002 | } |
---|
| 1003 | |
---|
[64a7146] | 1004 | static void __halt(processor * this) with( *this ) { |
---|
| 1005 | if( do_terminate ) return; |
---|
[8e16177] | 1006 | |
---|
[68f36f4] | 1007 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 1008 | __tls_stats()->ready.sleep.halts++; |
---|
| 1009 | #endif |
---|
[64a7146] | 1010 | // Push self to queue |
---|
| 1011 | push(cltr->idles, *this); |
---|
[8834751] | 1012 | |
---|
[64a7146] | 1013 | // Makre sure we don't miss a thread |
---|
| 1014 | if( __has_next_thread(cltr) ) { |
---|
| 1015 | // A thread was posted, make sure a processor is woken up |
---|
| 1016 | struct __processor_id_t *id = (struct __processor_id_t *) this; |
---|
| 1017 | ready_schedule_lock ( id ); |
---|
| 1018 | __wake_one( id, cltr ); |
---|
| 1019 | ready_schedule_unlock( id ); |
---|
[68f36f4] | 1020 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 1021 | __tls_stats()->ready.sleep.cancels++; |
---|
| 1022 | #endif |
---|
[64a7146] | 1023 | } |
---|
[8e16177] | 1024 | |
---|
[c34ebf2] | 1025 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 1026 | if(this->print_halts) { |
---|
[f2b18d01] | 1027 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl()); |
---|
[c34ebf2] | 1028 | } |
---|
| 1029 | #endif |
---|
| 1030 | |
---|
[64a7146] | 1031 | wait( idle ); |
---|
[c34ebf2] | 1032 | |
---|
| 1033 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 1034 | if(this->print_halts) { |
---|
[f2b18d01] | 1035 | __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl()); |
---|
[c34ebf2] | 1036 | } |
---|
| 1037 | #endif |
---|
[6b4cdd3] | 1038 | } |
---|
| 1039 | |
---|
[dbe9b08] | 1040 | //============================================================================================= |
---|
| 1041 | // Unexpected Terminating logic |
---|
| 1042 | //============================================================================================= |
---|
[ea7d2b0] | 1043 | static __spinlock_t kernel_abort_lock; |
---|
[9d944b2] | 1044 | static bool kernel_abort_called = false; |
---|
| 1045 | |
---|
[afd550c] | 1046 | void * kernel_abort(void) __attribute__ ((__nothrow__)) { |
---|
[9d944b2] | 1047 | // abort cannot be recursively entered by the same or different processors because all signal handlers return when |
---|
| 1048 | // the globalAbort flag is true. |
---|
[36982fc] | 1049 | lock( kernel_abort_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 1050 | |
---|
| 1051 | // first task to abort ? |
---|
[de94a60] | 1052 | if ( kernel_abort_called ) { // not first task to abort ? |
---|
[ea7d2b0] | 1053 | unlock( kernel_abort_lock ); |
---|
[1c273d0] | 1054 | |
---|
[9d944b2] | 1055 | sigset_t mask; |
---|
| 1056 | sigemptyset( &mask ); |
---|
[de94a60] | 1057 | sigaddset( &mask, SIGALRM ); // block SIGALRM signals |
---|
[8a13c47] | 1058 | sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals |
---|
| 1059 | sigsuspend( &mask ); // block the processor to prevent further damage during abort |
---|
| 1060 | _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it |
---|
[de94a60] | 1061 | } |
---|
| 1062 | else { |
---|
| 1063 | kernel_abort_called = true; |
---|
| 1064 | unlock( kernel_abort_lock ); |
---|
[9d944b2] | 1065 | } |
---|
| 1066 | |
---|
[14a61b5] | 1067 | return kernelTLS.this_thread; |
---|
[9d944b2] | 1068 | } |
---|
| 1069 | |
---|
| 1070 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) { |
---|
[ac2b598] | 1071 | $thread * thrd = kernel_data; |
---|
[9d944b2] | 1072 | |
---|
[de94a60] | 1073 | if(thrd) { |
---|
| 1074 | int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd ); |
---|
[1c40091] | 1075 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 1076 | |
---|
[212c2187] | 1077 | if ( &thrd->self_cor != thrd->curr_cor ) { |
---|
| 1078 | len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor ); |
---|
[1c40091] | 1079 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[de94a60] | 1080 | } |
---|
| 1081 | else { |
---|
[1c40091] | 1082 | __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 ); |
---|
[de94a60] | 1083 | } |
---|
[1c273d0] | 1084 | } |
---|
[9d944b2] | 1085 | else { |
---|
[de94a60] | 1086 | int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" ); |
---|
[1c40091] | 1087 | __cfaabi_bits_write( STDERR_FILENO, abort_text, len ); |
---|
[9d944b2] | 1088 | } |
---|
| 1089 | } |
---|
| 1090 | |
---|
[2b8bc41] | 1091 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) { |
---|
[14a61b5] | 1092 | return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2; |
---|
[2b8bc41] | 1093 | } |
---|
| 1094 | |
---|
[de94a60] | 1095 | static __spinlock_t kernel_debug_lock; |
---|
| 1096 | |
---|
[9d944b2] | 1097 | extern "C" { |
---|
[1c40091] | 1098 | void __cfaabi_bits_acquire() { |
---|
[36982fc] | 1099 | lock( kernel_debug_lock __cfaabi_dbg_ctx2 ); |
---|
[9d944b2] | 1100 | } |
---|
| 1101 | |
---|
[1c40091] | 1102 | void __cfaabi_bits_release() { |
---|
[ea7d2b0] | 1103 | unlock( kernel_debug_lock ); |
---|
[9d944b2] | 1104 | } |
---|
[8118303] | 1105 | } |
---|
| 1106 | |
---|
[fa21ac9] | 1107 | //============================================================================================= |
---|
| 1108 | // Kernel Utilities |
---|
| 1109 | //============================================================================================= |
---|
[bd98b58] | 1110 | //----------------------------------------------------------------------------- |
---|
| 1111 | // Locks |
---|
[242a902] | 1112 | void ?{}( semaphore & this, int count = 1 ) { |
---|
| 1113 | (this.lock){}; |
---|
| 1114 | this.count = count; |
---|
| 1115 | (this.waiting){}; |
---|
[db6f06a] | 1116 | } |
---|
[242a902] | 1117 | void ^?{}(semaphore & this) {} |
---|
[db6f06a] | 1118 | |
---|
[71c8b7e] | 1119 | bool P(semaphore & this) with( this ){ |
---|
[65deb18] | 1120 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 1121 | count -= 1; |
---|
| 1122 | if ( count < 0 ) { |
---|
[bdeba0b] | 1123 | // queue current task |
---|
[14a61b5] | 1124 | append( waiting, kernelTLS.this_thread ); |
---|
[bdeba0b] | 1125 | |
---|
| 1126 | // atomically release spin lock and block |
---|
[3381ed7] | 1127 | unlock( lock ); |
---|
[ae66348] | 1128 | park( __cfaabi_dbg_ctx ); |
---|
[71c8b7e] | 1129 | return true; |
---|
[8def349] | 1130 | } |
---|
[4e6fb8e] | 1131 | else { |
---|
[65deb18] | 1132 | unlock( lock ); |
---|
[71c8b7e] | 1133 | return false; |
---|
[4e6fb8e] | 1134 | } |
---|
[bd98b58] | 1135 | } |
---|
| 1136 | |
---|
[f0ce5f4] | 1137 | bool V(semaphore & this) with( this ) { |
---|
[ac2b598] | 1138 | $thread * thrd = 0p; |
---|
[65deb18] | 1139 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 1140 | count += 1; |
---|
| 1141 | if ( count <= 0 ) { |
---|
[bdeba0b] | 1142 | // remove task at head of waiting list |
---|
[65deb18] | 1143 | thrd = pop_head( waiting ); |
---|
[bd98b58] | 1144 | } |
---|
[bdeba0b] | 1145 | |
---|
[65deb18] | 1146 | unlock( lock ); |
---|
[bdeba0b] | 1147 | |
---|
| 1148 | // make new owner |
---|
[ae66348] | 1149 | unpark( thrd __cfaabi_dbg_ctx2 ); |
---|
[f0ce5f4] | 1150 | |
---|
[d384787] | 1151 | return thrd != 0p; |
---|
| 1152 | } |
---|
| 1153 | |
---|
| 1154 | bool V(semaphore & this, unsigned diff) with( this ) { |
---|
| 1155 | $thread * thrd = 0p; |
---|
| 1156 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 1157 | int release = max(-count, (int)diff); |
---|
| 1158 | count += diff; |
---|
| 1159 | for(release) { |
---|
| 1160 | unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 ); |
---|
| 1161 | } |
---|
| 1162 | |
---|
| 1163 | unlock( lock ); |
---|
| 1164 | |
---|
[f0ce5f4] | 1165 | return thrd != 0p; |
---|
[bd98b58] | 1166 | } |
---|
| 1167 | |
---|
[f7d6bb0] | 1168 | //----------------------------------------------------------------------------- |
---|
[de94a60] | 1169 | // Global Queues |
---|
| 1170 | void doregister( cluster & cltr ) { |
---|
[ea8b2f7] | 1171 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 1172 | push_front( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 1173 | unlock ( __cfa_dbg_global_clusters.lock ); |
---|
[de94a60] | 1174 | } |
---|
[f7d6bb0] | 1175 | |
---|
[de94a60] | 1176 | void unregister( cluster & cltr ) { |
---|
[ea8b2f7] | 1177 | lock ( __cfa_dbg_global_clusters.lock __cfaabi_dbg_ctx2); |
---|
| 1178 | remove( __cfa_dbg_global_clusters.list, cltr ); |
---|
| 1179 | unlock( __cfa_dbg_global_clusters.lock ); |
---|
[de94a60] | 1180 | } |
---|
[f7d6bb0] | 1181 | |
---|
[ac2b598] | 1182 | void doregister( cluster * cltr, $thread & thrd ) { |
---|
[a1a17a7] | 1183 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
[d4e68a6] | 1184 | cltr->nthreads += 1; |
---|
[a1a17a7] | 1185 | push_front(cltr->threads, thrd); |
---|
| 1186 | unlock (cltr->thread_list_lock); |
---|
| 1187 | } |
---|
| 1188 | |
---|
[ac2b598] | 1189 | void unregister( cluster * cltr, $thread & thrd ) { |
---|
[a1a17a7] | 1190 | lock (cltr->thread_list_lock __cfaabi_dbg_ctx2); |
---|
| 1191 | remove(cltr->threads, thrd ); |
---|
[d4e68a6] | 1192 | cltr->nthreads -= 1; |
---|
[a1a17a7] | 1193 | unlock(cltr->thread_list_lock); |
---|
| 1194 | } |
---|
[9181f1d] | 1195 | |
---|
[de94a60] | 1196 | //----------------------------------------------------------------------------- |
---|
| 1197 | // Debug |
---|
| 1198 | __cfaabi_dbg_debug_do( |
---|
[1997b4e] | 1199 | extern "C" { |
---|
[ae66348] | 1200 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) { |
---|
[1997b4e] | 1201 | this.prev_name = prev_name; |
---|
| 1202 | this.prev_thrd = kernelTLS.this_thread; |
---|
| 1203 | } |
---|
[ae66348] | 1204 | |
---|
| 1205 | void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) { |
---|
| 1206 | if(park) { |
---|
[f0ce5f4] | 1207 | this.park_caller = prev_name; |
---|
| 1208 | this.park_stale = false; |
---|
[ae66348] | 1209 | } |
---|
| 1210 | else { |
---|
[f0ce5f4] | 1211 | this.unpark_caller = prev_name; |
---|
| 1212 | this.unpark_stale = false; |
---|
[ae66348] | 1213 | } |
---|
| 1214 | } |
---|
[9181f1d] | 1215 | } |
---|
[f7d6bb0] | 1216 | ) |
---|
[2026bb6] | 1217 | |
---|
| 1218 | //----------------------------------------------------------------------------- |
---|
| 1219 | // Debug |
---|
[8c50aed] | 1220 | bool threading_enabled(void) __attribute__((const)) { |
---|
[2026bb6] | 1221 | return true; |
---|
| 1222 | } |
---|
[c34ebf2] | 1223 | |
---|
| 1224 | //----------------------------------------------------------------------------- |
---|
| 1225 | // Statistics |
---|
| 1226 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 1227 | void print_halts( processor & this ) { |
---|
| 1228 | this.print_halts = true; |
---|
| 1229 | } |
---|
| 1230 | #endif |
---|
[8118303] | 1231 | // Local Variables: // |
---|
| 1232 | // mode: c // |
---|
| 1233 | // tab-width: 4 // |
---|
| 1234 | // End: // |
---|