| [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);
 | 
|---|
| [e873838] | 110 | static void __wake_one(cluster * cltr);
 | 
|---|
| [1eb239e4] | 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
 | 
|---|
| [8fc652e0] | 124 |         __cfaabi_tls.rand_seed ^= rdtscl();
 | 
|---|
 | 125 |         __cfaabi_tls.ready_rng.fwd_seed = 25214903917_l64u * (rdtscl() ^ (uintptr_t)&runner);
 | 
|---|
| [f2384c9a] | 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 | 
 | 
|---|
| [325e6ea] | 142 |                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 143 |                         unsigned long long last_tally = rdtscl();
 | 
|---|
 | 144 |                 #endif
 | 
|---|
 | 145 | 
 | 
|---|
 | 146 | 
 | 
|---|
| [4069faad] | 147 |                 __cfadbg_print_safe(runtime_core, "Kernel : core %p started\n", this);
 | 
|---|
| [8118303] | 148 | 
 | 
|---|
| [ac2b598] | 149 |                 $thread * readyThread = 0p;
 | 
|---|
| [1eb239e4] | 150 |                 MAIN_LOOP:
 | 
|---|
 | 151 |                 for() {
 | 
|---|
| [92e7631] | 152 |                         // Try to get the next thread
 | 
|---|
| [8c50aed] | 153 |                         readyThread = __next_thread( this->cltr );
 | 
|---|
| [75f3522] | 154 | 
 | 
|---|
| [1eb239e4] | 155 |                         if( !readyThread ) {
 | 
|---|
 | 156 |                                 readyThread = __next_thread_slow( this->cltr );
 | 
|---|
 | 157 |                         }
 | 
|---|
| [4e6fb8e] | 158 | 
 | 
|---|
| [1eb239e4] | 159 |                         HALT:
 | 
|---|
 | 160 |                         if( !readyThread ) {
 | 
|---|
 | 161 |                                 // Don't block if we are done
 | 
|---|
 | 162 |                                 if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
 | 
|---|
| [c81ebf9] | 163 | 
 | 
|---|
| [1eb239e4] | 164 |                                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 165 |                                         __tls_stats()->ready.sleep.halts++;
 | 
|---|
 | 166 |                                 #endif
 | 
|---|
| [398e8e9] | 167 | 
 | 
|---|
| [1eb239e4] | 168 |                                 // Push self to idle stack
 | 
|---|
 | 169 |                                 push(this->cltr->idles, * this);
 | 
|---|
| [398e8e9] | 170 | 
 | 
|---|
| [1eb239e4] | 171 |                                 // Confirm the ready-queue is empty
 | 
|---|
 | 172 |                                 readyThread = __next_thread_slow( this->cltr );
 | 
|---|
 | 173 |                                 if( readyThread ) {
 | 
|---|
 | 174 |                                         // A thread was found, cancel the halt
 | 
|---|
 | 175 |                                         remove(this->cltr->idles, * this);
 | 
|---|
 | 176 | 
 | 
|---|
 | 177 |                                         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 178 |                                                 __tls_stats()->ready.sleep.cancels++;
 | 
|---|
 | 179 |                                         #endif
 | 
|---|
 | 180 | 
 | 
|---|
 | 181 |                                         // continue the mai loop
 | 
|---|
 | 182 |                                         break HALT;
 | 
|---|
 | 183 |                                 }
 | 
|---|
 | 184 | 
 | 
|---|
 | 185 |                                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 186 |                                         if(this->print_halts) {
 | 
|---|
 | 187 |                                                 __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 0\n", this->id, rdtscl());
 | 
|---|
 | 188 |                                         }
 | 
|---|
 | 189 |                                 #endif
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 |                                 wait( this->idle );
 | 
|---|
 | 192 | 
 | 
|---|
 | 193 |                                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 194 |                                         if(this->print_halts) {
 | 
|---|
 | 195 |                                                 __cfaabi_bits_print_safe( STDOUT_FILENO, "PH:%d - %lld 1\n", this->id, rdtscl());
 | 
|---|
 | 196 |                                         }
 | 
|---|
 | 197 |                                 #endif
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 |                                 // We were woken up, remove self from idle
 | 
|---|
 | 200 |                                 remove(this->cltr->idles, * this);
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 |                                 // DON'T just proceed, start looking again
 | 
|---|
 | 203 |                                 continue MAIN_LOOP;
 | 
|---|
| [64a7146] | 204 |                         }
 | 
|---|
| [1eb239e4] | 205 | 
 | 
|---|
 | 206 |                         /* paranoid */ verify( readyThread );
 | 
|---|
 | 207 | 
 | 
|---|
 | 208 |                         // We found a thread run it
 | 
|---|
 | 209 |                         __run_thread(this, readyThread);
 | 
|---|
 | 210 | 
 | 
|---|
 | 211 |                         // Are we done?
 | 
|---|
 | 212 |                         if( __atomic_load_n(&this->do_terminate, __ATOMIC_SEQ_CST) ) break MAIN_LOOP;
 | 
|---|
| [325e6ea] | 213 | 
 | 
|---|
 | 214 |                         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 215 |                                 unsigned long long curr = rdtscl();
 | 
|---|
 | 216 |                                 if(curr > (last_tally + 500000000)) {
 | 
|---|
 | 217 |                                         __tally_stats(this->cltr->stats, __cfaabi_tls.this_stats);
 | 
|---|
 | 218 |                                         last_tally = curr;
 | 
|---|
 | 219 |                                 }
 | 
|---|
 | 220 |                         #endif
 | 
|---|
| [c81ebf9] | 221 |                 }
 | 
