| [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
 | 
|---|
| [0190480] | 12 | // Last Modified On : Mon Aug 31 07:08:20 2020
 | 
|---|
 | 13 | // Update Count     : 71
 | 
|---|
| [8118303] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [2026bb6] | 16 | #define __cforall_thread__
 | 
|---|
| [4069faad] | 17 | // #define __CFA_DEBUG_PRINT_RUNTIME_CORE__
 | 
|---|
| [2026bb6] | 18 | 
 | 
|---|
| [8118303] | 19 | //C Includes
 | 
|---|
| [214e8da] | 20 | #include <errno.h>
 | 
|---|
| [9d944b2] | 21 | #include <stdio.h>
 | 
|---|
| [58b6d1b] | 22 | #include <signal.h>
 | 
|---|
| [9d944b2] | 23 | #include <unistd.h>
 | 
|---|
| [8118303] | 24 | 
 | 
|---|
 | 25 | //CFA Includes
 | 
|---|
| [73abe95] | 26 | #include "kernel_private.hfa"
 | 
|---|
 | 27 | #include "preemption.hfa"
 | 
|---|
| [8118303] | 28 | 
 | 
|---|
 | 29 | //Private includes
 | 
|---|
 | 30 | #define __CFA_INVOKE_PRIVATE__
 | 
|---|
 | 31 | #include "invoke.h"
 | 
|---|
 | 32 | 
 | 
|---|
| [4069faad] | 33 | 
 | 
|---|
| [deca0f5] | 34 | //-----------------------------------------------------------------------------
 | 
|---|
 | 35 | // Some assembly required
 | 
|---|
| [1805b1b] | 36 | #if defined( __i386 )
 | 
|---|
| [deca0f5] | 37 |         // mxcr : SSE Status and Control bits (control bits are preserved across function calls)
 | 
|---|
 | 38 |         // fcw  : X87 FPU control word (preserved across function calls)
 | 
|---|
 | 39 |         #define __x87_store         \
 | 
|---|
 | 40 |                 uint32_t __mxcr;      \
 | 
|---|
 | 41 |                 uint16_t __fcw;       \
 | 
|---|
 | 42 |                 __asm__ volatile (    \
 | 
|---|
 | 43 |                         "stmxcsr %0\n"  \
 | 
|---|
 | 44 |                         "fnstcw  %1\n"  \
 | 
|---|
 | 45 |                         : "=m" (__mxcr),\
 | 
|---|
 | 46 |                                 "=m" (__fcw)  \
 | 
|---|
 | 47 |                 )
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |         #define __x87_load         \
 | 
|---|
 | 50 |                 __asm__ volatile (   \
 | 
|---|
 | 51 |                         "fldcw  %1\n"  \
 | 
|---|
 | 52 |                         "ldmxcsr %0\n" \
 | 
|---|
 | 53 |                         ::"m" (__mxcr),\
 | 
|---|
 | 54 |                                 "m" (__fcw)  \
 | 
|---|
 | 55 |                 )
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | #elif defined( __x86_64 )
 | 
|---|
 | 58 |         #define __x87_store         \
 | 
|---|
 | 59 |                 uint32_t __mxcr;      \
 | 
|---|
 | 60 |                 uint16_t __fcw;       \
 | 
|---|
 | 61 |                 __asm__ volatile (    \
 | 
|---|
 | 62 |                         "stmxcsr %0\n"  \
 | 
|---|
 | 63 |                         "fnstcw  %1\n"  \
 | 
|---|
 | 64 |                         : "=m" (__mxcr),\
 | 
|---|
 | 65 |                                 "=m" (__fcw)  \
 | 
|---|
 | 66 |                 )
 | 
|---|
 | 67 | 
 | 
|---|
 | 68 |         #define __x87_load          \
 | 
|---|
 | 69 |                 __asm__ volatile (    \
 | 
|---|
 | 70 |                         "fldcw  %1\n"   \
 | 
|---|
 | 71 |                         "ldmxcsr %0\n"  \
 | 
|---|
 | 72 |                         :: "m" (__mxcr),\
 | 
|---|
 | 73 |                                 "m" (__fcw)  \
 | 
|---|
 | 74 |                 )
 | 
|---|
 | 75 | 
 | 
|---|
| [0190480] | 76 | #elif defined( __arm__ )
 | 
|---|
 | 77 |         #define __x87_store
 | 
|---|
 | 78 |         #define __x87_load
 | 
|---|
 | 79 | 
 | 
|---|
 | 80 | #elif defined( __aarch64__ )
 | 
|---|
| [74f5c83] | 81 |         #define __x87_store              \
 | 
|---|
 | 82 |                 uint32_t __fpcntl[2];    \
 | 
|---|
 | 83 |                 __asm__ volatile (    \
 | 
|---|
 | 84 |                         "mrs x9, FPCR\n" \
 | 
|---|
 | 85 |                         "mrs x10, FPSR\n"  \
 | 
|---|
 | 86 |                         "stp x9, x10, %0\n"  \
 | 
|---|
 | 87 |                         : "=m" (__fpcntl) : : "x9", "x10" \
 | 
|---|
 | 88 |                 )
 | 
|---|
 | 89 | 
 | 
|---|
 | 90 |         #define __x87_load         \
 | 
|---|
 | 91 |                 __asm__ volatile (    \
 | 
|---|
 | 92 |                         "ldp x9, x10, %0\n"  \
 | 
|---|
 | 93 |                         "msr FPSR, x10\n"  \
 | 
|---|
 | 94 |                         "msr FPCR, x9\n" \
 | 
|---|
 | 95 |                 : "=m" (__fpcntl) : : "x9", "x10" \
 | 
|---|
 | 96 |                 )
 | 
|---|
 | 97 | 
 | 
|---|
| [deca0f5] | 98 | #else
 | 
|---|
| [0190480] | 99 |         #error unsupported hardware architecture
 | 
|---|
| [deca0f5] | 100 | #endif
 | 
|---|
 | 101 | 
 | 
|---|
| [e660761] | 102 | extern $thread * mainThread;
 | 
|---|
 | 103 | extern processor * mainProcessor;
 | 
|---|
| [2ac095d] | 104 | 
 | 
|---|
| [92e7631] | 105 | //-----------------------------------------------------------------------------
 | 
|---|
 | 106 | // Kernel Scheduling logic
 | 
|---|
 | 107 | static $thread * __next_thread(cluster * this);
 | 
|---|
| [1eb239e4] | 108 | static $thread * __next_thread_slow(cluster * this);
 | 
|---|
| [92e7631] | 109 | static void __run_thread(processor * this, $thread * dst);
 | 
|---|
| [1eb239e4] | 110 | static void __wake_one(struct __processor_id_t * id, cluster * cltr);
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 | static void push  (__cluster_idles & idles, processor & proc);
 | 
|---|
 | 113 | static void remove(__cluster_idles & idles, processor & proc);
 | 
|---|
 | 114 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles idles );
 | 
