// // Cforall Version 1.0.0 Copyright (C) 2016 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // kernel_private.hfa -- // // Author : Thierry Delisle // Created On : Mon Feb 13 12:27:26 2017 // Last Modified By : Peter A. Buhr // Last Modified On : Sat Nov 30 19:25:02 2019 // Update Count : 8 // #pragma once #include "kernel.hfa" #include "thread.hfa" #include "alarm.hfa" //----------------------------------------------------------------------------- // Scheduler extern "C" { void disable_interrupts() OPTIONAL_THREAD; void enable_interrupts_noPoll(); void enable_interrupts( __cfaabi_dbg_ctx_param ); } void ScheduleThread( thread_desc * ) __attribute__((nonnull (1))); static inline void WakeThread( thread_desc * thrd, bool must_yield ) { if( !thrd ) return; enum coroutine_state new_state = must_yield ? Reschedule : Rerun; disable_interrupts(); static_assert(sizeof(thrd->state) == sizeof(int)); enum coroutine_state old_state = (enum coroutine_state)__atomic_exchange_n((volatile int *)&thrd->state, (int)new_state, __ATOMIC_SEQ_CST); switch(old_state) { case Active: // Wake won the race, the thread will reschedule/rerun itself break; case Inactive: // Wake lost the race, thrd->state = Inactive; ScheduleThread( thrd ); break; case Rerun: case Reschedule: abort("More than one thread attempted to schedule thread %p\n", thrd); break; case Halted: case Start: case Primed: default: // This makes no sense, something is wrong abort abort(); } enable_interrupts( __cfaabi_dbg_ctx ); } thread_desc * nextThread(cluster * this); //Block current thread and release/wake-up the following resources void BlockInternal(void); void BlockInternal(__spinlock_t * lock); void BlockInternal(thread_desc * thrd); void BlockInternal(__spinlock_t * lock, thread_desc * thrd); void BlockInternal(__spinlock_t * locks [], unsigned short count); void BlockInternal(__spinlock_t * locks [], unsigned short count, thread_desc * thrds [], unsigned short thrd_count); void BlockInternal(__finish_callback_fptr_t callback); void LeaveThread(__spinlock_t * lock, thread_desc * thrd); //----------------------------------------------------------------------------- // Processor void main(processorCtx_t *); void * create_pthread( pthread_t *, void * (*)(void *), void * ); static inline void wake_fast(processor * this) { __cfaabi_dbg_print_safe("Kernel : Waking up processor %p\n", this); post( this->idleLock ); } static inline void wake(processor * this) { disable_interrupts(); wake_fast(this); enable_interrupts( __cfaabi_dbg_ctx ); } struct event_kernel_t { alarm_list_t alarms; __spinlock_t lock; }; extern event_kernel_t * event_kernel; struct __cfa_kernel_preemption_state_t { bool enabled; bool in_progress; unsigned short disable_count; }; extern volatile thread_local __cfa_kernel_preemption_state_t preemption_state __attribute__ ((tls_model ( "initial-exec" ))); //----------------------------------------------------------------------------- // Threads extern "C" { void CtxInvokeThread(void (*main)(void *), void * this); } extern void ThreadCtxSwitch(coroutine_desc * src, coroutine_desc * dst); __cfaabi_dbg_debug_do( extern void __cfaabi_dbg_thread_register ( thread_desc * thrd ); extern void __cfaabi_dbg_thread_unregister( thread_desc * thrd ); ) //----------------------------------------------------------------------------- // Utils #define KERNEL_STORAGE(T,X) static char storage_##X[sizeof(T)] static inline uint32_t tls_rand() { kernelTLS.rand_seed ^= kernelTLS.rand_seed << 6; kernelTLS.rand_seed ^= kernelTLS.rand_seed >> 21; kernelTLS.rand_seed ^= kernelTLS.rand_seed << 7; return kernelTLS.rand_seed; } void doregister( struct cluster & cltr ); void unregister( struct cluster & cltr ); void doregister( struct cluster * cltr, struct thread_desc & thrd ); void unregister( struct cluster * cltr, struct thread_desc & thrd ); void doregister( struct cluster * cltr, struct processor * proc ); void unregister( struct cluster * cltr, struct processor * proc ); // Local Variables: // // mode: c // // tab-width: 4 // // End: //