|---|
 | 222 | 
 | 
|---|
| [4069faad] | 223 |                 __cfadbg_print_safe(runtime_core, "Kernel : core %p stopping\n", this);
 | 
|---|
| [c84e80a] | 224 |         }
 | 
|---|
| [8118303] | 225 | 
 | 
|---|
| [454f478] | 226 |         post( this->terminated );
 | 
|---|
| [bdeba0b] | 227 | 
 | 
|---|
| [28d73c1] | 228 |         if(this == mainProcessor) {
 | 
|---|
| [6a490b2] | 229 |                 // HACK : the coroutine context switch expects this_thread to be set
 | 
|---|
 | 230 |                 // and it make sense for it to be set in all other cases except here
 | 
|---|
 | 231 |                 // fake it
 | 
|---|
| [8fc652e0] | 232 |                 __cfaabi_tls.this_thread = mainThread;
 | 
|---|
| [6a490b2] | 233 |         }
 | 
|---|
| [7768b8d] | 234 | 
 | 
|---|
| [4069faad] | 235 |         __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
 | 
|---|
| [c84e80a] | 236 | }
 | 
|---|
 | 237 | 
 | 
|---|
| [5c1a531] | 238 | static int * __volatile_errno() __attribute__((noinline));
 | 
|---|
 | 239 | static int * __volatile_errno() { asm(""); return &errno; }
 | 
|---|
 | 240 | 
 | 
|---|
| [14a61b5] | 241 | // KERNEL ONLY
 | 
|---|
| [1c273d0] | 242 | // runThread runs a thread by context switching
 | 
|---|
 | 243 | // from the processor coroutine to the target thread
 | 
|---|
| [ac2b598] | 244 | static void __run_thread(processor * this, $thread * thrd_dst) {
 | 
|---|
| [8fc652e0] | 245 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 246 |         /* paranoid */ verifyf( thrd_dst->state == Ready || thrd_dst->preempted != __NO_PREEMPTION, "state : %d, preempted %d\n", thrd_dst->state, thrd_dst->preempted);
 | 
|---|
 | 247 |         /* paranoid */ verifyf( thrd_dst->link.next == 0p, "Expected null got %p", thrd_dst->link.next );
 | 
|---|
 | 248 |         __builtin_prefetch( thrd_dst->context.SP );
 | 
|---|
 | 249 | 
 | 
|---|
| [ac2b598] | 250 |         $coroutine * proc_cor = get_coroutine(this->runner);
 | 
|---|
| [8fcbb4c] | 251 | 
 | 
|---|
| [9f575ea] | 252 |         // set state of processor coroutine to inactive
 | 
|---|
 | 253 |         verify(proc_cor->state == Active);
 | 
|---|
| [ae7be7a] | 254 |         proc_cor->state = Blocked;
 | 
|---|
| [e8e457e] | 255 | 
 | 
|---|
| [9f575ea] | 256 |         // Actually run the thread
 | 
|---|
| [3381ed7] | 257 |         RUNNING:  while(true) {
 | 
|---|
| [ff79d5e] | 258 |                 thrd_dst->preempted = __NO_PREEMPTION;
 | 
|---|
 | 259 |                 thrd_dst->state = Active;
 | 
|---|
| [e8e457e] | 260 | 
 | 
|---|
| [58d64a4] | 261 |                 // Update global state
 | 
|---|
| [8fc652e0] | 262 |                 kernelTLS().this_thread = thrd_dst;
 | 
|---|
| [75f3522] | 263 | 
 | 
|---|
| [8fc652e0] | 264 |                 /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 265 |                 /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
 | 
|---|
| [9d6e1b8a] | 266 |                 /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );
 | 
|---|
| [b4b63e8] | 267 |                 /* paranoid */ verify( thrd_dst->context.SP );
 | 
|---|
| [5afb49a] | 268 |                 /* paranoid */ verify( thrd_dst->state != Halted );
 | 
|---|
| [210b8b3] | 269 |                 /* 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
 | 
|---|
 | 270 |                 /* 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
 | 
|---|
| [ac12f1f] | 271 |                 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
 | 
|---|
| [b4b63e8] | 272 | 
 | 
|---|
| [3381ed7] | 273 | 
 | 
|---|
| [58d64a4] | 274 | 
 | 
|---|
| [9f575ea] | 275 |                 // set context switch to the thread that the processor is executing
 | 
|---|
| [c7a900a] | 276 |                 __cfactx_switch( &proc_cor->context, &thrd_dst->context );
 | 
|---|
 | 277 |                 // when __cfactx_switch returns we are back in the processor coroutine
 | 
|---|
| [9f575ea] | 278 | 
 | 
|---|
| [ac12f1f] | 279 |                 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_dst->canary );
 | 
|---|
| [210b8b3] | 280 |                 /* 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 );
 | 
|---|
 | 281 |                 /* 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 );
 | 
|---|
| [b4b63e8] | 282 |                 /* paranoid */ verify( thrd_dst->context.SP );
 | 
|---|
| [9d6e1b8a] | 283 |                 /* paranoid */ verify( thrd_dst->curr_cluster == this->cltr );
 | 
|---|
| [8fc652e0] | 284 |                 /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
 | 
|---|
 | 285 |                 /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [75f3522] | 286 | 
 | 
|---|
| [58d64a4] | 287 |                 // Reset global state
 | 
|---|
| [8fc652e0] | 288 |                 kernelTLS().this_thread = 0p;
 | 
|---|
| [3381ed7] | 289 | 
 | 
|---|
 | 290 |                 // We just finished running a thread, there are a few things that could have happened.
 | 
|---|
 | 291 |                 // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
 | 
|---|
 | 292 |                 // 2 - Racy case    : the thread has blocked but someone has already tried to schedule it.
 | 
|---|
 | 293 |                 // 4 - Preempted
 | 
|---|
 | 294 |                 // In case 1, we may have won a race so we can't write to the state again.
 | 
|---|
 | 295 |                 // In case 2, we lost the race so we now own the thread.
 | 
|---|
 | 296 | 
 | 
|---|
 | 297 |                 if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
 | 
|---|
 | 298 |                         // The thread was preempted, reschedule it and reset the flag
 | 
|---|
| [e873838] | 299 |                         __schedule_thread( thrd_dst );
 | 
|---|
| [3381ed7] | 300 |                         break RUNNING;
 | 
|---|
 | 301 |                 }
 | 