|---|
 | 115 | 
 | 
|---|
| [c84e80a] | 116 | 
 | 
|---|
| [75f3522] | 117 | //=============================================================================================
 | 
|---|
 | 118 | // Kernel Scheduling logic
 | 
|---|
 | 119 | //=============================================================================================
 | 
|---|
| [8fcbb4c] | 120 | //Main of the processor contexts
 | 
|---|
| [83a071f9] | 121 | void main(processorCtx_t & runner) {
 | 
|---|
| [21184e3] | 122 |         // Because of a bug, we couldn't initialized the seed on construction
 | 
|---|
 | 123 |         // Do it here
 | 
|---|
| [7768b8d] | 124 |         kernelTLS.rand_seed ^= rdtscl();
 | 
|---|
| [f2384c9a] | 125 |         kernelTLS.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&runner);
 | 
|---|
 | 126 |         __tls_rand_advance_bck();
 | 
|---|
| [21184e3] | 127 | 
 | 
|---|
| [83a071f9] | 128 |         processor * this = runner.proc;
 | 
|---|
| [094476d] | 129 |         verify(this);
 | 
|---|
| [c81ebf9] | 130 | 
 | 
|---|
| [4069faad] | 131 |         __cfadbg_print_safe(runtime_core, "Kernel : core %p starting\n", this);
 | 
|---|
| [28d73c1] | 132 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 133 |                 if( this->print_halts ) {
 | 
|---|
 | 134 |                         __cfaabi_bits_print_safe( STDOUT_FILENO, "Processor : %d - %s (%p)\n", this->id, this->name, (void*)this);
 | 
|---|
 | 135 |                 }
 | 
|---|
 | 136 |         #endif
 | 
|---|
| [b798713] | 137 | 
 | 
|---|
| [75f3522] | 138 |         {
 | 
|---|
| [c81ebf9] | 139 |                 // Setup preemption data
 | 
|---|
 | 140 |                 preemption_scope scope = { this };
 | 
|---|
 | 141 | 
 | 
|---|
| [4069faad] | 142 |                 __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
 | 
|---|
| [8118303] | 143 | 
 | 
|---|
| [ac2b598] | 144 |                 $thread * readyThread = 0p;
 | 
|---|
| [1eb239e4] | 145 |                 MAIN_LOOP:
 | 
|---|
 | 146 |                 for() {
 | 
|---|
| [92e7631] | 147 |                         // Try to get the next thread
 | 
|---|
| [8c50aed] | 148 |                         readyThread = __next_thread( this->cltr );
 | 
|---|
| [75f3522] | 149 | 
 | 
|---|
| [1eb239e4] | 150 |                         if( !readyThread ) {
 | 
|---|
 | 151 |                                 readyThread = __next_thread_slow( this->cltr );
 | 
|---|
 | 152 |                         }
 | 
|---|
| [4e6fb8e] | 153 | 
 | 
|---|
| [1eb239e4] | 154 |                         HALT:
 | 
|---|
 | 155 |                         if( !readyThread ) {
 | 
|---|
 | 156 |                                 // Don't block if we are done
 | 
|---|
 | 157 |                                 if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
 | 
|---|
| [c81ebf9] | 158 | 
 | 
|---|
| [1eb239e4] | 159 |                                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 160 |                                         __tls_stats()->ready.sleep.halts++;
 | 
|---|
 | 161 |                                 #endif
 | 
|---|
| [398e8e9] | 162 | 
 | 
|---|
| [1eb239e4] | 163 |                                 // Push self to idle stack
 | 
|---|
 | 164 |                                 push(this->cltr->idles, * this);
 | 
|---|
| [398e8e9] | 165 | 
 | 
|---|
| [1eb239e4] | 166 |                                 // Confirm the ready-queue is empty
 | 
|---|
 | 167 |                                 readyThread = __next_thread_slow( this->cltr );
 | 
|---|
 | 168 |                                 if( readyThread ) {
 | 
|---|
 | 169 |                                         // A thread was found, cancel the halt
 | 
|---|
 | 170 |                                         remove(this->cltr->idles, * this);
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 |                                         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 173 |                                                 __tls_stats()->ready.sleep.cancels++;
 | 
|---|
 | 174 |                                         #endif
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 |                                         // continue the mai loop
 | 
|---|
 | 177 |                                         break HALT;
 | 
|---|
 | 178 |                                 }
 | 
|---|
 | 179 | 
 | 
|---|
 | 180 |                                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 181 |                                         if(this->print_halts) {
 | 
|---|
 | 182 |                                                 __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl());
 | 
|---|
 | 183 |                                         }
 | 
|---|
 | 184 |                                 #endif
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 |                                 wait( this->idle );
 | 
|---|
 | 187 | 
 | 
|---|
 | 188 |                                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 189 |                                         if(this->print_halts) {
 | 
|---|
 | 190 |                                                 __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl());
 | 
|---|
 | 191 |                                         }
 | 
|---|
 | 192 |                                 #endif
 | 
|---|
 | 193 | 
 | 
|---|
 | 194 |                                 // We were woken up, remove self from idle
 | 
|---|
 | 195 |                                 remove(this->cltr->idles, * this);
 | 
|---|
 | 196 | 
 | 
|---|
 | 197 |                                 // DON'T just proceed, start looking again
 | 
|---|
 | 198 |                                 continue MAIN_LOOP;
 | 
|---|
| [64a7146] | 199 |                         }
 | 
