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