|---|
| [75f3522] | 302 | 
 | 
|---|
| [3ea8ad1] | 303 |                 if(unlikely(thrd_dst->state == Halting)) {
 | 
|---|
| [ff79d5e] | 304 |                         // The thread has halted, it should never be scheduled/run again
 | 
|---|
| [5afb49a] | 305 |                         // finish the thread
 | 
|---|
 | 306 |                         __thread_finish( thrd_dst );
 | 
|---|
| [ff79d5e] | 307 |                         break RUNNING;
 | 
|---|
 | 308 |                 }
 | 
|---|
 | 309 | 
 | 
|---|
 | 310 |                 /* paranoid */ verify( thrd_dst->state == Active );
 | 
|---|
 | 311 |                 thrd_dst->state = Blocked;
 | 
|---|
 | 312 | 
 | 
|---|
| [3381ed7] | 313 |                 // set state of processor coroutine to active and the thread to inactive
 | 
|---|
| [ff79d5e] | 314 |                 int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST);
 | 
|---|
 | 315 |                 switch(old_ticket) {
 | 
|---|
| [6a77224] | 316 |                         case TICKET_RUNNING:
 | 
|---|
| [3381ed7] | 317 |                                 // This is case 1, the regular case, nothing more is needed
 | 
|---|
 | 318 |                                 break RUNNING;
 | 
|---|
| [6a77224] | 319 |                         case TICKET_UNBLOCK:
 | 
|---|
| [3381ed7] | 320 |                                 // This is case 2, the racy case, someone tried to run this thread before it finished blocking
 | 
|---|
 | 321 |                                 // In this case, just run it again.
 | 
|---|
 | 322 |                                 continue RUNNING;
 | 
|---|
 | 323 |                         default:
 | 
|---|
 | 324 |                                 // This makes no sense, something is wrong abort
 | 
|---|
| [ff79d5e] | 325 |                                 abort();
 | 
|---|
| [3381ed7] | 326 |                 }
 | 
|---|
| [9f575ea] | 327 |         }
 | 
|---|
| [e8e457e] | 328 | 
 | 
|---|
| [9f575ea] | 329 |         // Just before returning to the processor, set the processor coroutine to active
 | 
|---|
| [e8e457e] | 330 |         proc_cor->state = Active;
 | 
|---|
| [1eb239e4] | 331 | 
 | 
|---|
| [8fc652e0] | 332 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [82c948c] | 333 | }
 | 
|---|
 | 334 | 
 | 
|---|
| [14a61b5] | 335 | // KERNEL_ONLY
 | 