|---|
| [1eb239e4] | 200 | 
 | 
|---|
 | 201 |                         /* paranoid */ verify( readyThread );
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |                         // We found a thread run it
 | 
|---|
 | 204 |                         __run_thread(this, readyThread);
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 |                         // Are we done?
 | 
|---|
 | 207 |                         if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
 | 
|---|
| [c81ebf9] | 208 |                 }
 | 
|---|
 | 209 | 
 | 
|---|
| [4069faad] | 210 |                 __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
 | 
|---|
| [c84e80a] | 211 |         }
 | 
|---|
| [8118303] | 212 | 
 | 
|---|
| [4cedd9f] | 213 |         V( this->terminated );
 | 
|---|
| [bdeba0b] | 214 | 
 | 
|---|
| [28d73c1] | 215 |         if(this == mainProcessor) {
 | 
|---|
| [6a490b2] | 216 |                 // HACK : the coroutine context switch expects this_thread to be set
 | 
|---|
 | 217 |                 // and it make sense for it to be set in all other cases except here
 | 
|---|
 | 218 |                 // fake it
 | 
|---|
 | 219 |                 kernelTLS.this_thread = mainThread;
 | 
|---|
 | 220 |         }
 | 
|---|
| [7768b8d] | 221 | 
 | 
|---|
| [4069faad] | 222 |         __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
 | 
|---|
| [c84e80a] | 223 | }
 | 
|---|
 | 224 | 
 | 
|---|
| [5c1a531] | 225 | static int * __volatile_errno() __attribute__((noinline));
 | 
|---|
 | 226 | static int * __volatile_errno() { asm(""); return &errno; }
 | 
|---|
 | 227 | 
 | 
|---|
| [14a61b5] | 228 | // KERNEL ONLY
 | 
|---|
| [1c273d0] | 229 | // runThread runs a thread by context switching
 | 
|---|
 | 230 | // from the processor coroutine to the target thread
 | 
|---|
| [ac2b598] | 231 | static void __run_thread(processor * this, $thread * thrd_dst) {
 | 
|---|
| [1eb239e4] | 232 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 233 |         /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted);
 | 
|---|
 | 234 |         /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next );
 | 
|---|
 | 235 |         __builtin_prefetch( thrd_dst->context.SP );
 | 
|---|
 | 236 | 
 | 
|---|
| [ac2b598] | 237 |         $coroutine * proc_cor = get_coroutine(this->runner);
 | 
|---|
| [8fcbb4c] | 238 | 
 | 
|---|
| [14a61b5] | 239 |         // Update global state
 | 
|---|
| [e8e457e] | 240 |         kernelTLS.this_thread = thrd_dst;
 | 
|---|
 | 241 | 
 | 
|---|
| [9f575ea] | 242 |         // set state of processor coroutine to inactive
 | 
|---|
 | 243 |         verify(proc_cor->state == Active);
 | 
|---|
| [ae7be7a] | 244 |         proc_cor->state = Blocked;
 | 
|---|
| [e8e457e] | 245 | 
 | 
|---|
| [9f575ea] | 246 |         // Actually run the thread
 | 
|---|
| [3381ed7] | 247 |         RUNNING:  while(true) {
 | 
|---|
| [ff79d5e] | 248 |                 thrd_dst->preempted = __NO_PREEMPTION;
 | 
|---|
 | 249 |                 thrd_dst->state = Active;
 | 
|---|
| [e8e457e] | 250 | 
 | 
|---|
| [ae66348] | 251 |                 __cfaabi_dbg_debug_do(
 | 
|---|
| [276ae57e] | 252 |                         thrd_dst->park_stale   = true;
 | 
|---|
 | 253 |                         thrd_dst->unpark_stale = true;
 | 
|---|
| [ae66348] | 254 |                 )
 | 
|---|
| [75f3522] | 255 | 
 | 
|---|
| [3381ed7] | 256 |                 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [a7b486b] | 257 |                 /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
 | 
|---|
| [210b8b3] | 258 |                 /* 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
 | 
|---|
 | 259 |                 /* 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] | 260 | 
 | 
|---|
| [9f575ea] | 261 |                 // set context switch to the thread that the processor is executing
 | 
|---|
 | 262 |                 verify( thrd_dst->context.SP );
 | 
|---|
| [c7a900a] | 263 |                 __cfactx_switch( &proc_cor->context, &thrd_dst->context );
 | 
|---|
 | 264 |                 // when __cfactx_switch returns we are back in the processor coroutine
 | 
|---|
| [9f575ea] | 265 | 
 | 
|---|
| [210b8b3] | 266 |                 /* 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 );
 | 
|---|
 | 267 |                 /* 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] | 268 |                 /* paranoid */ verify( kernelTLS.this_thread == thrd_dst );
 | 
|---|
| [3381ed7] | 269 |                 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [75f3522] | 270 | 
 | 
|---|
| [3381ed7] | 271 | 
 | 
|---|
 | 272 |                 // We just finished running a thread, there are a few things that could have happened.
 | 
|---|
 | 273 |                 // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
 | 
|---|
 | 274 |                 // 2 - Racy case    : the thread has blocked but someone has already tried to schedule it.
 | 
|---|
 | 275 |                 // 4 - Preempted
 | 
|---|
 | 276 |                 // In case 1, we may have won a race so we can't write to the state again.
 | 
|---|
 | 277 |                 // In case 2, we lost the race so we now own the thread.
 | 
|---|
 | 278 | 
 | 
|---|
 | 279 |                 if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
 | 
|---|
 | 280 |                         // The thread was preempted, reschedule it and reset the flag
 | 
|---|
| [9b1dcc2] | 281 |                         __schedule_thread( (__processor_id_t*)this, thrd_dst );
 | 
|---|
| [3381ed7] | 282 |                         break RUNNING;
 | 
|---|
 | 283 |                 }
 | 
|---|
| [75f3522] | 284 | 
 | 
