[75f3522] | 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 | // |
---|
[73abe95] | 7 | // kernel_private.hfa -- |
---|
[75f3522] | 8 | // |
---|
| 9 | // Author : Thierry Delisle |
---|
| 10 | // Created On : Mon Feb 13 12:27:26 2017 |
---|
[6b0b624] | 11 | // Last Modified By : Peter A. Buhr |
---|
[fd9b524] | 12 | // Last Modified On : Wed Aug 12 08:21:33 2020 |
---|
| 13 | // Update Count : 9 |
---|
[75f3522] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[75f3522] | 17 | |
---|
[58b6d1b] | 18 | #include "kernel.hfa" |
---|
| 19 | #include "thread.hfa" |
---|
[75f3522] | 20 | |
---|
[73abe95] | 21 | #include "alarm.hfa" |
---|
[8834751] | 22 | #include "stats.hfa" |
---|
[fa21ac9] | 23 | |
---|
[75f3522] | 24 | //----------------------------------------------------------------------------- |
---|
| 25 | // Scheduler |
---|
[1c273d0] | 26 | |
---|
[37ba662] | 27 | struct __attribute__((aligned(128))) __scheduler_lock_id_t; |
---|
[9b1dcc2] | 28 | |
---|
[1c273d0] | 29 | extern "C" { |
---|
[2026bb6] | 30 | void disable_interrupts() OPTIONAL_THREAD; |
---|
[969b3fe] | 31 | void enable_interrupts_noPoll(); |
---|
[36982fc] | 32 | void enable_interrupts( __cfaabi_dbg_ctx_param ); |
---|
[1c273d0] | 33 | } |
---|
| 34 | |
---|
[e873838] | 35 | void __schedule_thread( $thread * ) |
---|
[efc171d1] | 36 | #if defined(NDEBUG) || (!defined(__CFA_DEBUG__) && !defined(__CFA_VERIFY__)) |
---|
[e873838] | 37 | __attribute__((nonnull (1))) |
---|
[efc171d1] | 38 | #endif |
---|
| 39 | ; |
---|
[75f3522] | 40 | |
---|
[8fc652e0] | 41 | extern bool __preemption_enabled(); |
---|
| 42 | |
---|
[5afb49a] | 43 | //release/wake-up the following resources |
---|
| 44 | void __thread_finish( $thread * thrd ); |
---|
[db6f06a] | 45 | |
---|
[75f3522] | 46 | //----------------------------------------------------------------------------- |
---|
| 47 | // Processor |
---|
| 48 | void main(processorCtx_t *); |
---|
[85b1deb] | 49 | |
---|
[8c50aed] | 50 | void * __create_pthread( pthread_t *, void * (*)(void *), void * ); |
---|
[1805b1b] | 51 | |
---|
[85b1deb] | 52 | |
---|
[75f3522] | 53 | |
---|
[6502a2b] | 54 | extern cluster * mainCluster; |
---|
| 55 | |
---|
[75f3522] | 56 | //----------------------------------------------------------------------------- |
---|
| 57 | // Threads |
---|
| 58 | extern "C" { |
---|
[c7a900a] | 59 | void __cfactx_invoke_thread(void (*main)(void *), void * this); |
---|
[75f3522] | 60 | } |
---|
| 61 | |
---|
[f7d6bb0] | 62 | __cfaabi_dbg_debug_do( |
---|
[ac2b598] | 63 | extern void __cfaabi_dbg_thread_register ( $thread * thrd ); |
---|
| 64 | extern void __cfaabi_dbg_thread_unregister( $thread * thrd ); |
---|
[f7d6bb0] | 65 | ) |
---|
| 66 | |
---|
[6a77224] | 67 | #define TICKET_BLOCKED (-1) // thread is blocked |
---|
| 68 | #define TICKET_RUNNING ( 0) // thread is running |
---|
| 69 | #define TICKET_UNBLOCK ( 1) // thread should ignore next block |
---|
| 70 | |
---|
[969b3fe] | 71 | //----------------------------------------------------------------------------- |
---|
| 72 | // Utils |
---|
[ac2b598] | 73 | void doregister( struct cluster * cltr, struct $thread & thrd ); |
---|
| 74 | void unregister( struct cluster * cltr, struct $thread & thrd ); |
---|
[de94a60] | 75 | |
---|
[f00b26d4] | 76 | //----------------------------------------------------------------------------- |
---|
| 77 | // I/O |
---|
| 78 | void ^?{}(io_context & this, bool ); |
---|
| 79 | |
---|
[7768b8d] | 80 | //======================================================================= |
---|
| 81 | // Cluster lock API |
---|
| 82 | //======================================================================= |
---|
[b388ee81] | 83 | // Cells use by the reader writer lock |
---|
| 84 | // while not generic it only relies on a opaque pointer |
---|
[37ba662] | 85 | struct __attribute__((aligned(128))) __scheduler_lock_id_t { |
---|
[64a7146] | 86 | // Spin lock used as the underlying lock |
---|
[7768b8d] | 87 | volatile bool lock; |
---|
[64a7146] | 88 | |
---|
| 89 | // Handle pointing to the proc owning this cell |
---|
| 90 | // Used for allocating cells and debugging |
---|
| 91 | __processor_id_t * volatile handle; |
---|
| 92 | |
---|
| 93 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 94 | // Debug, check if this is owned for reading |
---|
| 95 | bool owned; |
---|
| 96 | #endif |
---|
[7768b8d] | 97 | }; |
---|
| 98 | |
---|
[64a7146] | 99 | static_assert( sizeof(struct __scheduler_lock_id_t) <= __alignof(struct __scheduler_lock_id_t)); |
---|
| 100 | |
---|
[7768b8d] | 101 | // Lock-Free registering/unregistering of threads |
---|
| 102 | // Register a processor to a given cluster and get its unique id in return |
---|
[9b1dcc2] | 103 | unsigned doregister( struct __processor_id_t * proc ); |
---|
[7768b8d] | 104 | |
---|
| 105 | // Unregister a processor from a given cluster using its id, getting back the original pointer |
---|
[9b1dcc2] | 106 | void unregister( struct __processor_id_t * proc ); |
---|
[7768b8d] | 107 | |
---|
[1eb239e4] | 108 | //----------------------------------------------------------------------- |
---|
| 109 | // Cluster idle lock/unlock |
---|
| 110 | static inline void lock(__cluster_idles & this) { |
---|
| 111 | for() { |
---|
| 112 | uint64_t l = this.lock; |
---|
| 113 | if( |
---|
| 114 | (0 == (l % 2)) |
---|
| 115 | && __atomic_compare_exchange_n(&this.lock, &l, l + 1, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST) |
---|
| 116 | ) return; |
---|
| 117 | Pause(); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | static inline void unlock(__cluster_idles & this) { |
---|
| 122 | /* paranoid */ verify( 1 == (this.lock % 2) ); |
---|
| 123 | __atomic_fetch_add( &this.lock, 1, __ATOMIC_SEQ_CST ); |
---|
| 124 | } |
---|
| 125 | |
---|
[7768b8d] | 126 | //======================================================================= |
---|
| 127 | // Reader-writer lock implementation |
---|
| 128 | // Concurrent with doregister/unregister, |
---|
| 129 | // i.e., threads can be added at any point during or between the entry/exit |
---|
[dca5802] | 130 | |
---|
| 131 | //----------------------------------------------------------------------- |
---|
| 132 | // simple spinlock underlying the RWLock |
---|
| 133 | // Blocking acquire |
---|
[7768b8d] | 134 | static inline void __atomic_acquire(volatile bool * ll) { |
---|
| 135 | while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) { |
---|
| 136 | while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED)) |
---|
[fd9b524] | 137 | Pause(); |
---|
[7768b8d] | 138 | } |
---|
| 139 | /* paranoid */ verify(*ll); |
---|
| 140 | } |
---|
| 141 | |
---|
[dca5802] | 142 | // Non-Blocking acquire |
---|
[7768b8d] | 143 | static inline bool __atomic_try_acquire(volatile bool * ll) { |
---|
[b798713] | 144 | return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST); |
---|
[7768b8d] | 145 | } |
---|
| 146 | |
---|
[dca5802] | 147 | // Release |
---|
[7768b8d] | 148 | static inline void __atomic_unlock(volatile bool * ll) { |
---|
| 149 | /* paranoid */ verify(*ll); |
---|
| 150 | __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE); |
---|
| 151 | } |
---|
| 152 | |
---|
[b388ee81] | 153 | //----------------------------------------------------------------------- |
---|
| 154 | // Reader-Writer lock protecting the ready-queues |
---|
| 155 | // while this lock is mostly generic some aspects |
---|
| 156 | // have been hard-coded to for the ready-queue for |
---|
| 157 | // simplicity and performance |
---|
| 158 | struct __scheduler_RWLock_t { |
---|
| 159 | // total cachelines allocated |
---|
| 160 | unsigned int max; |
---|
| 161 | |
---|
| 162 | // cachelines currently in use |
---|
| 163 | volatile unsigned int alloc; |
---|
| 164 | |
---|
| 165 | // cachelines ready to itereate over |
---|
| 166 | // (!= to alloc when thread is in second half of doregister) |
---|
| 167 | volatile unsigned int ready; |
---|
| 168 | |
---|
| 169 | // writer lock |
---|
| 170 | volatile bool lock; |
---|
| 171 | |
---|
| 172 | // data pointer |
---|
[9b1dcc2] | 173 | __scheduler_lock_id_t * data; |
---|
[b388ee81] | 174 | }; |
---|
| 175 | |
---|
| 176 | void ?{}(__scheduler_RWLock_t & this); |
---|
| 177 | void ^?{}(__scheduler_RWLock_t & this); |
---|
| 178 | |
---|
| 179 | extern __scheduler_RWLock_t * __scheduler_lock; |
---|
| 180 | |
---|
[7768b8d] | 181 | //----------------------------------------------------------------------- |
---|
| 182 | // Reader side : acquire when using the ready queue to schedule but not |
---|
| 183 | // creating/destroying queues |
---|
[e873838] | 184 | static inline void ready_schedule_lock(void) with(*__scheduler_lock) { |
---|
[8fc652e0] | 185 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 186 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
[e873838] | 187 | |
---|
[8fc652e0] | 188 | unsigned iproc = kernelTLS().this_proc_id->id; |
---|
| 189 | /*paranoid*/ verify(data[iproc].handle == kernelTLS().this_proc_id); |
---|
[7768b8d] | 190 | /*paranoid*/ verify(iproc < ready); |
---|
| 191 | |
---|
| 192 | // Step 1 : make sure no writer are in the middle of the critical section |
---|
| 193 | while(__atomic_load_n(&lock, (int)__ATOMIC_RELAXED)) |
---|
[fd9b524] | 194 | Pause(); |
---|
[7768b8d] | 195 | |
---|
| 196 | // Fence needed because we don't want to start trying to acquire the lock |
---|
| 197 | // before we read a false. |
---|
| 198 | // Not needed on x86 |
---|
| 199 | // std::atomic_thread_fence(std::memory_order_seq_cst); |
---|
| 200 | |
---|
| 201 | // Step 2 : acquire our local lock |
---|
| 202 | __atomic_acquire( &data[iproc].lock ); |
---|
| 203 | /*paranoid*/ verify(data[iproc].lock); |
---|
[64a7146] | 204 | |
---|
| 205 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 206 | // Debug, check if this is owned for reading |
---|
| 207 | data[iproc].owned = true; |
---|
| 208 | #endif |
---|
[7768b8d] | 209 | } |
---|
| 210 | |
---|
[e873838] | 211 | static inline void ready_schedule_unlock(void) with(*__scheduler_lock) { |
---|
[8fc652e0] | 212 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 213 | /* paranoid */ verify( kernelTLS().this_proc_id ); |
---|
[e873838] | 214 | |
---|
[8fc652e0] | 215 | unsigned iproc = kernelTLS().this_proc_id->id; |
---|
| 216 | /*paranoid*/ verify(data[iproc].handle == kernelTLS().this_proc_id); |
---|
[7768b8d] | 217 | /*paranoid*/ verify(iproc < ready); |
---|
| 218 | /*paranoid*/ verify(data[iproc].lock); |
---|
[64a7146] | 219 | /*paranoid*/ verify(data[iproc].owned); |
---|
| 220 | #ifdef __CFA_WITH_VERIFY__ |
---|
| 221 | // Debug, check if this is owned for reading |
---|
| 222 | data[iproc].owned = false; |
---|
| 223 | #endif |
---|
[dca5802] | 224 | __atomic_unlock(&data[iproc].lock); |
---|
[7768b8d] | 225 | } |
---|
| 226 | |
---|
[64a7146] | 227 | #ifdef __CFA_WITH_VERIFY__ |
---|
[e873838] | 228 | static inline bool ready_schedule_islocked(void) { |
---|
[8fc652e0] | 229 | /* paranoid */ verify( ! __preemption_enabled() ); |
---|
| 230 | /*paranoid*/ verify( kernelTLS().this_proc_id ); |
---|
| 231 | __processor_id_t * proc = kernelTLS().this_proc_id; |
---|
[64a7146] | 232 | return __scheduler_lock->data[proc->id].owned; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | static inline bool ready_mutate_islocked() { |
---|
| 236 | return __scheduler_lock->lock; |
---|
| 237 | } |
---|
| 238 | #endif |
---|
| 239 | |
---|
[7768b8d] | 240 | //----------------------------------------------------------------------- |
---|
| 241 | // Writer side : acquire when changing the ready queue, e.g. adding more |
---|
| 242 | // queues or removing them. |
---|
[b388ee81] | 243 | uint_fast32_t ready_mutate_lock( void ); |
---|
[7768b8d] | 244 | |
---|
[b388ee81] | 245 | void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ ); |
---|
[7768b8d] | 246 | |
---|
[b798713] | 247 | //======================================================================= |
---|
| 248 | // Ready-Queue API |
---|
[64a7146] | 249 | //----------------------------------------------------------------------- |
---|
| 250 | // pop thread from the ready queue of a cluster |
---|
| 251 | // returns 0p if empty |
---|
| 252 | __attribute__((hot)) bool query(struct cluster * cltr); |
---|
| 253 | |
---|
[dca5802] | 254 | //----------------------------------------------------------------------- |
---|
| 255 | // push thread onto a ready queue for a cluster |
---|
| 256 | // returns true if the list was previously empty, false otherwise |
---|
[504a7dc] | 257 | __attribute__((hot)) bool push(struct cluster * cltr, struct $thread * thrd); |
---|
[dca5802] | 258 | |
---|
| 259 | //----------------------------------------------------------------------- |
---|
| 260 | // pop thread from the ready queue of a cluster |
---|
| 261 | // returns 0p if empty |
---|
[1eb239e4] | 262 | // May return 0p spuriously |
---|
[504a7dc] | 263 | __attribute__((hot)) struct $thread * pop(struct cluster * cltr); |
---|
[dca5802] | 264 | |
---|
[1eb239e4] | 265 | //----------------------------------------------------------------------- |
---|
| 266 | // pop thread from the ready queue of a cluster |
---|
| 267 | // returns 0p if empty |
---|
| 268 | // guaranteed to find any threads added before this call |
---|
| 269 | __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr); |
---|
| 270 | |
---|
[13c5e19] | 271 | //----------------------------------------------------------------------- |
---|
| 272 | // remove thread from the ready queue of a cluster |
---|
| 273 | // returns bool if it wasn't found |
---|
| 274 | bool remove_head(struct cluster * cltr, struct $thread * thrd); |
---|
| 275 | |
---|
[dca5802] | 276 | //----------------------------------------------------------------------- |
---|
| 277 | // Increase the width of the ready queue (number of lanes) by 4 |
---|
[320ec6fc] | 278 | void ready_queue_grow (struct cluster * cltr, int target); |
---|
[dca5802] | 279 | |
---|
| 280 | //----------------------------------------------------------------------- |
---|
| 281 | // Decrease the width of the ready queue (number of lanes) by 4 |
---|
[320ec6fc] | 282 | void ready_queue_shrink(struct cluster * cltr, int target); |
---|
[b798713] | 283 | |
---|
[de94a60] | 284 | |
---|
[75f3522] | 285 | // Local Variables: // |
---|
| 286 | // mode: c // |
---|
| 287 | // tab-width: 4 // |
---|
[4aa2fb2] | 288 | // End: // |
---|