|---|
| [b0c7419] | 336 | void returnToKernel() {
 | 
|---|
| [8fc652e0] | 337 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 338 |         $coroutine * proc_cor = get_coroutine(kernelTLS().this_processor->runner);
 | 
|---|
 | 339 |         $thread * thrd_src = kernelTLS().this_thread;
 | 
|---|
| [e8e457e] | 340 | 
 | 
|---|
| [29cb302] | 341 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| [8fc652e0] | 342 |                 struct processor * last_proc = kernelTLS().this_processor;
 | 
|---|
| [29cb302] | 343 |         #endif
 | 
|---|
 | 344 | 
 | 
|---|
| [9f575ea] | 345 |         // Run the thread on this processor
 | 
|---|
 | 346 |         {
 | 
|---|
 | 347 |                 int local_errno = *__volatile_errno();
 | 
|---|
 | 348 |                 #if defined( __i386 ) || defined( __x86_64 )
 | 
|---|
 | 349 |                         __x87_store;
 | 
|---|
 | 350 |                 #endif
 | 
|---|
| [b4b63e8] | 351 |                 /* paranoid */ verify( proc_cor->context.SP );
 | 
|---|
| [ac12f1f] | 352 |                 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary );
 | 
|---|
| [c7a900a] | 353 |                 __cfactx_switch( &thrd_src->context, &proc_cor->context );
 | 
|---|
| [ac12f1f] | 354 |                 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd_src->canary );
 | 
|---|
| [9f575ea] | 355 |                 #if defined( __i386 ) || defined( __x86_64 )
 | 
|---|
 | 356 |                         __x87_load;
 | 
|---|
 | 357 |                 #endif
 | 
|---|
 | 358 |                 *__volatile_errno() = local_errno;
 | 
|---|
| [8fcbb4c] | 359 |         }
 | 
|---|
| [deca0f5] | 360 | 
 | 
|---|
| [29cb302] | 361 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| [8fc652e0] | 362 |                 if(last_proc != kernelTLS().this_processor) {
 | 
|---|
| [29cb302] | 363 |                         __tls_stats()->ready.threads.migration++;
 | 
|---|
 | 364 |                 }
 | 
|---|
 | 365 |         #endif
 | 
|---|
 | 366 | 
 | 
|---|
| [8fc652e0] | 367 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [210b8b3] | 368 |         /* 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 );
 | 
|---|
 | 369 |         /* 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] | 370 | }
 | 
|---|
 | 371 | 
 | 
|---|
| [8def349] | 372 | //-----------------------------------------------------------------------------
 | 
|---|
 | 373 | // Scheduler routines
 | 
|---|
| [14a61b5] | 374 | // KERNEL ONLY
 | 
|---|
| [e873838] | 375 | void __schedule_thread( $thread * thrd ) {
 | 
|---|
| [8fc652e0] | 376 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [9d6e1b8a] | 377 |         /* paranoid */ verify( kernelTLS().this_proc_id );
 | 
|---|
| [6a490b2] | 378 |         /* paranoid */ verify( thrd );
 | 
|---|
 | 379 |         /* paranoid */ verify( thrd->state != Halted );
 | 
|---|
| [9d6e1b8a] | 380 |         /* paranoid */ verify( thrd->curr_cluster );
 | 
|---|
| [3381ed7] | 381 |         /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
 | 
|---|
| [504a7dc] | 382 |         /* paranoid */  if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
 | 
|---|
 | 383 |                                         "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
 | 
|---|
| [ff79d5e] | 384 |         /* paranoid */  if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
 | 
|---|
| [504a7dc] | 385 |                                         "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
 | 
|---|
| [3381ed7] | 386 |         /* paranoid */ #endif
 | 
|---|
| [6a490b2] | 387 |         /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
 | 
|---|
| [ac12f1f] | 388 |         /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary );
 | 
|---|
| [b4b63e8] | 389 | 
 | 
|---|
| [9f575ea] | 390 | 
 | 
|---|
| [ae7be7a] | 391 |         if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
 | 
|---|
| [6b4cdd3] | 392 | 
 | 
|---|
| [e873838] | 393 |         ready_schedule_lock();
 | 
|---|
| [32a8b61] | 394 |                 // Dereference the thread now because once we push it, there is not guaranteed it's still valid.
 | 
|---|
 | 395 |                 struct cluster * cl = thrd->curr_cluster;
 | 
|---|
 | 396 | 
 | 
|---|
 | 397 |                 // push the thread to the cluster ready-queue
 | 
|---|
 | 398 |                 push( cl, thrd );
 | 
|---|
 | 399 | 
 | 
|---|
 | 400 |                 // variable thrd is no longer safe to use
 | 
|---|
 | 401 | 
 | 
|---|
 | 402 |                 // wake the cluster using the save variable.
 | 
|---|
 | 403 |                 __wake_one( cl );
 | 
|---|
| [e873838] | 404 |         ready_schedule_unlock();
 | 
|---|
| [1c273d0] | 405 | 
 | 
|---|
| [8fc652e0] | 406 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [db6f06a] | 407 | }
 | 
|---|
 | 408 | 
 | 
|---|
| [14a61b5] | 409 | // KERNEL ONLY
 | 