|---|
| [ff79d5e] | 285 |                 if(unlikely(thrd_dst->state == Halted)) {
 | 
|---|
 | 286 |                         // The thread has halted, it should never be scheduled/run again
 | 
|---|
 | 287 |                         // We may need to wake someone up here since
 | 
|---|
 | 288 |                         unpark( this->destroyer __cfaabi_dbg_ctx2 );
 | 
|---|
 | 289 |                         this->destroyer = 0p;
 | 
|---|
 | 290 |                         break RUNNING;
 | 
|---|
 | 291 |                 }
 | 
|---|
 | 292 | 
 | 
|---|
 | 293 |                 /* paranoid */ verify( thrd_dst->state == Active );
 | 
|---|
 | 294 |                 thrd_dst->state = Blocked;
 | 
|---|
 | 295 | 
 | 
|---|
| [3381ed7] | 296 |                 // set state of processor coroutine to active and the thread to inactive
 | 
|---|
| [ff79d5e] | 297 |                 int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST);
 | 
|---|
 | 298 |                 __cfaabi_dbg_debug_do( thrd_dst->park_result = old_ticket; )
 | 
|---|
 | 299 |                 switch(old_ticket) {
 | 
|---|
 | 300 |                         case 1:
 | 
|---|
| [3381ed7] | 301 |                                 // This is case 1, the regular case, nothing more is needed
 | 
|---|
 | 302 |                                 break RUNNING;
 | 
|---|
| [ff79d5e] | 303 |                         case 2:
 | 
|---|
| [3381ed7] | 304 |                                 // This is case 2, the racy case, someone tried to run this thread before it finished blocking
 | 
|---|
 | 305 |                                 // In this case, just run it again.
 | 
|---|
 | 306 |                                 continue RUNNING;
 | 
|---|
 | 307 |                         default:
 | 
|---|
 | 308 |                                 // This makes no sense, something is wrong abort
 | 
|---|
| [ff79d5e] | 309 |                                 abort();
 | 
|---|
| [3381ed7] | 310 |                 }
 | 
|---|
| [9f575ea] | 311 |         }
 | 
|---|
| [e8e457e] | 312 | 
 | 
|---|
| [9f575ea] | 313 |         // Just before returning to the processor, set the processor coroutine to active
 | 
|---|
| [e8e457e] | 314 |         proc_cor->state = Active;
 | 
|---|
| [ae7be7a] | 315 |         kernelTLS.this_thread = 0p;
 | 
|---|
| [1eb239e4] | 316 | 
 | 
|---|
 | 317 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [82c948c] | 318 | }
 | 
|---|
 | 319 | 
 | 
|---|
| [14a61b5] | 320 | // KERNEL_ONLY
 | 
|---|
| [b0c7419] | 321 | void returnToKernel() {
 | 
|---|
| [3381ed7] | 322 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [ac2b598] | 323 |         $coroutine * proc_cor = get_coroutine(kernelTLS.this_processor->runner);
 | 
|---|
 | 324 |         $thread * thrd_src = kernelTLS.this_thread;
 | 
|---|
| [e8e457e] | 325 | 
 | 
|---|
| [29cb302] | 326 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 327 |                 struct processor * last_proc = kernelTLS.this_processor;
 | 
|---|
 | 328 |         #endif
 | 
|---|
 | 329 | 
 | 
|---|
| [9f575ea] | 330 |         // Run the thread on this processor
 | 
|---|
 | 331 |         {
 | 
|---|
 | 332 |                 int local_errno = *__volatile_errno();
 | 
|---|
 | 333 |                 #if defined( __i386 ) || defined( __x86_64 )
 | 
|---|
 | 334 |                         __x87_store;
 | 
|---|
 | 335 |                 #endif
 | 
|---|
 | 336 |                 verify( proc_cor->context.SP );
 | 
|---|
| [c7a900a] | 337 |                 __cfactx_switch( &thrd_src->context, &proc_cor->context );
 | 
|---|
| [9f575ea] | 338 |                 #if defined( __i386 ) || defined( __x86_64 )
 | 
|---|
 | 339 |                         __x87_load;
 | 
|---|
 | 340 |                 #endif
 | 
|---|
 | 341 |                 *__volatile_errno() = local_errno;
 | 
|---|
| [8fcbb4c] | 342 |         }
 | 
|---|
| [deca0f5] | 343 | 
 | 
|---|
| [29cb302] | 344 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 345 |                 if(last_proc != kernelTLS.this_processor) {
 | 
|---|
 | 346 |                         __tls_stats()->ready.threads.migration++;
 | 
|---|
 | 347 |                 }
 | 
|---|
 | 348 |         #endif
 | 
|---|
 | 349 | 
 | 
|---|
| [3381ed7] | 350 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [210b8b3] | 351 |         /* 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 );
 | 
|---|
 | 352 |         /* 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] | 353 | }
 | 
|---|
 | 354 | 
 | 
|---|
| [8def349] | 355 | //-----------------------------------------------------------------------------
 | 
|---|
 | 356 | // Scheduler routines
 | 
|---|
| [14a61b5] | 357 | // KERNEL ONLY
 | 
|---|
| [9b1dcc2] | 358 | void __schedule_thread( struct __processor_id_t * id, $thread * thrd ) {
 | 
|---|
| [6a490b2] | 359 |         /* paranoid */ verify( thrd );
 | 
|---|
 | 360 |         /* paranoid */ verify( thrd->state != Halted );
 | 
|---|
| [9f575ea] | 361 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [3381ed7] | 362 |         /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
 | 
|---|
| [504a7dc] | 363 |         /* paranoid */  if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
 | 
|---|
 | 364 |                                         "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
 | 
|---|
| [ff79d5e] | 365 |         /* paranoid */  if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
 | 
|---|
| [504a7dc] | 366 |                                         "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
 | 
|---|
| [3381ed7] | 367 |         /* paranoid */ #endif
 | 
|---|
| [6a490b2] | 368 |         /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
 | 
|---|
| [9f575ea] | 369 | 
 | 
|---|
| [ae7be7a] | 370 |         if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
 | 
|---|
| [6b4cdd3] | 371 | 
 | 
|---|
| [9b1dcc2] | 372 |         ready_schedule_lock  ( id );
 | 
|---|
| [504a7dc] | 373 |                 push( thrd->curr_cluster, thrd );
 | 
|---|
| [1eb239e4] | 374 |                 __wake_one(id, thrd->curr_cluster);
 | 
|---|
| [9b1dcc2] | 375 |         ready_schedule_unlock( id );
 | 
|---|
| [1c273d0] | 376 | 
 | 
|---|
| [9f575ea] | 377 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [db6f06a] | 378 | }
 | 
