[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 |
|
---|
[3489ea6] | 18 | #if !defined(__cforall_thread__)
|
---|
| 19 | #error kernel_private.hfa should only be included in libcfathread source
|
---|
| 20 | #endif
|
---|
| 21 |
|
---|
[58b6d1b] | 22 | #include "kernel.hfa"
|
---|
| 23 | #include "thread.hfa"
|
---|
[75f3522] | 24 |
|
---|
[73abe95] | 25 | #include "alarm.hfa"
|
---|
[8834751] | 26 | #include "stats.hfa"
|
---|
[fa21ac9] | 27 |
|
---|
[3489ea6] | 28 | extern "C" {
|
---|
| 29 | #if defined(CFA_HAVE_LINUX_LIBRSEQ)
|
---|
| 30 | #include <rseq/rseq.h>
|
---|
| 31 | #elif defined(CFA_HAVE_LINUX_RSEQ_H)
|
---|
[3814957] | 32 | // #include <linux/rseq.h>
|
---|
[3489ea6] | 33 | #else
|
---|
| 34 | #ifndef _GNU_SOURCE
|
---|
| 35 | #error kernel_private requires gnu_source
|
---|
| 36 | #endif
|
---|
| 37 | #include <sched.h>
|
---|
| 38 | #endif
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[75f3522] | 41 | //-----------------------------------------------------------------------------
|
---|
| 42 | // Scheduler
|
---|
[1c273d0] | 43 | extern "C" {
|
---|
[2026bb6] | 44 | void disable_interrupts() OPTIONAL_THREAD;
|
---|
[a3821fa] | 45 | void enable_interrupts( bool poll = true );
|
---|
[1c273d0] | 46 | }
|
---|
| 47 |
|
---|
[254ad1b] | 48 | void schedule_thread$( $thread * ) __attribute__((nonnull (1)));
|
---|
[75f3522] | 49 |
|
---|
[8fc652e0] | 50 | extern bool __preemption_enabled();
|
---|
| 51 |
|
---|
[5afb49a] | 52 | //release/wake-up the following resources
|
---|
| 53 | void __thread_finish( $thread * thrd );
|
---|
[db6f06a] | 54 |
|
---|
[3489ea6] | 55 | //-----------------------------------------------------------------------------
|
---|
| 56 | // Hardware
|
---|
| 57 |
|
---|
| 58 | #if defined(CFA_HAVE_LINUX_LIBRSEQ)
|
---|
| 59 | // No data needed
|
---|
| 60 | #elif defined(CFA_HAVE_LINUX_RSEQ_H)
|
---|
| 61 | extern "Cforall" {
|
---|
[3814957] | 62 | // extern __attribute__((aligned(128))) thread_local volatile struct rseq __cfaabi_rseq;
|
---|
[3489ea6] | 63 | }
|
---|
| 64 | #else
|
---|
| 65 | // No data needed
|
---|
| 66 | #endif
|
---|
| 67 |
|
---|
| 68 | static inline int __kernel_getcpu() {
|
---|
| 69 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 70 | #if defined(CFA_HAVE_LINUX_LIBRSEQ)
|
---|
| 71 | return rseq_current_cpu_raw();
|
---|
| 72 | #elif defined(CFA_HAVE_LINUX_RSEQ_H)
|
---|
[3814957] | 73 | // return __cfaabi_rseq.cpu_id;
|
---|
[3489ea6] | 74 | #else
|
---|
| 75 | return sched_getcpu();
|
---|
| 76 | #endif
|
---|
| 77 | }
|
---|
| 78 |
|
---|
[75f3522] | 79 | //-----------------------------------------------------------------------------
|
---|
| 80 | // Processor
|
---|
| 81 | void main(processorCtx_t *);
|
---|
[85b1deb] | 82 |
|
---|
[8c50aed] | 83 | void * __create_pthread( pthread_t *, void * (*)(void *), void * );
|
---|
[bfcf6b9] | 84 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval );
|
---|
[1805b1b] | 85 |
|
---|
[6502a2b] | 86 | extern cluster * mainCluster;
|
---|
| 87 |
|
---|
[75f3522] | 88 | //-----------------------------------------------------------------------------
|
---|
| 89 | // Threads
|
---|
| 90 | extern "C" {
|
---|
[c7a900a] | 91 | void __cfactx_invoke_thread(void (*main)(void *), void * this);
|
---|
[75f3522] | 92 | }
|
---|
| 93 |
|
---|
[f7d6bb0] | 94 | __cfaabi_dbg_debug_do(
|
---|
[ac2b598] | 95 | extern void __cfaabi_dbg_thread_register ( $thread * thrd );
|
---|
| 96 | extern void __cfaabi_dbg_thread_unregister( $thread * thrd );
|
---|
[f7d6bb0] | 97 | )
|
---|
| 98 |
|
---|
[6a77224] | 99 | #define TICKET_BLOCKED (-1) // thread is blocked
|
---|
| 100 | #define TICKET_RUNNING ( 0) // thread is running
|
---|
| 101 | #define TICKET_UNBLOCK ( 1) // thread should ignore next block
|
---|
| 102 |
|
---|
[969b3fe] | 103 | //-----------------------------------------------------------------------------
|
---|
| 104 | // Utils
|
---|
[ac2b598] | 105 | void doregister( struct cluster * cltr, struct $thread & thrd );
|
---|
| 106 | void unregister( struct cluster * cltr, struct $thread & thrd );
|
---|
[de94a60] | 107 |
|
---|
[f00b26d4] | 108 | //-----------------------------------------------------------------------------
|
---|
| 109 | // I/O
|
---|
[78da4ab] | 110 | $io_arbiter * create(void);
|
---|
| 111 | void destroy($io_arbiter *);
|
---|
[f00b26d4] | 112 |
|
---|
[7768b8d] | 113 | //=======================================================================
|
---|
| 114 | // Cluster lock API
|
---|
| 115 | //=======================================================================
|
---|
| 116 | // Lock-Free registering/unregistering of threads
|
---|
| 117 | // Register a processor to a given cluster and get its unique id in return
|
---|
[c993b15] | 118 | unsigned register_proc_id( void );
|
---|
[7768b8d] | 119 |
|
---|
| 120 | // Unregister a processor from a given cluster using its id, getting back the original pointer
|
---|
[c993b15] | 121 | void unregister_proc_id( unsigned );
|
---|
[7768b8d] | 122 |
|
---|
| 123 | //=======================================================================
|
---|
| 124 | // Reader-writer lock implementation
|
---|
| 125 | // Concurrent with doregister/unregister,
|
---|
| 126 | // i.e., threads can be added at any point during or between the entry/exit
|
---|
[dca5802] | 127 |
|
---|
| 128 | //-----------------------------------------------------------------------
|
---|
| 129 | // simple spinlock underlying the RWLock
|
---|
| 130 | // Blocking acquire
|
---|
[7768b8d] | 131 | static inline void __atomic_acquire(volatile bool * ll) {
|
---|
| 132 | while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
|
---|
| 133 | while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
|
---|
[fd9b524] | 134 | Pause();
|
---|
[7768b8d] | 135 | }
|
---|
| 136 | /* paranoid */ verify(*ll);
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[dca5802] | 139 | // Non-Blocking acquire
|
---|
[7768b8d] | 140 | static inline bool __atomic_try_acquire(volatile bool * ll) {
|
---|
[b798713] | 141 | return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
|
---|
[7768b8d] | 142 | }
|
---|
| 143 |
|
---|
[dca5802] | 144 | // Release
|
---|
[7768b8d] | 145 | static inline void __atomic_unlock(volatile bool * ll) {
|
---|
| 146 | /* paranoid */ verify(*ll);
|
---|
| 147 | __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
|
---|
| 148 | }
|
---|
| 149 |
|
---|
[a33c113] | 150 |
|
---|
| 151 |
|
---|
| 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
|
---|
[c993b15] | 171 | volatile bool write_lock;
|
---|
[b388ee81] | 172 |
|
---|
| 173 | // data pointer
|
---|
[c993b15] | 174 | volatile bool * volatile * 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() );
|
---|
[c993b15] | 187 | /* paranoid */ verify( ! kernelTLS().in_sched_lock );
|
---|
| 188 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
|
---|
| 189 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
|
---|
[7768b8d] | 190 |
|
---|
| 191 | // Step 1 : make sure no writer are in the middle of the critical section
|
---|
[c993b15] | 192 | while(__atomic_load_n(&write_lock, (int)__ATOMIC_RELAXED))
|
---|
[fd9b524] | 193 | Pause();
|
---|
[7768b8d] | 194 |
|
---|
| 195 | // Fence needed because we don't want to start trying to acquire the lock
|
---|
| 196 | // before we read a false.
|
---|
| 197 | // Not needed on x86
|
---|
| 198 | // std::atomic_thread_fence(std::memory_order_seq_cst);
|
---|
| 199 |
|
---|
| 200 | // Step 2 : acquire our local lock
|
---|
[c993b15] | 201 | __atomic_acquire( &kernelTLS().sched_lock );
|
---|
| 202 | /*paranoid*/ verify(kernelTLS().sched_lock);
|
---|
[64a7146] | 203 |
|
---|
| 204 | #ifdef __CFA_WITH_VERIFY__
|
---|
| 205 | // Debug, check if this is owned for reading
|
---|
[c993b15] | 206 | kernelTLS().in_sched_lock = true;
|
---|
[64a7146] | 207 | #endif
|
---|
[7768b8d] | 208 | }
|
---|
| 209 |
|
---|
[e873838] | 210 | static inline void ready_schedule_unlock(void) with(*__scheduler_lock) {
|
---|
[8fc652e0] | 211 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[c993b15] | 212 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
|
---|
| 213 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
|
---|
| 214 | /* paranoid */ verify( kernelTLS().sched_lock );
|
---|
| 215 | /* paranoid */ verify( kernelTLS().in_sched_lock );
|
---|
[64a7146] | 216 | #ifdef __CFA_WITH_VERIFY__
|
---|
| 217 | // Debug, check if this is owned for reading
|
---|
[c993b15] | 218 | kernelTLS().in_sched_lock = false;
|
---|
[64a7146] | 219 | #endif
|
---|
[c993b15] | 220 | __atomic_unlock(&kernelTLS().sched_lock);
|
---|
[7768b8d] | 221 | }
|
---|
| 222 |
|
---|
[64a7146] | 223 | #ifdef __CFA_WITH_VERIFY__
|
---|
[e873838] | 224 | static inline bool ready_schedule_islocked(void) {
|
---|
[8fc652e0] | 225 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[c993b15] | 226 | /* paranoid */ verify( (!kernelTLS().in_sched_lock) || kernelTLS().sched_lock );
|
---|
| 227 | return kernelTLS().sched_lock;
|
---|
[64a7146] | 228 | }
|
---|
| 229 |
|
---|
| 230 | static inline bool ready_mutate_islocked() {
|
---|
[c993b15] | 231 | return __scheduler_lock->write_lock;
|
---|
[64a7146] | 232 | }
|
---|
| 233 | #endif
|
---|
| 234 |
|
---|
[7768b8d] | 235 | //-----------------------------------------------------------------------
|
---|
| 236 | // Writer side : acquire when changing the ready queue, e.g. adding more
|
---|
| 237 | // queues or removing them.
|
---|
[b388ee81] | 238 | uint_fast32_t ready_mutate_lock( void );
|
---|
[7768b8d] | 239 |
|
---|
[b388ee81] | 240 | void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
|
---|
[7768b8d] | 241 |
|
---|
[a33c113] | 242 | //-----------------------------------------------------------------------
|
---|
| 243 | // Lock-Free registering/unregistering of threads
|
---|
| 244 | // Register a processor to a given cluster and get its unique id in return
|
---|
| 245 | // For convenience, also acquires the lock
|
---|
[c993b15] | 246 | static inline [unsigned, uint_fast32_t] ready_mutate_register() {
|
---|
| 247 | unsigned id = register_proc_id();
|
---|
| 248 | uint_fast32_t last = ready_mutate_lock();
|
---|
| 249 | return [id, last];
|
---|
[a33c113] | 250 | }
|
---|
| 251 |
|
---|
| 252 | // Unregister a processor from a given cluster using its id, getting back the original pointer
|
---|
| 253 | // assumes the lock is acquired
|
---|
[c993b15] | 254 | static inline void ready_mutate_unregister( unsigned id, uint_fast32_t last_s ) {
|
---|
[a33c113] | 255 | ready_mutate_unlock( last_s );
|
---|
[c993b15] | 256 | unregister_proc_id( id );
|
---|
[a33c113] | 257 | }
|
---|
| 258 |
|
---|
[a7504db5] | 259 | //-----------------------------------------------------------------------
|
---|
| 260 | // Cluster idle lock/unlock
|
---|
[6a9b12b] | 261 | static inline void lock(__cluster_proc_list & this) {
|
---|
[a7504db5] | 262 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 263 |
|
---|
| 264 | // Start by locking the global RWlock so that we know no-one is
|
---|
| 265 | // adding/removing processors while we mess with the idle lock
|
---|
| 266 | ready_schedule_lock();
|
---|
| 267 |
|
---|
| 268 | // Simple counting lock, acquired, acquired by incrementing the counter
|
---|
| 269 | // to an odd number
|
---|
| 270 | for() {
|
---|
| 271 | uint64_t l = this.lock;
|
---|
| 272 | if(
|
---|
| 273 | (0 == (l % 2))
|
---|
| 274 | && __atomic_compare_exchange_n(&this.lock, &l, l + 1, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)
|
---|
| 275 | ) return;
|
---|
| 276 | Pause();
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 280 | }
|
---|
| 281 |
|
---|
[6a9b12b] | 282 | static inline void unlock(__cluster_proc_list & this) {
|
---|
[a7504db5] | 283 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 284 |
|
---|
| 285 | /* paranoid */ verify( 1 == (this.lock % 2) );
|
---|
| 286 | // Simple couting lock, release by incrementing to an even number
|
---|
| 287 | __atomic_fetch_add( &this.lock, 1, __ATOMIC_SEQ_CST );
|
---|
| 288 |
|
---|
| 289 | // Release the global lock, which we acquired when locking
|
---|
| 290 | ready_schedule_unlock();
|
---|
| 291 |
|
---|
| 292 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[b798713] | 295 | //=======================================================================
|
---|
| 296 | // Ready-Queue API
|
---|
[dca5802] | 297 | //-----------------------------------------------------------------------
|
---|
| 298 | // push thread onto a ready queue for a cluster
|
---|
| 299 | // returns true if the list was previously empty, false otherwise
|
---|
[b808625] | 300 | __attribute__((hot)) void push(struct cluster * cltr, struct $thread * thrd, bool local);
|
---|
[dca5802] | 301 |
|
---|
| 302 | //-----------------------------------------------------------------------
|
---|
[fc59df78] | 303 | // pop thread from the local queues of a cluster
|
---|
[dca5802] | 304 | // returns 0p if empty
|
---|
[1eb239e4] | 305 | // May return 0p spuriously
|
---|
[431cd4f] | 306 | __attribute__((hot)) struct $thread * pop_fast(struct cluster * cltr);
|
---|
[dca5802] | 307 |
|
---|
[1eb239e4] | 308 | //-----------------------------------------------------------------------
|
---|
[fc59df78] | 309 | // pop thread from any ready queue of a cluster
|
---|
[1eb239e4] | 310 | // returns 0p if empty
|
---|
[fc59df78] | 311 | // May return 0p spuriously
|
---|
[1eb239e4] | 312 | __attribute__((hot)) struct $thread * pop_slow(struct cluster * cltr);
|
---|
| 313 |
|
---|
[fc59df78] | 314 | //-----------------------------------------------------------------------
|
---|
| 315 | // search all ready queues of a cluster for any thread
|
---|
| 316 | // returns 0p if empty
|
---|
| 317 | // guaranteed to find any threads added before this call
|
---|
| 318 | __attribute__((hot)) struct $thread * pop_search(struct cluster * cltr);
|
---|
| 319 |
|
---|
[dca5802] | 320 | //-----------------------------------------------------------------------
|
---|
| 321 | // Increase the width of the ready queue (number of lanes) by 4
|
---|
[a017ee7] | 322 | void ready_queue_grow (struct cluster * cltr);
|
---|
[dca5802] | 323 |
|
---|
| 324 | //-----------------------------------------------------------------------
|
---|
| 325 | // Decrease the width of the ready queue (number of lanes) by 4
|
---|
[a017ee7] | 326 | void ready_queue_shrink(struct cluster * cltr);
|
---|
[b798713] | 327 |
|
---|
[de94a60] | 328 |
|
---|
[75f3522] | 329 | // Local Variables: //
|
---|
| 330 | // mode: c //
|
---|
| 331 | // tab-width: 4 //
|
---|
[4aa2fb2] | 332 | // End: //
|
---|