|---|
| [1eb239e4] | 410 | static inline $thread * __next_thread(cluster * this) with( *this ) {
 | 
|---|
| [8fc652e0] | 411 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 412 |         /* paranoid */ verify( kernelTLS().this_proc_id );
 | 
|---|
| [7768b8d] | 413 | 
 | 
|---|
| [e873838] | 414 |         ready_schedule_lock();
 | 
|---|
| [1eb239e4] | 415 |                 $thread * thrd = pop( this );
 | 
|---|
| [e873838] | 416 |         ready_schedule_unlock();
 | 
|---|
| [7768b8d] | 417 | 
 | 
|---|
| [8fc652e0] | 418 |         /* paranoid */ verify( kernelTLS().this_proc_id );
 | 
|---|
 | 419 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 420 |         return thrd;
 | 
|---|
| [eb2e723] | 421 | }
 | 
|---|
 | 422 | 
 | 
|---|
| [64a7146] | 423 | // KERNEL ONLY
 | 
|---|
| [1eb239e4] | 424 | static inline $thread * __next_thread_slow(cluster * this) with( *this ) {
 | 
|---|
| [8fc652e0] | 425 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 426 |         /* paranoid */ verify( kernelTLS().this_proc_id );
 | 
|---|
| [64a7146] | 427 | 
 | 
|---|
| [e873838] | 428 |         ready_schedule_lock();
 | 
|---|
| [1eb239e4] | 429 |                 $thread * thrd = pop_slow( this );
 | 
|---|
| [e873838] | 430 |         ready_schedule_unlock();
 | 
|---|
| [64a7146] | 431 | 
 | 
|---|
| [8fc652e0] | 432 |         /* paranoid */ verify( kernelTLS().this_proc_id );
 | 
|---|
 | 433 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 434 |         return thrd;
 | 
|---|
| [64a7146] | 435 | }
 | 
|---|
 | 436 | 
 | 
|---|
| [e873838] | 437 | void unpark( $thread * thrd ) {
 | 
|---|
 | 438 |         if( !thrd ) return;
 | 
|---|
 | 439 | 
 | 
|---|
| [ff79d5e] | 440 |         int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
 | 
|---|
 | 441 |         switch(old_ticket) {
 | 
|---|
| [6a77224] | 442 |                 case TICKET_RUNNING:
 | 
|---|
| [3381ed7] | 443 |                         // Wake won the race, the thread will reschedule/rerun itself
 | 
|---|
 | 444 |                         break;
 | 
|---|
| [6a77224] | 445 |                 case TICKET_BLOCKED:
 | 
|---|
| [3381ed7] | 446 |                         /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
 | 
|---|
| [ff79d5e] | 447 |                         /* paranoid */ verify( thrd->state == Blocked );
 | 
|---|
| [0b33412] | 448 | 
 | 
|---|
| [8fc652e0] | 449 |                         {
 | 
|---|
 | 450 |                                 /* paranoid */ verify( publicTLS_get(this_proc_id) );
 | 
|---|
 | 451 |                                 bool full = publicTLS_get(this_proc_id)->full_proc;
 | 
|---|
 | 452 |                                 if(full) disable_interrupts();
 | 
|---|
 | 453 | 
 | 
|---|
 | 454 |                                 /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 455 | 
 | 
|---|
 | 456 |                                 // Wake lost the race,
 | 
|---|
 | 457 |                                 __schedule_thread( thrd );
 | 
|---|
 | 458 | 
 | 
|---|
 | 459 |                                 /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 460 | 
 | 
|---|
 | 461 |                                 if(full) enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
 | 462 |                                 /* paranoid */ verify( publicTLS_get(this_proc_id) );
 | 
|---|
 | 463 |                         }
 | 
|---|
 | 464 | 
 | 
|---|
| [3381ed7] | 465 |                         break;
 | 
|---|
 | 466 |                 default:
 | 
|---|
 | 467 |                         // This makes no sense, something is wrong abort
 | 
|---|
| [7ee8153] | 468 |                         abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name);
 | 
|---|
| [de6319f] | 469 |         }
 | 
|---|
| [eb2e723] | 470 | }
 | 
|---|
 | 471 | 
 | 
|---|
| [e235429] | 472 | void park( void ) {
 | 
|---|
| [8fc652e0] | 473 |         /* paranoid */ verify( __preemption_enabled() );
 | 
|---|
| [82ff5845] | 474 |         disable_interrupts();
 | 
|---|
| [8fc652e0] | 475 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 476 |         /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION );
 | 
|---|
| [0b33412] | 477 | 
 | 
|---|
| [82c948c] | 478 |         returnToKernel();
 | 
|---|
| [0b33412] | 479 | 
 | 
|---|
| [8fc652e0] | 480 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [36982fc] | 481 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| [8fc652e0] | 482 |         /* paranoid */ verify( __preemption_enabled() );
 | 
|---|
| [0c78741] | 483 | 
 | 
|---|
 | 484 | }
 | 
|---|
| [09800e9] | 485 | 
 | 