|---|
 | 379 | 
 | 
|---|
| [14a61b5] | 380 | // KERNEL ONLY
 | 
|---|
| [1eb239e4] | 381 | static inline $thread * __next_thread(cluster * this) with( *this ) {
 | 
|---|
| [3381ed7] | 382 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [7768b8d] | 383 | 
 | 
|---|
| [9b1dcc2] | 384 |         ready_schedule_lock  ( (__processor_id_t*)kernelTLS.this_processor );
 | 
|---|
| [1eb239e4] | 385 |                 $thread * thrd = pop( this );
 | 
|---|
| [9b1dcc2] | 386 |         ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor );
 | 
|---|
| [7768b8d] | 387 | 
 | 
|---|
| [3381ed7] | 388 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [1eb239e4] | 389 |         return thrd;
 | 
|---|
| [eb2e723] | 390 | }
 | 
|---|
 | 391 | 
 | 
|---|
| [64a7146] | 392 | // KERNEL ONLY
 | 
|---|
| [1eb239e4] | 393 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) {
 | 
|---|
| [64a7146] | 394 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 395 | 
 | 
|---|
 | 396 |         ready_schedule_lock  ( (__processor_id_t*)kernelTLS.this_processor );
 | 
|---|
| [1eb239e4] | 397 |                 $thread * thrd = pop_slow( this );
 | 
|---|
| [64a7146] | 398 |         ready_schedule_unlock( (__processor_id_t*)kernelTLS.this_processor );
 | 
|---|
 | 399 | 
 | 
|---|
 | 400 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [1eb239e4] | 401 |         return thrd;
 | 
|---|
| [64a7146] | 402 | }
 | 
|---|
 | 403 | 
 | 
|---|
| [2d8f7b0] | 404 | // KERNEL ONLY unpark with out disabling interrupts
 | 
|---|
| [9b1dcc2] | 405 | void __unpark(  struct __processor_id_t * id, $thread * thrd __cfaabi_dbg_ctx_param2 ) {
 | 
|---|
| [ae66348] | 406 |         // record activity
 | 
|---|
 | 407 |         __cfaabi_dbg_record_thrd( *thrd, false, caller );
 | 
|---|
 | 408 | 
 | 
|---|
| [ff79d5e] | 409 |         int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
 | 
|---|
 | 410 |         __cfaabi_dbg_debug_do( thrd->unpark_result = old_ticket; thrd->unpark_state = thrd->state; )
 | 
|---|
 | 411 |         switch(old_ticket) {
 | 
|---|
 | 412 |                 case 1:
 | 
|---|
| [3381ed7] | 413 |                         // Wake won the race, the thread will reschedule/rerun itself
 | 
|---|
 | 414 |                         break;
 | 
|---|
| [ff79d5e] | 415 |                 case 0:
 | 
|---|
| [3381ed7] | 416 |                         /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
 | 
|---|
| [ff79d5e] | 417 |                         /* paranoid */ verify( thrd->state == Blocked );
 | 
|---|
| [0b33412] | 418 | 
 | 
|---|
| [3381ed7] | 419 |                         // Wake lost the race,
 | 
|---|
| [9b1dcc2] | 420 |                         __schedule_thread( id, thrd );
 | 
|---|
| [3381ed7] | 421 |                         break;
 | 
|---|
 | 422 |                 default:
 | 
|---|
 | 423 |                         // This makes no sense, something is wrong abort
 | 
|---|
 | 424 |                         abort();
 | 
|---|
| [de6319f] | 425 |         }
 | 
|---|
| [db6f06a] | 426 | }
 | 
|---|
 | 427 | 
 | 
|---|
| [2d8f7b0] | 428 | void unpark( $thread * thrd __cfaabi_dbg_ctx_param2 ) {
 | 
|---|
 | 429 |         if( !thrd ) return;
 | 
|---|
| [db6f06a] | 430 | 
 | 
|---|
| [82ff5845] | 431 |         disable_interrupts();
 | 
|---|
| [9b1dcc2] | 432 |         __unpark( (__processor_id_t*)kernelTLS.this_processor, thrd __cfaabi_dbg_ctx_fwd2 );
 | 
|---|
| [36982fc] | 433 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| [eb2e723] | 434 | }
 | 
|---|
 | 435 | 
 | 
|---|
| [ae66348] | 436 | void park( __cfaabi_dbg_ctx_param ) {
 | 
|---|
| [3381ed7] | 437 |         /* paranoid */ verify( kernelTLS.preemption_state.enabled );
 | 
|---|
| [82ff5845] | 438 |         disable_interrupts();
 | 
|---|
| [3381ed7] | 439 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 440 |         /* paranoid */ verify( kernelTLS.this_thread->preempted == __NO_PREEMPTION );
 | 
|---|
| [0b33412] | 441 | 
 | 
|---|
| [ae66348] | 442 |         // record activity
 | 
|---|
 | 443 |         __cfaabi_dbg_record_thrd( *kernelTLS.this_thread, true, caller );
 | 
|---|
| [0b33412] | 444 | 
 | 
|---|
| [82c948c] | 445 |         returnToKernel();
 | 
|---|
| [0b33412] | 446 | 
 | 
|---|
| [3381ed7] | 447 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [36982fc] | 448 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| [3381ed7] | 449 |         /* paranoid */ verify( kernelTLS.preemption_state.enabled );
 | 
|---|
| [0c78741] | 450 | 
 | 
|---|
 | 451 | }
 | 
|---|
| [09800e9] | 452 | 
 | 
|---|
| [3381ed7] | 453 | // KERNEL ONLY
 | 
|---|
| [b0c7419] | 454 | void __leave_thread() {
 | 
|---|
| [3381ed7] | 455 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [09800e9] | 456 |         returnToKernel();
 | 
|---|
| [b0c7419] | 457 |         abort();
 | 
|---|
| [09800e9] | 458 | }
 | 
