source: libcfa/src/concurrency/kernel.cfa@ 71c8b7e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 71c8b7e was 71c8b7e, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Semaphore P() now returned whether or not it block

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