|---|
| [5afb49a] | 486 | extern "C" {
 | 
|---|
 | 487 |         // Leave the thread monitor
 | 
|---|
 | 488 |         // last routine called by a thread.
 | 
|---|
 | 489 |         // Should never return
 | 
|---|
 | 490 |         void __cfactx_thrd_leave() {
 | 
|---|
| [8fc652e0] | 491 |                 $thread * thrd = active_thread();
 | 
|---|
| [5afb49a] | 492 |                 $monitor * this = &thrd->self_mon;
 | 
|---|
 | 493 | 
 | 
|---|
 | 494 |                 // Lock the monitor now
 | 
|---|
 | 495 |                 lock( this->lock __cfaabi_dbg_ctx2 );
 | 
|---|
 | 496 | 
 | 
|---|
 | 497 |                 disable_interrupts();
 | 
|---|
 | 498 | 
 | 
|---|
| [9d6e1b8a] | 499 |                 /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
 | 500 |                 /* paranoid */ verify( thrd->state == Active );
 | 
|---|
 | 501 |                 /* paranoid */ verify( 0x0D15EA5E0D15EA5Ep == thrd->canary );
 | 
|---|
 | 502 |                 /* paranoid */ verify( kernelTLS().this_thread == thrd );
 | 
|---|
 | 503 |                 /* paranoid */ verify( thrd->context.SP );
 | 
|---|
 | 504 |                 /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) > ((uintptr_t)__get_stack(thrd->curr_cor)->limit), "ERROR : $thread %p has been corrupted.\n StackPointer too large.\n", thrd );
 | 
|---|
 | 505 |                 /* paranoid */ verifyf( ((uintptr_t)thrd->context.SP) < ((uintptr_t)__get_stack(thrd->curr_cor)->base ), "ERROR : $thread %p has been corrupted.\n StackPointer too small.\n", thrd );
 | 
|---|
 | 506 | 
 | 
|---|
| [3ea8ad1] | 507 |                 thrd->state = Halting;
 | 
|---|
| [58688bf] | 508 |                 if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); }
 | 
|---|
| [9d6e1b8a] | 509 |                 if( thrd != this->owner ) { abort( "Thread internal monitor has incorrect owner" ); }
 | 
|---|
 | 510 |                 if( this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); }
 | 
|---|
| [5afb49a] | 511 | 
 | 
|---|
 | 512 |                 // Leave the thread
 | 
|---|
 | 513 |                 returnToKernel();
 | 
|---|
 | 514 | 
 | 
|---|
 | 515 |                 // Control flow should never reach here!
 | 
|---|
| [9d6e1b8a] | 516 |                 abort();
 | 
|---|
| [5afb49a] | 517 |         }
 | 
|---|
| [09800e9] | 518 | }
 | 
|---|
 | 519 | 
 | 
|---|
| [14a61b5] | 520 | // KERNEL ONLY
 | 
|---|
| [3381ed7] | 521 | bool force_yield( __Preemption_Reason reason ) {
 | 
|---|
| [8fc652e0] | 522 |         /* paranoid */ verify( __preemption_enabled() );
 | 
|---|
| [09800e9] | 523 |         disable_interrupts();
 | 
|---|
| [8fc652e0] | 524 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [09800e9] | 525 | 
 | 
|---|
| [8fc652e0] | 526 |         $thread * thrd = kernelTLS().this_thread;
 | 
|---|
| [ff79d5e] | 527 |         /* paranoid */ verify(thrd->state == Active);
 | 
|---|
| [3381ed7] | 528 | 
 | 
|---|
 | 529 |         // SKULLDUGGERY: It is possible that we are preempting this thread just before
 | 
|---|
 | 530 |         // it was going to park itself. If that is the case and it is already using the
 | 
|---|
 | 531 |         // intrusive fields then we can't use them to preempt the thread
 | 
|---|
 | 532 |         // If that is the case, abandon the preemption.
 | 
|---|
 | 533 |         bool preempted = false;
 | 
|---|
| [504a7dc] | 534 |         if(thrd->link.next == 0p) {
 | 
|---|
| [3381ed7] | 535 |                 preempted = true;
 | 
|---|
 | 536 |                 thrd->preempted = reason;
 | 
|---|
 | 537 |                 returnToKernel();
 | 
|---|
| [de6319f] | 538 |         }
 | 
|---|
| [f2b12406] | 539 | 
 | 
|---|
| [8fc652e0] | 540 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [3381ed7] | 541 |         enable_interrupts_noPoll();
 | 
|---|
| [8fc652e0] | 542 |         /* paranoid */ verify( __preemption_enabled() );
 | 
|---|
| [f2b12406] | 543 | 
 | 
|---|
| [3381ed7] | 544 |         return preempted;
 | 
|---|
| [f2b12406] | 545 | }
 | 
|---|
 | 546 | 
 | 
|---|
| [14a61b5] | 547 | //=============================================================================================
 | 
|---|
| [92e7631] | 548 | // Kernel Idle Sleep
 | 
|---|
| [14a61b5] | 549 | //=============================================================================================
 | 
|---|
| [64a7146] | 550 | // Wake a thread from the front if there are any
 | 