|---|
 | 459 | 
 | 
|---|
| [14a61b5] | 460 | // KERNEL ONLY
 | 
|---|
| [3381ed7] | 461 | bool force_yield( __Preemption_Reason reason ) {
 | 
|---|
 | 462 |         /* paranoid */ verify( kernelTLS.preemption_state.enabled );
 | 
|---|
| [09800e9] | 463 |         disable_interrupts();
 | 
|---|
| [3381ed7] | 464 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [09800e9] | 465 | 
 | 
|---|
| [ac2b598] | 466 |         $thread * thrd = kernelTLS.this_thread;
 | 
|---|
| [ff79d5e] | 467 |         /* paranoid */ verify(thrd->state == Active);
 | 
|---|
| [3381ed7] | 468 | 
 | 
|---|
 | 469 |         // SKULLDUGGERY: It is possible that we are preempting this thread just before
 | 
|---|
 | 470 |         // it was going to park itself. If that is the case and it is already using the
 | 
|---|
 | 471 |         // intrusive fields then we can't use them to preempt the thread
 | 
|---|
 | 472 |         // If that is the case, abandon the preemption.
 | 
|---|
 | 473 |         bool preempted = false;
 | 
|---|
| [504a7dc] | 474 |         if(thrd->link.next == 0p) {
 | 
|---|
| [3381ed7] | 475 |                 preempted = true;
 | 
|---|
 | 476 |                 thrd->preempted = reason;
 | 
|---|
 | 477 |                 returnToKernel();
 | 
|---|
| [de6319f] | 478 |         }
 | 
|---|
| [f2b12406] | 479 | 
 | 
|---|
| [3381ed7] | 480 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 481 |         enable_interrupts_noPoll();
 | 
|---|
 | 482 |         /* paranoid */ verify( kernelTLS.preemption_state.enabled );
 | 
|---|
| [f2b12406] | 483 | 
 | 
|---|
| [3381ed7] | 484 |         return preempted;
 | 
|---|
| [f2b12406] | 485 | }
 | 
|---|
 | 486 | 
 | 
|---|
| [14a61b5] | 487 | //=============================================================================================
 | 
|---|
| [92e7631] | 488 | // Kernel Idle Sleep
 | 
|---|
| [14a61b5] | 489 | //=============================================================================================
 | 
|---|
| [64a7146] | 490 | // Wake a thread from the front if there are any
 | 
|---|
| [1eb239e4] | 491 | static void __wake_one(struct __processor_id_t * id, cluster * this) {
 | 
|---|
 | 492 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
| [64a7146] | 493 |         /* paranoid */ verify( ready_schedule_islocked( id ) );
 | 
|---|
| [14a61b5] | 494 | 
 | 
|---|
| [64a7146] | 495 |         // Check if there is a sleeping processor
 | 
|---|
| [1eb239e4] | 496 |         processor * p;
 | 
|---|
 | 497 |         unsigned idle;
 | 
|---|
 | 498 |         unsigned total;
 | 
|---|
 | 499 |         [idle, total, p] = query(this->idles);
 | 
|---|
| [14a61b5] | 500 | 
 | 
|---|
| [64a7146] | 501 |         // If no one is sleeping, we are done
 | 
|---|
| [1eb239e4] | 502 |         if( idle == 0 ) return;
 | 
|---|
| [14a61b5] | 503 | 
 | 
|---|
| [64a7146] | 504 |         // We found a processor, wake it up
 | 
|---|
 | 505 |         post( p->idle );
 | 
|---|
| [14a61b5] | 506 | 
 | 
|---|
| [1eb239e4] | 507 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 508 |                 __tls_stats()->ready.sleep.wakes++;
 | 
|---|
 | 509 |         #endif
 | 
|---|
 | 510 | 
 | 
|---|
 | 511 |         /* paranoid */ verify( ready_schedule_islocked( id ) );
 | 
|---|
 | 512 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 513 | 
 | 
|---|
 | 514 |         return;
 | 
|---|
| [64a7146] | 515 | }
 | 
|---|
| [14a61b5] | 516 | 
 | 
|---|
| [64a7146] | 517 | // Unconditionnaly wake a thread
 | 
|---|
| [1eb239e4] | 518 | void __wake_proc(processor * this) {
 | 
|---|
| [64a7146] | 519 |         __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
 | 
|---|
| [14a61b5] | 520 | 
 | 
|---|
| [64a7146] | 521 |         disable_interrupts();
 | 
|---|
 | 522 |                 /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 523 |                 bool ret = post( this->idle );
 | 
|---|
 | 524 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| [92e7631] | 525 | }
 | 
|---|
 | 526 | 
 | 
|---|
| [1eb239e4] | 527 | static void push  (__cluster_idles & this, processor & proc) {
 | 
|---|
 | 528 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 529 |         lock( this );
 | 
|---|
 | 530 |                 this.idle++;
 | 
|---|
 | 531 |                 /* paranoid */ verify( this.idle <= this.total );
 | 
|---|
| [8e16177] | 532 | 
 | 
|---|
| [1eb239e4] | 533 |                 insert_first(this.list, proc);
 | 
|---|
 | 534 |         unlock( this );
 | 
|---|
 | 535 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 536 | }
 | 
|---|
| [8e16177] | 537 | 
 | 
|---|
| [1eb239e4] | 538 | static void remove(__cluster_idles & this, processor & proc) {
 | 
|---|
 | 539 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 540 |         lock( this );
 | 
|---|
 | 541 |                 this.idle--;
 | 
|---|
 | 542 |                 /* paranoid */ verify( this.idle >= 0 );
 | 
|---|
| [c34ebf2] | 543 | 
 | 
|---|
| [1eb239e4] | 544 |                 remove(proc);
 | 
|---|
 | 545 |         unlock( this );
 | 
|---|
 | 546 |         /* paranoid */ verify( ! kernelTLS.preemption_state.enabled );
 | 
|---|
 | 547 | }
 | 
|---|
| [c34ebf2] | 548 | 
 | 
