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