|---|
| [e873838] | 551 | static void __wake_one(cluster * this) {
 | 
|---|
| [8fc652e0] | 552 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [e873838] | 553 |         /* paranoid */ verify( ready_schedule_islocked() );
 | 
|---|
| [14a61b5] | 554 | 
 | 
|---|
| [64a7146] | 555 |         // Check if there is a sleeping processor
 | 
|---|
| [1eb239e4] | 556 |         processor * p;
 | 
|---|
 | 557 |         unsigned idle;
 | 
|---|
 | 558 |         unsigned total;
 | 
|---|
 | 559 |         [idle, total, p] = query(this->idles);
 | 
|---|
| [14a61b5] | 560 | 
 | 
|---|
| [64a7146] | 561 |         // If no one is sleeping, we are done
 | 
|---|
| [1eb239e4] | 562 |         if( idle == 0 ) return;
 | 
|---|
| [14a61b5] | 563 | 
 | 
|---|
| [64a7146] | 564 |         // We found a processor, wake it up
 | 
|---|
 | 565 |         post( p->idle );
 | 
|---|
| [14a61b5] | 566 | 
 | 
|---|
| [1eb239e4] | 567 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 568 |                 __tls_stats()->ready.sleep.wakes++;
 | 
|---|
 | 569 |         #endif
 | 
|---|
 | 570 | 
 | 
|---|
| [e873838] | 571 |         /* paranoid */ verify( ready_schedule_islocked() );
 | 
|---|
| [8fc652e0] | 572 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 573 | 
 | 
|---|
 | 574 |         return;
 | 
|---|
| [64a7146] | 575 | }
 | 
|---|
| [14a61b5] | 576 | 
 | 
|---|
| [64a7146] | 577 | // Unconditionnaly wake a thread
 | 
|---|
| [1eb239e4] | 578 | void __wake_proc(processor * this) {
 | 
|---|
| [64a7146] | 579 |         __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
 | 
|---|
| [14a61b5] | 580 | 
 | 
|---|
| [64a7146] | 581 |         disable_interrupts();
 | 
|---|
| [8fc652e0] | 582 |                 /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [58d64a4] | 583 |                 post( this->idle );
 | 
|---|
| [64a7146] | 584 |         enable_interrupts( __cfaabi_dbg_ctx );
 | 
|---|
| [92e7631] | 585 | }
 | 
|---|
 | 586 | 
 | 
|---|
| [1eb239e4] | 587 | static void push  (__cluster_idles & this, processor & proc) {
 | 
|---|
| [8fc652e0] | 588 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 589 |         lock( this );
 | 
|---|
 | 590 |                 this.idle++;
 | 
|---|
 | 591 |                 /* paranoid */ verify( this.idle <= this.total );
 | 
|---|
| [8e16177] | 592 | 
 | 
|---|
| [1eb239e4] | 593 |                 insert_first(this.list, proc);
 | 
|---|
 | 594 |         unlock( this );
 | 
|---|
| [8fc652e0] | 595 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 596 | }
 | 
|---|
| [8e16177] | 597 | 
 | 
|---|
| [1eb239e4] | 598 | static void remove(__cluster_idles & this, processor & proc) {
 | 
|---|
| [8fc652e0] | 599 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 600 |         lock( this );
 | 
|---|
 | 601 |                 this.idle--;
 | 
|---|
 | 602 |                 /* paranoid */ verify( this.idle >= 0 );
 | 
|---|
| [c34ebf2] | 603 | 
 | 
|---|
| [1eb239e4] | 604 |                 remove(proc);
 | 
|---|
 | 605 |         unlock( this );
 | 
|---|
| [8fc652e0] | 606 |         /* paranoid */ verify( ! __preemption_enabled() );
 | 
|---|
| [1eb239e4] | 607 | }
 | 
|---|
| [c34ebf2] | 608 | 
 | 