|---|
| [1eb239e4] | 549 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles this ) {
 | 
|---|
 | 550 |         for() {
 | 
|---|
 | 551 |                 uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST);
 | 
|---|
 | 552 |                 if( 1 == (l % 2) ) { Pause(); continue; }
 | 
|---|
 | 553 |                 unsigned idle    = this.idle;
 | 
|---|
 | 554 |                 unsigned total   = this.total;
 | 
|---|
 | 555 |                 processor * proc = &this.list`first;
 | 
|---|
| [7fdae38] | 556 |                 // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it
 | 
|---|
 | 557 |                 asm volatile("": : :"memory");
 | 
|---|
| [1eb239e4] | 558 |                 if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; }
 | 
|---|
 | 559 |                 return [idle, total, proc];
 | 
|---|
 | 560 |         }
 | 
|---|
| [6b4cdd3] | 561 | }
 | 
|---|
 | 562 | 
 | 
|---|
| [dbe9b08] | 563 | //=============================================================================================
 | 
|---|
 | 564 | // Unexpected Terminating logic
 | 
|---|
 | 565 | //=============================================================================================
 | 
|---|
| [ea7d2b0] | 566 | static __spinlock_t kernel_abort_lock;
 | 
|---|
| [9d944b2] | 567 | static bool kernel_abort_called = false;
 | 
|---|
 | 568 | 
 | 
|---|
| [afd550c] | 569 | void * kernel_abort(void) __attribute__ ((__nothrow__)) {
 | 
|---|
| [9d944b2] | 570 |         // abort cannot be recursively entered by the same or different processors because all signal handlers return when
 | 
|---|
 | 571 |         // the globalAbort flag is true.
 | 
|---|
| [36982fc] | 572 |         lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
 | 
|---|
| [9d944b2] | 573 | 
 | 
|---|
 | 574 |         // first task to abort ?
 | 
|---|
| [de94a60] | 575 |         if ( kernel_abort_called ) {                    // not first task to abort ?
 | 
|---|
| [ea7d2b0] | 576 |                 unlock( kernel_abort_lock );
 | 
|---|
| [1c273d0] | 577 | 
 | 
|---|
| [9d944b2] | 578 |                 sigset_t mask;
 | 
|---|
 | 579 |                 sigemptyset( &mask );
 | 
|---|
| [de94a60] | 580 |                 sigaddset( &mask, SIGALRM );            // block SIGALRM signals
 | 
|---|
| [8a13c47] | 581 |                 sigaddset( &mask, SIGUSR1 );            // block SIGALRM signals
 | 
|---|
 | 582 |                 sigsuspend( &mask );                            // block the processor to prevent further damage during abort
 | 
|---|
 | 583 |                 _exit( EXIT_FAILURE );                          // if processor unblocks before it is killed, terminate it
 | 
|---|
| [de94a60] | 584 |         }
 | 
|---|
 | 585 |         else {
 | 
|---|
 | 586 |                 kernel_abort_called = true;
 | 
|---|
 | 587 |                 unlock( kernel_abort_lock );
 | 
|---|
| [9d944b2] | 588 |         }
 | 
|---|
 | 589 | 
 | 
|---|
| [14a61b5] | 590 |         return kernelTLS.this_thread;
 | 
|---|
| [9d944b2] | 591 | }
 | 
|---|
 | 592 | 
 | 
|---|
 | 593 | void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
 | 
|---|
| [b81fd95] | 594 |         $thread * thrd = ( $thread * ) kernel_data;
 | 
|---|
| [9d944b2] | 595 | 
 | 
|---|
| [de94a60] | 596 |         if(thrd) {
 | 
|---|
 | 597 |                 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
 | 
|---|
| [1c40091] | 598 |                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| [de94a60] | 599 | 
 | 
|---|
| [212c2187] | 600 |                 if ( &thrd->self_cor != thrd->curr_cor ) {
 | 
|---|
 | 601 |                         len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
 | 
|---|
| [1c40091] | 602 |                         __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| [de94a60] | 603 |                 }
 | 
|---|
 | 604 |                 else {
 | 
|---|
| [1c40091] | 605 |                         __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
 | 
|---|
| [de94a60] | 606 |                 }
 | 
|---|
| [1c273d0] | 607 |         }
 | 
|---|
| [9d944b2] | 608 |         else {
 | 
|---|
| [de94a60] | 609 |                 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
 | 
|---|
| [1c40091] | 610 |                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| [9d944b2] | 611 |         }
 | 
|---|
 | 612 | }
 | 
|---|
 | 613 | 
 | 
|---|
| [2b8bc41] | 614 | int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
 | 
|---|
| [14a61b5] | 615 |         return get_coroutine(kernelTLS.this_thread) == get_coroutine(mainThread) ? 4 : 2;
 | 
|---|
| [2b8bc41] | 616 | }
 | 
|---|
 | 617 | 
 | 
|---|
| [de94a60] | 618 | static __spinlock_t kernel_debug_lock;
 | 
|---|
 | 619 | 
 | 
|---|
| [9d944b2] | 620 | extern "C" {
 | 
|---|
| [1c40091] | 621 |         void __cfaabi_bits_acquire() {
 | 
|---|
| [36982fc] | 622 |                 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
 | 
|---|
| [9d944b2] | 623 |         }
 | 
|---|
 | 624 | 
 | 
|---|
| [1c40091] | 625 |         void __cfaabi_bits_release() {
 | 
|---|
| [ea7d2b0] | 626 |                 unlock( kernel_debug_lock );
 | 
|---|
| [9d944b2] | 627 |         }
 | 
|---|
| [8118303] | 628 | }
 | 
|---|
 | 629 | 
 | 
|---|
| [fa21ac9] | 630 | //=============================================================================================
 | 
|---|
 | 631 | // Kernel Utilities
 | 
|---|
 | 632 | //=============================================================================================
 | 
|---|
| [bd98b58] | 633 | //-----------------------------------------------------------------------------
 | 
|---|
 | 634 | // Locks
 | 
|---|
| [242a902] | 635 | void  ?{}( semaphore & this, int count = 1 ) {
 | 
|---|
 | 636 |         (this.lock){};
 | 
|---|
 | 637 |         this.count = count;
 | 
|---|
 | 638 |         (this.waiting){};
 | 
|---|
| [db6f06a] | 639 | }
 | 
|---|
| [242a902] | 640 | void ^?{}(semaphore & this) {}
 | 
|---|
| [db6f06a] | 641 | 
 | 
|---|
| [71c8b7e] | 642 | bool P(semaphore & this) with( this ){
 | 
|---|
| [65deb18] | 643 |         lock( lock __cfaabi_dbg_ctx2 );
 | 
|---|
 | 644 |         count -= 1;
 | 
|---|
 | 645 |         if ( count < 0 ) {
 | 
|---|
| [bdeba0b] | 646 |                 // queue current task
 | 
|---|
| [14a61b5] | 647 |                 append( waiting, kernelTLS.this_thread );
 | 
|---|
| [bdeba0b] | 648 | 
 | 
|---|
 | 649 |                 // atomically release spin lock and block
 | 
|---|
| [3381ed7] | 650 |                 unlock( lock );
 | 
|---|
| [ae66348] | 651 |                 park( __cfaabi_dbg_ctx );
 | 
|---|
| [71c8b7e] | 652 |                 return true;
 | 
|---|
| [8def349] | 653 |         }
 | 
|---|
| [4e6fb8e] | 654 |         else {
 | 
|---|
| [65deb18] | 655 |             unlock( lock );
 | 
|---|
| [71c8b7e] | 656 |             return false;
 | 
|---|
| [4e6fb8e] | 657 |         }
 | 
|---|
| [bd98b58] | 658 | }
 | 
|---|
 | 659 | 
 | 
|---|
| [f0ce5f4] | 660 | bool V(semaphore & this) with( this ) {
 | 
|---|
| [ac2b598] | 661 |         $thread * thrd = 0p;
 | 
|---|
| [65deb18] | 662 |         lock( lock __cfaabi_dbg_ctx2 );
 | 
|---|
 | 663 |         count += 1;
 | 
|---|
 | 664 |         if ( count <= 0 ) {
 | 
|---|
| [bdeba0b] | 665 |                 // remove task at head of waiting list
 | 
|---|
| [65deb18] | 666 |                 thrd = pop_head( waiting );
 | 
|---|
| [bd98b58] | 667 |         }
 | 
|---|
| [bdeba0b] | 668 | 
 | 
|---|
| [65deb18] | 669 |         unlock( lock );
 | 
|---|
| [bdeba0b] | 670 | 
 | 
|---|
 | 671 |         // make new owner
 | 
|---|
| [ae66348] | 672 |         unpark( thrd __cfaabi_dbg_ctx2 );
 | 
|---|
| [f0ce5f4] | 673 | 
 | 
|---|
| [d384787] | 674 |         return thrd != 0p;
 | 
|---|
 | 675 | }
 | 
|---|
 | 676 | 
 | 
|---|
 | 677 | bool V(semaphore & this, unsigned diff) with( this ) {
 | 
|---|
 | 678 |         $thread * thrd = 0p;
 | 
|---|
 | 679 |         lock( lock __cfaabi_dbg_ctx2 );
 | 
|---|
 | 680 |         int release = max(-count, (int)diff);
 | 
|---|
 | 681 |         count += diff;
 | 
|---|
 | 682 |         for(release) {
 | 
|---|
 | 683 |                 unpark( pop_head( waiting ) __cfaabi_dbg_ctx2 );
 | 
|---|
 | 684 |         }
 | 
|---|
 | 685 | 
 | 
|---|
 | 686 |         unlock( lock );
 | 
|---|
 | 687 | 
 | 
|---|
| [f0ce5f4] | 688 |         return thrd != 0p;
 | 
|---|
| [bd98b58] | 689 | }
 | 
|---|
 | 690 | 
 | 
|---|
| [de94a60] | 691 | //-----------------------------------------------------------------------------
 | 
|---|
 | 692 | // Debug
 | 
|---|
 | 693 | __cfaabi_dbg_debug_do(
 | 
|---|
| [1997b4e] | 694 |         extern "C" {
 | 
|---|
| [ae66348] | 695 |                 void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
 | 
|---|
| [1997b4e] | 696 |                         this.prev_name = prev_name;
 | 
|---|
 | 697 |                         this.prev_thrd = kernelTLS.this_thread;
 | 
|---|
 | 698 |                 }
 | 
|---|
| [ae66348] | 699 | 
 | 
|---|
 | 700 |                 void __cfaabi_dbg_record_thrd($thread & this, bool park, const char prev_name[]) {
 | 
|---|
 | 701 |                         if(park) {
 | 
|---|
| [f0ce5f4] | 702 |                                 this.park_caller   = prev_name;
 | 
|---|
 | 703 |                                 this.park_stale    = false;
 | 
|---|
| [ae66348] | 704 |                         }
 | 
|---|
 | 705 |                         else {
 | 
|---|
| [f0ce5f4] | 706 |                                 this.unpark_caller = prev_name;
 | 
|---|
 | 707 |                                 this.unpark_stale  = false;
 | 
|---|
| [ae66348] | 708 |                         }
 | 
|---|
 | 709 |                 }
 | 
|---|
| [9181f1d] | 710 |         }
 | 
|---|
| [f7d6bb0] | 711 | )
 | 
|---|
| [2026bb6] | 712 | 
 | 
|---|
 | 713 | //-----------------------------------------------------------------------------
 | 
|---|
 | 714 | // Debug
 | 
|---|
| [8c50aed] | 715 | bool threading_enabled(void) __attribute__((const)) {
 | 
|---|
| [2026bb6] | 716 |         return true;
 | 
|---|
 | 717 | }
 | 
|---|
| [c34ebf2] | 718 | 
 | 
|---|
 | 719 | //-----------------------------------------------------------------------------
 | 
|---|
 | 720 | // Statistics
 | 
|---|
 | 721 | #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 722 |         void print_halts( processor & this ) {
 | 
|---|
 | 723 |                 this.print_halts = true;
 | 
|---|
 | 724 |         }
 | 
|---|
 | 725 | #endif
 | 
|---|
| [8118303] | 726 | // Local Variables: //
 | 
|---|
 | 727 | // mode: c //
 | 
|---|
 | 728 | // tab-width: 4 //
 | 
|---|
 | 729 | // End: //
 | 
|---|