source: libcfa/src/concurrency/kernel.cfa@ 726b748

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

Change usage of TLS to more strongly segregate in kernel and out of kernel usage.

  • Property mode set to 100644
File size: 22.8 KB
RevLine 
[8118303]1//
2// Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// kernel.c --
8//
9// Author : Thierry Delisle
[75f3522]10// Created On : Tue Jan 17 12:27:26 2017
[6b0b624]11// Last Modified By : Peter A. Buhr
[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]102extern $thread * mainThread;
103extern processor * mainProcessor;
[2ac095d]104
[92e7631]105//-----------------------------------------------------------------------------
106// Kernel Scheduling logic
107static $thread * __next_thread(cluster * this);
[1eb239e4]108static $thread * __next_thread_slow(cluster * this);
[92e7631]109static void __run_thread(processor * this, $thread * dst);
[e873838]110static void __wake_one(cluster * cltr);
[1eb239e4]111
112static void push (__cluster_idles & idles, processor & proc);
113static void remove(__cluster_idles & idles, processor & proc);
114static [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]121void 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
[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
[8fc652e0]219 __cfaabi_tls.this_thread = mainThread;
[6a490b2]220 }
[7768b8d]221
[4069faad]222 __cfadbg_print_safe(runtime_core, "Kernel : core %p terminated\n", this);
[c84e80a]223}
224
[5c1a531]225static int * __volatile_errno() __attribute__((noinline));
226static 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]231static void __run_thread(processor * this, $thread * thrd_dst) {
[8fc652e0]232 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]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
[9f575ea]239 // set state of processor coroutine to inactive
240 verify(proc_cor->state == Active);
[ae7be7a]241 proc_cor->state = Blocked;
[e8e457e]242
[9f575ea]243 // Actually run the thread
[3381ed7]244 RUNNING: while(true) {
[ff79d5e]245 thrd_dst->preempted = __NO_PREEMPTION;
246 thrd_dst->state = Active;
[e8e457e]247
[58d64a4]248 // Update global state
[8fc652e0]249 kernelTLS().this_thread = thrd_dst;
[75f3522]250
[8fc652e0]251 /* paranoid */ verify( ! __preemption_enabled() );
252 /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
[b4b63e8]253 /* paranoid */ verify( thrd_dst->context.SP );
[5afb49a]254 /* paranoid */ verify( thrd_dst->state != Halted );
[210b8b3]255 /* 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
256 /* 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
[b4b63e8]257 /* paranoid */ verify( 0x0D15EA5E0D15EA5E == thrd_dst->canary );
258
[3381ed7]259
[58d64a4]260
[9f575ea]261 // set context switch to the thread that the processor is executing
[c7a900a]262 __cfactx_switch( &proc_cor->context, &thrd_dst->context );
263 // when __cfactx_switch returns we are back in the processor coroutine
[9f575ea]264
[b4b63e8]265 /* paranoid */ verify( 0x0D15EA5E0D15EA5E == thrd_dst->canary );
[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 );
[b4b63e8]268 /* paranoid */ verify( thrd_dst->context.SP );
[8fc652e0]269 /* paranoid */ verify( kernelTLS().this_thread == thrd_dst );
270 /* paranoid */ verify( ! __preemption_enabled() );
[75f3522]271
[58d64a4]272 // Reset global state
[8fc652e0]273 kernelTLS().this_thread = 0p;
[3381ed7]274
275 // We just finished running a thread, there are a few things that could have happened.
276 // 1 - Regular case : the thread has blocked and now one has scheduled it yet.
277 // 2 - Racy case : the thread has blocked but someone has already tried to schedule it.
278 // 4 - Preempted
279 // In case 1, we may have won a race so we can't write to the state again.
280 // In case 2, we lost the race so we now own the thread.
281
282 if(unlikely(thrd_dst->preempted != __NO_PREEMPTION)) {
283 // The thread was preempted, reschedule it and reset the flag
[e873838]284 __schedule_thread( thrd_dst );
[3381ed7]285 break RUNNING;
286 }
[75f3522]287
[ff79d5e]288 if(unlikely(thrd_dst->state == Halted)) {
289 // The thread has halted, it should never be scheduled/run again
[5afb49a]290 // finish the thread
291 __thread_finish( thrd_dst );
[ff79d5e]292 break RUNNING;
293 }
294
295 /* paranoid */ verify( thrd_dst->state == Active );
296 thrd_dst->state = Blocked;
297
[3381ed7]298 // set state of processor coroutine to active and the thread to inactive
[ff79d5e]299 int old_ticket = __atomic_fetch_sub(&thrd_dst->ticket, 1, __ATOMIC_SEQ_CST);
300 switch(old_ticket) {
[6a77224]301 case TICKET_RUNNING:
[3381ed7]302 // This is case 1, the regular case, nothing more is needed
303 break RUNNING;
[6a77224]304 case TICKET_UNBLOCK:
[3381ed7]305 // This is case 2, the racy case, someone tried to run this thread before it finished blocking
306 // In this case, just run it again.
307 continue RUNNING;
308 default:
309 // This makes no sense, something is wrong abort
[ff79d5e]310 abort();
[3381ed7]311 }
[9f575ea]312 }
[e8e457e]313
[9f575ea]314 // Just before returning to the processor, set the processor coroutine to active
[e8e457e]315 proc_cor->state = Active;
[1eb239e4]316
[8fc652e0]317 /* paranoid */ verify( ! __preemption_enabled() );
[82c948c]318}
319
[14a61b5]320// KERNEL_ONLY
[b0c7419]321void returnToKernel() {
[8fc652e0]322 /* paranoid */ verify( ! __preemption_enabled() );
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__)
[8fc652e0]327 struct processor * last_proc = kernelTLS().this_processor;
[29cb302]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
[b4b63e8]336 /* paranoid */ verify( proc_cor->context.SP );
337 /* paranoid */ verify( 0x0D15EA5E0D15EA5E == thrd_src->canary );
[c7a900a]338 __cfactx_switch( &thrd_src->context, &proc_cor->context );
[b4b63e8]339 /* paranoid */ verify( 0x0D15EA5E0D15EA5E == thrd_src->canary );
[9f575ea]340 #if defined( __i386 ) || defined( __x86_64 )
341 __x87_load;
342 #endif
343 *__volatile_errno() = local_errno;
[8fcbb4c]344 }
[deca0f5]345
[29cb302]346 #if !defined(__CFA_NO_STATISTICS__)
[8fc652e0]347 if(last_proc != kernelTLS().this_processor) {
[29cb302]348 __tls_stats()->ready.threads.migration++;
349 }
350 #endif
351
[8fc652e0]352 /* paranoid */ verify( ! __preemption_enabled() );
[210b8b3]353 /* 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 );
354 /* 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]355}
356
[8def349]357//-----------------------------------------------------------------------------
358// Scheduler routines
[14a61b5]359// KERNEL ONLY
[e873838]360void __schedule_thread( $thread * thrd ) {
[8fc652e0]361 /* paranoid */ verify( ! __preemption_enabled() );
[6a490b2]362 /* paranoid */ verify( thrd );
363 /* paranoid */ verify( thrd->state != Halted );
[8fc652e0]364 /* paranoid */ verify( kernelTLS().this_proc_id );
[3381ed7]365 /* paranoid */ #if defined( __CFA_WITH_VERIFY__ )
[504a7dc]366 /* paranoid */ if( thrd->state == Blocked || thrd->state == Start ) assertf( thrd->preempted == __NO_PREEMPTION,
367 "Error inactive thread marked as preempted, state %d, preemption %d\n", thrd->state, thrd->preempted );
[ff79d5e]368 /* paranoid */ if( thrd->preempted != __NO_PREEMPTION ) assertf(thrd->state == Active,
[504a7dc]369 "Error preempted thread marked as not currently running, state %d, preemption %d\n", thrd->state, thrd->preempted );
[3381ed7]370 /* paranoid */ #endif
[6a490b2]371 /* paranoid */ verifyf( thrd->link.next == 0p, "Expected null got %p", thrd->link.next );
[b4b63e8]372 /* paranoid */ verify( 0x0D15EA5E0D15EA5E == thrd->canary );
373
[9f575ea]374
[ae7be7a]375 if (thrd->preempted == __NO_PREEMPTION) thrd->state = Ready;
[6b4cdd3]376
[e873838]377 ready_schedule_lock();
[504a7dc]378 push( thrd->curr_cluster, thrd );
[e873838]379 __wake_one(thrd->curr_cluster);
380 ready_schedule_unlock();
[1c273d0]381
[8fc652e0]382 /* paranoid */ verify( ! __preemption_enabled() );
[db6f06a]383}
384
[14a61b5]385// KERNEL ONLY
[1eb239e4]386static inline $thread * __next_thread(cluster * this) with( *this ) {
[8fc652e0]387 /* paranoid */ verify( ! __preemption_enabled() );
388 /* paranoid */ verify( kernelTLS().this_proc_id );
[7768b8d]389
[e873838]390 ready_schedule_lock();
[1eb239e4]391 $thread * thrd = pop( this );
[e873838]392 ready_schedule_unlock();
[7768b8d]393
[8fc652e0]394 /* paranoid */ verify( kernelTLS().this_proc_id );
395 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]396 return thrd;
[eb2e723]397}
398
[64a7146]399// KERNEL ONLY
[1eb239e4]400static inline $thread * __next_thread_slow(cluster * this) with( *this ) {
[8fc652e0]401 /* paranoid */ verify( ! __preemption_enabled() );
402 /* paranoid */ verify( kernelTLS().this_proc_id );
[64a7146]403
[e873838]404 ready_schedule_lock();
[1eb239e4]405 $thread * thrd = pop_slow( this );
[e873838]406 ready_schedule_unlock();
[64a7146]407
[8fc652e0]408 /* paranoid */ verify( kernelTLS().this_proc_id );
409 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]410 return thrd;
[64a7146]411}
412
[e873838]413void unpark( $thread * thrd ) {
414 if( !thrd ) return;
415
[ff79d5e]416 int old_ticket = __atomic_fetch_add(&thrd->ticket, 1, __ATOMIC_SEQ_CST);
417 switch(old_ticket) {
[6a77224]418 case TICKET_RUNNING:
[3381ed7]419 // Wake won the race, the thread will reschedule/rerun itself
420 break;
[6a77224]421 case TICKET_BLOCKED:
[3381ed7]422 /* paranoid */ verify( ! thrd->preempted != __NO_PREEMPTION );
[ff79d5e]423 /* paranoid */ verify( thrd->state == Blocked );
[0b33412]424
[8fc652e0]425 {
426 /* paranoid */ verify( publicTLS_get(this_proc_id) );
427 bool full = publicTLS_get(this_proc_id)->full_proc;
428 if(full) disable_interrupts();
429
430 /* paranoid */ verify( ! __preemption_enabled() );
431
432 // Wake lost the race,
433 __schedule_thread( thrd );
434
435 /* paranoid */ verify( ! __preemption_enabled() );
436
437 if(full) enable_interrupts( __cfaabi_dbg_ctx );
438 /* paranoid */ verify( publicTLS_get(this_proc_id) );
439 }
440
[3381ed7]441 break;
442 default:
443 // This makes no sense, something is wrong abort
[7ee8153]444 abort("Thread %p (%s) has mismatch park/unpark\n", thrd, thrd->self_cor.name);
[de6319f]445 }
[eb2e723]446}
447
[e235429]448void park( void ) {
[8fc652e0]449 /* paranoid */ verify( __preemption_enabled() );
[82ff5845]450 disable_interrupts();
[8fc652e0]451 /* paranoid */ verify( ! __preemption_enabled() );
452 /* paranoid */ verify( kernelTLS().this_thread->preempted == __NO_PREEMPTION );
[0b33412]453
[82c948c]454 returnToKernel();
[0b33412]455
[8fc652e0]456 /* paranoid */ verify( ! __preemption_enabled() );
[36982fc]457 enable_interrupts( __cfaabi_dbg_ctx );
[8fc652e0]458 /* paranoid */ verify( __preemption_enabled() );
[0c78741]459
460}
[09800e9]461
[5afb49a]462extern "C" {
463 // Leave the thread monitor
464 // last routine called by a thread.
465 // Should never return
466 void __cfactx_thrd_leave() {
[8fc652e0]467 $thread * thrd = active_thread();
[5afb49a]468 $monitor * this = &thrd->self_mon;
469
470 // Lock the monitor now
471 lock( this->lock __cfaabi_dbg_ctx2 );
472
473 disable_interrupts();
474
475 thrd->state = Halted;
[58688bf]476 if( TICKET_RUNNING != thrd->ticket ) { abort( "Thread terminated with pending unpark" ); }
[5afb49a]477 if( thrd != this->owner || this->recursion != 1) { abort( "Thread internal monitor has unbalanced recursion" ); }
478
479 // Leave the thread
[8fc652e0]480 /* paranoid */ verify( ! __preemption_enabled() );
[5afb49a]481 returnToKernel();
482 abort();
483
484 // Control flow should never reach here!
485 }
[09800e9]486}
487
[14a61b5]488// KERNEL ONLY
[3381ed7]489bool force_yield( __Preemption_Reason reason ) {
[8fc652e0]490 /* paranoid */ verify( __preemption_enabled() );
[09800e9]491 disable_interrupts();
[8fc652e0]492 /* paranoid */ verify( ! __preemption_enabled() );
[09800e9]493
[8fc652e0]494 $thread * thrd = kernelTLS().this_thread;
[ff79d5e]495 /* paranoid */ verify(thrd->state == Active);
[3381ed7]496
497 // SKULLDUGGERY: It is possible that we are preempting this thread just before
498 // it was going to park itself. If that is the case and it is already using the
499 // intrusive fields then we can't use them to preempt the thread
500 // If that is the case, abandon the preemption.
501 bool preempted = false;
[504a7dc]502 if(thrd->link.next == 0p) {
[3381ed7]503 preempted = true;
504 thrd->preempted = reason;
505 returnToKernel();
[de6319f]506 }
[f2b12406]507
[8fc652e0]508 /* paranoid */ verify( ! __preemption_enabled() );
[3381ed7]509 enable_interrupts_noPoll();
[8fc652e0]510 /* paranoid */ verify( __preemption_enabled() );
[f2b12406]511
[3381ed7]512 return preempted;
[f2b12406]513}
514
[14a61b5]515//=============================================================================================
[92e7631]516// Kernel Idle Sleep
[14a61b5]517//=============================================================================================
[64a7146]518// Wake a thread from the front if there are any
[e873838]519static void __wake_one(cluster * this) {
[8fc652e0]520 /* paranoid */ verify( ! __preemption_enabled() );
[e873838]521 /* paranoid */ verify( ready_schedule_islocked() );
[14a61b5]522
[64a7146]523 // Check if there is a sleeping processor
[1eb239e4]524 processor * p;
525 unsigned idle;
526 unsigned total;
527 [idle, total, p] = query(this->idles);
[14a61b5]528
[64a7146]529 // If no one is sleeping, we are done
[1eb239e4]530 if( idle == 0 ) return;
[14a61b5]531
[64a7146]532 // We found a processor, wake it up
533 post( p->idle );
[14a61b5]534
[1eb239e4]535 #if !defined(__CFA_NO_STATISTICS__)
536 __tls_stats()->ready.sleep.wakes++;
537 #endif
538
[e873838]539 /* paranoid */ verify( ready_schedule_islocked() );
[8fc652e0]540 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]541
542 return;
[64a7146]543}
[14a61b5]544
[64a7146]545// Unconditionnaly wake a thread
[1eb239e4]546void __wake_proc(processor * this) {
[64a7146]547 __cfadbg_print_safe(runtime_core, "Kernel : waking Processor %p\n", this);
[14a61b5]548
[64a7146]549 disable_interrupts();
[8fc652e0]550 /* paranoid */ verify( ! __preemption_enabled() );
[58d64a4]551 post( this->idle );
[64a7146]552 enable_interrupts( __cfaabi_dbg_ctx );
[92e7631]553}
554
[1eb239e4]555static void push (__cluster_idles & this, processor & proc) {
[8fc652e0]556 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]557 lock( this );
558 this.idle++;
559 /* paranoid */ verify( this.idle <= this.total );
[8e16177]560
[1eb239e4]561 insert_first(this.list, proc);
562 unlock( this );
[8fc652e0]563 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]564}
[8e16177]565
[1eb239e4]566static void remove(__cluster_idles & this, processor & proc) {
[8fc652e0]567 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]568 lock( this );
569 this.idle--;
570 /* paranoid */ verify( this.idle >= 0 );
[c34ebf2]571
[1eb239e4]572 remove(proc);
573 unlock( this );
[8fc652e0]574 /* paranoid */ verify( ! __preemption_enabled() );
[1eb239e4]575}
[c34ebf2]576
[1eb239e4]577static [unsigned idle, unsigned total, * processor] query( & __cluster_idles this ) {
578 for() {
579 uint64_t l = __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST);
580 if( 1 == (l % 2) ) { Pause(); continue; }
581 unsigned idle = this.idle;
582 unsigned total = this.total;
583 processor * proc = &this.list`first;
[7fdae38]584 // Compiler fence is unnecessary, but gcc-8 and older incorrectly reorder code without it
585 asm volatile("": : :"memory");
[1eb239e4]586 if(l != __atomic_load_n(&this.lock, __ATOMIC_SEQ_CST)) { Pause(); continue; }
587 return [idle, total, proc];
588 }
[6b4cdd3]589}
590
[dbe9b08]591//=============================================================================================
592// Unexpected Terminating logic
593//=============================================================================================
[ea7d2b0]594static __spinlock_t kernel_abort_lock;
[9d944b2]595static bool kernel_abort_called = false;
596
[afd550c]597void * kernel_abort(void) __attribute__ ((__nothrow__)) {
[9d944b2]598 // abort cannot be recursively entered by the same or different processors because all signal handlers return when
599 // the globalAbort flag is true.
[36982fc]600 lock( kernel_abort_lock __cfaabi_dbg_ctx2 );
[9d944b2]601
602 // first task to abort ?
[de94a60]603 if ( kernel_abort_called ) { // not first task to abort ?
[ea7d2b0]604 unlock( kernel_abort_lock );
[1c273d0]605
[9d944b2]606 sigset_t mask;
607 sigemptyset( &mask );
[de94a60]608 sigaddset( &mask, SIGALRM ); // block SIGALRM signals
[8a13c47]609 sigaddset( &mask, SIGUSR1 ); // block SIGALRM signals
610 sigsuspend( &mask ); // block the processor to prevent further damage during abort
611 _exit( EXIT_FAILURE ); // if processor unblocks before it is killed, terminate it
[de94a60]612 }
613 else {
614 kernel_abort_called = true;
615 unlock( kernel_abort_lock );
[9d944b2]616 }
617
[8fc652e0]618 return __cfaabi_tls.this_thread;
[9d944b2]619}
620
621void kernel_abort_msg( void * kernel_data, char * abort_text, int abort_text_size ) {
[b81fd95]622 $thread * thrd = ( $thread * ) kernel_data;
[9d944b2]623
[de94a60]624 if(thrd) {
625 int len = snprintf( abort_text, abort_text_size, "Error occurred while executing thread %.256s (%p)", thrd->self_cor.name, thrd );
[1c40091]626 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
[de94a60]627
[212c2187]628 if ( &thrd->self_cor != thrd->curr_cor ) {
629 len = snprintf( abort_text, abort_text_size, " in coroutine %.256s (%p).\n", thrd->curr_cor->name, thrd->curr_cor );
[1c40091]630 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
[de94a60]631 }
632 else {
[1c40091]633 __cfaabi_bits_write( STDERR_FILENO, ".\n", 2 );
[de94a60]634 }
[1c273d0]635 }
[9d944b2]636 else {
[de94a60]637 int len = snprintf( abort_text, abort_text_size, "Error occurred outside of any thread.\n" );
[1c40091]638 __cfaabi_bits_write( STDERR_FILENO, abort_text, len );
[9d944b2]639 }
640}
641
[2b8bc41]642int kernel_abort_lastframe( void ) __attribute__ ((__nothrow__)) {
[8fc652e0]643 return get_coroutine(kernelTLS().this_thread) == get_coroutine(mainThread) ? 4 : 2;
[2b8bc41]644}
645
[de94a60]646static __spinlock_t kernel_debug_lock;
647
[9d944b2]648extern "C" {
[1c40091]649 void __cfaabi_bits_acquire() {
[36982fc]650 lock( kernel_debug_lock __cfaabi_dbg_ctx2 );
[9d944b2]651 }
652
[1c40091]653 void __cfaabi_bits_release() {
[ea7d2b0]654 unlock( kernel_debug_lock );
[9d944b2]655 }
[8118303]656}
657
[fa21ac9]658//=============================================================================================
659// Kernel Utilities
660//=============================================================================================
[bd98b58]661//-----------------------------------------------------------------------------
662// Locks
[242a902]663void ?{}( semaphore & this, int count = 1 ) {
664 (this.lock){};
665 this.count = count;
666 (this.waiting){};
[db6f06a]667}
[242a902]668void ^?{}(semaphore & this) {}
[db6f06a]669
[71c8b7e]670bool P(semaphore & this) with( this ){
[65deb18]671 lock( lock __cfaabi_dbg_ctx2 );
672 count -= 1;
673 if ( count < 0 ) {
[bdeba0b]674 // queue current task
[8fc652e0]675 append( waiting, active_thread() );
[bdeba0b]676
677 // atomically release spin lock and block
[3381ed7]678 unlock( lock );
[e235429]679 park();
[71c8b7e]680 return true;
[8def349]681 }
[4e6fb8e]682 else {
[65deb18]683 unlock( lock );
[71c8b7e]684 return false;
[4e6fb8e]685 }
[bd98b58]686}
687
[f0ce5f4]688bool V(semaphore & this) with( this ) {
[ac2b598]689 $thread * thrd = 0p;
[65deb18]690 lock( lock __cfaabi_dbg_ctx2 );
691 count += 1;
692 if ( count <= 0 ) {
[bdeba0b]693 // remove task at head of waiting list
[65deb18]694 thrd = pop_head( waiting );
[bd98b58]695 }
[bdeba0b]696
[65deb18]697 unlock( lock );
[bdeba0b]698
699 // make new owner
[e235429]700 unpark( thrd );
[f0ce5f4]701
[d384787]702 return thrd != 0p;
703}
704
705bool V(semaphore & this, unsigned diff) with( this ) {
706 $thread * thrd = 0p;
707 lock( lock __cfaabi_dbg_ctx2 );
708 int release = max(-count, (int)diff);
709 count += diff;
710 for(release) {
[e235429]711 unpark( pop_head( waiting ) );
[d384787]712 }
713
714 unlock( lock );
715
[f0ce5f4]716 return thrd != 0p;
[bd98b58]717}
718
[de94a60]719//-----------------------------------------------------------------------------
720// Debug
721__cfaabi_dbg_debug_do(
[1997b4e]722 extern "C" {
[ae66348]723 void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]) {
[1997b4e]724 this.prev_name = prev_name;
[8fc652e0]725 this.prev_thrd = kernelTLS().this_thread;
[1997b4e]726 }
[9181f1d]727 }
[f7d6bb0]728)
[2026bb6]729
730//-----------------------------------------------------------------------------
731// Debug
[8c50aed]732bool threading_enabled(void) __attribute__((const)) {
[2026bb6]733 return true;
734}
[c34ebf2]735
736//-----------------------------------------------------------------------------
737// Statistics
738#if !defined(__CFA_NO_STATISTICS__)
739 void print_halts( processor & this ) {
740 this.print_halts = true;
741 }
[58688bf]742
743 void print_stats_now( cluster & this, int flags ) {
744 __print_stats( this.stats, this.print_stats, true, this.name, (void*)&this );
745 }
[c34ebf2]746#endif
[8118303]747// Local Variables: //
748// mode: c //
749// tab-width: 4 //
750// End: //
Note: See TracBrowser for help on using the repository browser.