|---|
| [1eb239e4] | 609 | static [unsigned idle, unsigned total, * processor] query( & __cluster_idles this ) {
 | 
|---|
 | 610 |         for() {
 | 
|---|
 | 611 |                 uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST);
 | 
|---|
 | 612 |                 if( 1 == (l % 2) ) { Pause(); continue; }
 | 
|---|
 | 613 |                 unsigned idle    = this.idle;
 | 
|---|
 | 614 |                 unsigned total   = this.total;
 | 
|---|
 | 615 |                 processor * proc = &this.list`first;
 | 
|---|
| [7fdae38] | 616 |                 // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it
 | 
|---|
 | 617 |                 asm volatile("": : :"memory");
 | 
|---|
| [1eb239e4] | 618 |                 if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; }
 | 
|---|
 | 619 |                 return [idle, total, proc];
 | 
|---|
 | 620 |         }
 | 
|---|
| [6b4cdd3] | 621 | }
 | 
|---|
 | 622 | 
 | 
|---|
| [dbe9b08] | 623 | //=============================================================================================
 | 
|---|
 | 624 | // Unexpected Terminating logic
 | 
|---|
 | 625 | //=============================================================================================
 | 
|---|
| [92bfda0] | 626 | void __kernel_abort_msg( char * abort_text, int abort_text_size ) {
 | 
|---|
 | 627 |         $thread * thrd = __cfaabi_tls.this_thread;
 | 
|---|
| [9d944b2] | 628 | 
 | 
|---|
| [de94a60] | 629 |         if(thrd) {
 | 
|---|
 | 630 |                 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
 | 
|---|
| [1c40091] | 631 |                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| [de94a60] | 632 | 
 | 
|---|
| [212c2187] | 633 |                 if ( &thrd->self_cor != thrd->curr_cor ) {
 | 
|---|
 | 634 |                         len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
 | 
|---|
| [1c40091] | 635 |                         __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| [de94a60] | 636 |                 }
 | 
|---|
 | 637 |                 else {
 | 
|---|
| [1c40091] | 638 |                         __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
 | 
|---|
| [de94a60] | 639 |                 }
 | 
|---|
| [1c273d0] | 640 |         }
 | 
|---|
| [9d944b2] | 641 |         else {
 | 
|---|
| [de94a60] | 642 |                 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
 | 
|---|
| [1c40091] | 643 |                 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
 | 
|---|
| [9d944b2] | 644 |         }
 | 
|---|
 | 645 | }
 | 
|---|
 | 646 | 
 | 
|---|
| [92bfda0] | 647 | int __kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
 | 
|---|
 | 648 |         return get_coroutine(__cfaabi_tls.this_thread) == get_coroutine(mainThread) ? 4 : 2;
 | 
|---|
| [2b8bc41] | 649 | }
 | 
|---|
 | 650 | 
 | 
|---|
| [de94a60] | 651 | static __spinlock_t kernel_debug_lock;
 | 
|---|
 | 652 | 
 | 
|---|
| [9d944b2] | 653 | extern "C" {
 | 
|---|
| [1c40091] | 654 |         void __cfaabi_bits_acquire() {
 | 
|---|
| [36982fc] | 655 |                 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
 | 
|---|
| [9d944b2] | 656 |         }
 | 
|---|
 | 657 | 
 | 
|---|
| [1c40091] | 658 |         void __cfaabi_bits_release() {
 | 
|---|
| [ea7d2b0] | 659 |                 unlock( kernel_debug_lock );
 | 
|---|
| [9d944b2] | 660 |         }
 | 
|---|
| [8118303] | 661 | }
 | 
|---|
 | 662 | 
 | 
|---|
| [fa21ac9] | 663 | //=============================================================================================
 | 
|---|
 | 664 | // Kernel Utilities
 | 
|---|
 | 665 | //=============================================================================================
 | 
|---|
| [de94a60] | 666 | //-----------------------------------------------------------------------------
 | 
|---|
 | 667 | // Debug
 | 
|---|
 | 668 | __cfaabi_dbg_debug_do(
 | 
|---|
| [1997b4e] | 669 |         extern "C" {
 | 
|---|
| [ae66348] | 670 |                 void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
 | 
|---|
| [1997b4e] | 671 |                         this.prev_name = prev_name;
 | 
|---|
| [8fc652e0] | 672 |                         this.prev_thrd = kernelTLS().this_thread;
 | 
|---|
| [1997b4e] | 673 |                 }
 | 
|---|
| [9181f1d] | 674 |         }
 | 
|---|
| [f7d6bb0] | 675 | )
 | 
|---|
| [2026bb6] | 676 | 
 | 
|---|
 | 677 | //-----------------------------------------------------------------------------
 | 
|---|
 | 678 | // Debug
 | 
|---|
| [8c50aed] | 679 | bool threading_enabled(void) __attribute__((const)) {
 | 
|---|
| [2026bb6] | 680 |         return true;
 | 
|---|
 | 681 | }
 | 
|---|
| [c34ebf2] | 682 | 
 | 
|---|
 | 683 | //-----------------------------------------------------------------------------
 | 
|---|
 | 684 | // Statistics
 | 
|---|
 | 685 | #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 686 |         void print_halts( processor & this ) {
 | 
|---|
 | 687 |                 this.print_halts = true;
 | 
|---|
 | 688 |         }
 | 
|---|
| [58688bf] | 689 | 
 | 
|---|
 | 690 |         void print_stats_now( cluster & this, int flags ) {
 | 
|---|
| [1b033b8] | 691 |                 __print_stats( this.stats, this.print_stats, "Cluster", this.name, (void*)&this );
 | 
|---|
 | 692 |         }
 | 
|---|
 | 693 | 
 | 
|---|
 | 694 |         extern int __print_alarm_stats;
 | 
|---|
 | 695 |         void print_alarm_stats() {
 | 
|---|
 | 696 |                 __print_alarm_stats = -1;
 | 
|---|
| [58688bf] | 697 |         }
 | 
|---|
| [c34ebf2] | 698 | #endif
 | 
|---|
| [8118303] | 699 | // Local Variables: //
 | 
|---|
 | 700 | // mode: c //
 | 
|---|
 | 701 | // tab-width: 4 //
 | 
|---|
 | 702 | // End: //
 | 
|---|