[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)
|
---|
[f558b5f] | 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 |
|
---|
[d3605f8] | 41 | // Defines whether or not we *want* to use io_uring_enter as the idle_sleep blocking call
|
---|
| 42 | #define CFA_WANT_IO_URING_IDLE
|
---|
| 43 |
|
---|
| 44 | // Defines whether or not we *can* use io_uring_enter as the idle_sleep blocking call
|
---|
| 45 | #if defined(CFA_WANT_IO_URING_IDLE) && defined(CFA_HAVE_LINUX_IO_URING_H)
|
---|
| 46 | #if defined(CFA_HAVE_IORING_OP_READ) || (defined(CFA_HAVE_READV) && defined(CFA_HAVE_IORING_OP_READV))
|
---|
| 47 | #define CFA_WITH_IO_URING_IDLE
|
---|
| 48 | #endif
|
---|
| 49 | #endif
|
---|
[059ad16] | 50 |
|
---|
[75f3522] | 51 | //-----------------------------------------------------------------------------
|
---|
| 52 | // Scheduler
|
---|
[1c273d0] | 53 | extern "C" {
|
---|
[2026bb6] | 54 | void disable_interrupts() OPTIONAL_THREAD;
|
---|
[a3821fa] | 55 | void enable_interrupts( bool poll = true );
|
---|
[1c273d0] | 56 | }
|
---|
| 57 |
|
---|
[24e321c] | 58 | void schedule_thread$( thread$ *, unpark_hint hint ) __attribute__((nonnull (1)));
|
---|
[75f3522] | 59 |
|
---|
[8fc652e0] | 60 | extern bool __preemption_enabled();
|
---|
| 61 |
|
---|
[5afb49a] | 62 | //release/wake-up the following resources
|
---|
[e84ab3d] | 63 | void __thread_finish( thread$ * thrd );
|
---|
[db6f06a] | 64 |
|
---|
[3489ea6] | 65 | //-----------------------------------------------------------------------------
|
---|
| 66 | // Hardware
|
---|
| 67 |
|
---|
| 68 | #if defined(CFA_HAVE_LINUX_LIBRSEQ)
|
---|
| 69 | // No data needed
|
---|
| 70 | #elif defined(CFA_HAVE_LINUX_RSEQ_H)
|
---|
| 71 | extern "Cforall" {
|
---|
[f558b5f] | 72 | extern __attribute__((aligned(128))) thread_local volatile struct rseq __cfaabi_rseq;
|
---|
[3489ea6] | 73 | }
|
---|
| 74 | #else
|
---|
| 75 | // No data needed
|
---|
| 76 | #endif
|
---|
| 77 |
|
---|
| 78 | static inline int __kernel_getcpu() {
|
---|
| 79 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 80 | #if defined(CFA_HAVE_LINUX_LIBRSEQ)
|
---|
[f558b5f] | 81 | return rseq_current_cpu();
|
---|
[3489ea6] | 82 | #elif defined(CFA_HAVE_LINUX_RSEQ_H)
|
---|
[f558b5f] | 83 | int r = __cfaabi_rseq.cpu_id;
|
---|
| 84 | /* paranoid */ verify( r >= 0 );
|
---|
| 85 | return r;
|
---|
[3489ea6] | 86 | #else
|
---|
| 87 | return sched_getcpu();
|
---|
| 88 | #endif
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[75f3522] | 91 | //-----------------------------------------------------------------------------
|
---|
| 92 | // Processor
|
---|
| 93 | void main(processorCtx_t *);
|
---|
[85b1deb] | 94 |
|
---|
[8c50aed] | 95 | void * __create_pthread( pthread_t *, void * (*)(void *), void * );
|
---|
[bfcf6b9] | 96 | void __destroy_pthread( pthread_t pthread, void * stack, void ** retval );
|
---|
[1805b1b] | 97 |
|
---|
[6502a2b] | 98 | extern cluster * mainCluster;
|
---|
| 99 |
|
---|
[75f3522] | 100 | //-----------------------------------------------------------------------------
|
---|
| 101 | // Threads
|
---|
| 102 | extern "C" {
|
---|
[c7a900a] | 103 | void __cfactx_invoke_thread(void (*main)(void *), void * this);
|
---|
[75f3522] | 104 | }
|
---|
| 105 |
|
---|
[f7d6bb0] | 106 | __cfaabi_dbg_debug_do(
|
---|
[e84ab3d] | 107 | extern void __cfaabi_dbg_thread_register ( thread$ * thrd );
|
---|
| 108 | extern void __cfaabi_dbg_thread_unregister( thread$ * thrd );
|
---|
[f7d6bb0] | 109 | )
|
---|
| 110 |
|
---|
[6a77224] | 111 | #define TICKET_BLOCKED (-1) // thread is blocked
|
---|
| 112 | #define TICKET_RUNNING ( 0) // thread is running
|
---|
| 113 | #define TICKET_UNBLOCK ( 1) // thread should ignore next block
|
---|
| 114 |
|
---|
[969b3fe] | 115 | //-----------------------------------------------------------------------------
|
---|
| 116 | // Utils
|
---|
[e84ab3d] | 117 | void doregister( struct cluster * cltr, struct thread$ & thrd );
|
---|
| 118 | void unregister( struct cluster * cltr, struct thread$ & thrd );
|
---|
[de94a60] | 119 |
|
---|
[f00b26d4] | 120 | //-----------------------------------------------------------------------------
|
---|
| 121 | // I/O
|
---|
[78da4ab] | 122 | $io_arbiter * create(void);
|
---|
| 123 | void destroy($io_arbiter *);
|
---|
[f00b26d4] | 124 |
|
---|
[7768b8d] | 125 | //=======================================================================
|
---|
| 126 | // Cluster lock API
|
---|
| 127 | //=======================================================================
|
---|
| 128 | // Lock-Free registering/unregistering of threads
|
---|
| 129 | // Register a processor to a given cluster and get its unique id in return
|
---|
[c993b15] | 130 | unsigned register_proc_id( void );
|
---|
[7768b8d] | 131 |
|
---|
| 132 | // Unregister a processor from a given cluster using its id, getting back the original pointer
|
---|
[c993b15] | 133 | void unregister_proc_id( unsigned );
|
---|
[7768b8d] | 134 |
|
---|
| 135 | //=======================================================================
|
---|
| 136 | // Reader-writer lock implementation
|
---|
| 137 | // Concurrent with doregister/unregister,
|
---|
| 138 | // i.e., threads can be added at any point during or between the entry/exit
|
---|
[dca5802] | 139 |
|
---|
| 140 | //-----------------------------------------------------------------------
|
---|
| 141 | // simple spinlock underlying the RWLock
|
---|
| 142 | // Blocking acquire
|
---|
[7768b8d] | 143 | static inline void __atomic_acquire(volatile bool * ll) {
|
---|
| 144 | while( __builtin_expect(__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST), false) ) {
|
---|
| 145 | while(__atomic_load_n(ll, (int)__ATOMIC_RELAXED))
|
---|
[fd9b524] | 146 | Pause();
|
---|
[7768b8d] | 147 | }
|
---|
| 148 | /* paranoid */ verify(*ll);
|
---|
| 149 | }
|
---|
| 150 |
|
---|
[dca5802] | 151 | // Non-Blocking acquire
|
---|
[7768b8d] | 152 | static inline bool __atomic_try_acquire(volatile bool * ll) {
|
---|
[b798713] | 153 | return !__atomic_exchange_n(ll, (bool)true, __ATOMIC_SEQ_CST);
|
---|
[7768b8d] | 154 | }
|
---|
| 155 |
|
---|
[dca5802] | 156 | // Release
|
---|
[7768b8d] | 157 | static inline void __atomic_unlock(volatile bool * ll) {
|
---|
| 158 | /* paranoid */ verify(*ll);
|
---|
| 159 | __atomic_store_n(ll, (bool)false, __ATOMIC_RELEASE);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[b388ee81] | 162 | //-----------------------------------------------------------------------
|
---|
| 163 | // Reader-Writer lock protecting the ready-queues
|
---|
| 164 | // while this lock is mostly generic some aspects
|
---|
| 165 | // have been hard-coded to for the ready-queue for
|
---|
| 166 | // simplicity and performance
|
---|
| 167 | struct __scheduler_RWLock_t {
|
---|
| 168 | // total cachelines allocated
|
---|
| 169 | unsigned int max;
|
---|
| 170 |
|
---|
| 171 | // cachelines currently in use
|
---|
| 172 | volatile unsigned int alloc;
|
---|
| 173 |
|
---|
| 174 | // cachelines ready to itereate over
|
---|
| 175 | // (!= to alloc when thread is in second half of doregister)
|
---|
| 176 | volatile unsigned int ready;
|
---|
| 177 |
|
---|
| 178 | // writer lock
|
---|
[c993b15] | 179 | volatile bool write_lock;
|
---|
[b388ee81] | 180 |
|
---|
| 181 | // data pointer
|
---|
[c993b15] | 182 | volatile bool * volatile * data;
|
---|
[b388ee81] | 183 | };
|
---|
| 184 |
|
---|
| 185 | void ?{}(__scheduler_RWLock_t & this);
|
---|
| 186 | void ^?{}(__scheduler_RWLock_t & this);
|
---|
| 187 |
|
---|
| 188 | extern __scheduler_RWLock_t * __scheduler_lock;
|
---|
| 189 |
|
---|
[7768b8d] | 190 | //-----------------------------------------------------------------------
|
---|
| 191 | // Reader side : acquire when using the ready queue to schedule but not
|
---|
| 192 | // creating/destroying queues
|
---|
[e873838] | 193 | static inline void ready_schedule_lock(void) with(*__scheduler_lock) {
|
---|
[8fc652e0] | 194 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[c993b15] | 195 | /* paranoid */ verify( ! kernelTLS().in_sched_lock );
|
---|
| 196 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
|
---|
| 197 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
|
---|
[7768b8d] | 198 |
|
---|
| 199 | // Step 1 : make sure no writer are in the middle of the critical section
|
---|
[c993b15] | 200 | while(__atomic_load_n(&write_lock, (int)__ATOMIC_RELAXED))
|
---|
[fd9b524] | 201 | Pause();
|
---|
[7768b8d] | 202 |
|
---|
| 203 | // Fence needed because we don't want to start trying to acquire the lock
|
---|
| 204 | // before we read a false.
|
---|
| 205 | // Not needed on x86
|
---|
| 206 | // std::atomic_thread_fence(std::memory_order_seq_cst);
|
---|
| 207 |
|
---|
| 208 | // Step 2 : acquire our local lock
|
---|
[c993b15] | 209 | __atomic_acquire( &kernelTLS().sched_lock );
|
---|
| 210 | /*paranoid*/ verify(kernelTLS().sched_lock);
|
---|
[64a7146] | 211 |
|
---|
| 212 | #ifdef __CFA_WITH_VERIFY__
|
---|
| 213 | // Debug, check if this is owned for reading
|
---|
[c993b15] | 214 | kernelTLS().in_sched_lock = true;
|
---|
[64a7146] | 215 | #endif
|
---|
[7768b8d] | 216 | }
|
---|
| 217 |
|
---|
[e873838] | 218 | static inline void ready_schedule_unlock(void) with(*__scheduler_lock) {
|
---|
[8fc652e0] | 219 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[c993b15] | 220 | /* paranoid */ verify( data[kernelTLS().sched_id] == &kernelTLS().sched_lock );
|
---|
| 221 | /* paranoid */ verify( !kernelTLS().this_processor || kernelTLS().this_processor->unique_id == kernelTLS().sched_id );
|
---|
| 222 | /* paranoid */ verify( kernelTLS().sched_lock );
|
---|
| 223 | /* paranoid */ verify( kernelTLS().in_sched_lock );
|
---|
[64a7146] | 224 | #ifdef __CFA_WITH_VERIFY__
|
---|
| 225 | // Debug, check if this is owned for reading
|
---|
[c993b15] | 226 | kernelTLS().in_sched_lock = false;
|
---|
[64a7146] | 227 | #endif
|
---|
[c993b15] | 228 | __atomic_unlock(&kernelTLS().sched_lock);
|
---|
[7768b8d] | 229 | }
|
---|
| 230 |
|
---|
[64a7146] | 231 | #ifdef __CFA_WITH_VERIFY__
|
---|
[e873838] | 232 | static inline bool ready_schedule_islocked(void) {
|
---|
[8fc652e0] | 233 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
[c993b15] | 234 | /* paranoid */ verify( (!kernelTLS().in_sched_lock) || kernelTLS().sched_lock );
|
---|
| 235 | return kernelTLS().sched_lock;
|
---|
[64a7146] | 236 | }
|
---|
| 237 |
|
---|
| 238 | static inline bool ready_mutate_islocked() {
|
---|
[c993b15] | 239 | return __scheduler_lock->write_lock;
|
---|
[64a7146] | 240 | }
|
---|
| 241 | #endif
|
---|
| 242 |
|
---|
[7768b8d] | 243 | //-----------------------------------------------------------------------
|
---|
| 244 | // Writer side : acquire when changing the ready queue, e.g. adding more
|
---|
| 245 | // queues or removing them.
|
---|
[b388ee81] | 246 | uint_fast32_t ready_mutate_lock( void );
|
---|
[7768b8d] | 247 |
|
---|
[b388ee81] | 248 | void ready_mutate_unlock( uint_fast32_t /* value returned by lock */ );
|
---|
[7768b8d] | 249 |
|
---|
[a33c113] | 250 | //-----------------------------------------------------------------------
|
---|
| 251 | // Lock-Free registering/unregistering of threads
|
---|
| 252 | // Register a processor to a given cluster and get its unique id in return
|
---|
| 253 | // For convenience, also acquires the lock
|
---|
[c993b15] | 254 | static inline [unsigned, uint_fast32_t] ready_mutate_register() {
|
---|
| 255 | unsigned id = register_proc_id();
|
---|
| 256 | uint_fast32_t last = ready_mutate_lock();
|
---|
| 257 | return [id, last];
|
---|
[a33c113] | 258 | }
|
---|
| 259 |
|
---|
| 260 | // Unregister a processor from a given cluster using its id, getting back the original pointer
|
---|
| 261 | // assumes the lock is acquired
|
---|
[c993b15] | 262 | static inline void ready_mutate_unregister( unsigned id, uint_fast32_t last_s ) {
|
---|
[a33c113] | 263 | ready_mutate_unlock( last_s );
|
---|
[c993b15] | 264 | unregister_proc_id( id );
|
---|
[a33c113] | 265 | }
|
---|
| 266 |
|
---|
[a7504db5] | 267 | //-----------------------------------------------------------------------
|
---|
| 268 | // Cluster idle lock/unlock
|
---|
[6a9b12b] | 269 | static inline void lock(__cluster_proc_list & this) {
|
---|
[a7504db5] | 270 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 271 |
|
---|
| 272 | // Start by locking the global RWlock so that we know no-one is
|
---|
| 273 | // adding/removing processors while we mess with the idle lock
|
---|
| 274 | ready_schedule_lock();
|
---|
| 275 |
|
---|
[a633f6f] | 276 | lock( this.lock __cfaabi_dbg_ctx2 );
|
---|
[a7504db5] | 277 |
|
---|
| 278 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 279 | }
|
---|
| 280 |
|
---|
[5f5a729] | 281 | static inline bool try_lock(__cluster_proc_list & this) {
|
---|
| 282 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 283 |
|
---|
| 284 | // Start by locking the global RWlock so that we know no-one is
|
---|
| 285 | // adding/removing processors while we mess with the idle lock
|
---|
| 286 | ready_schedule_lock();
|
---|
| 287 |
|
---|
[a633f6f] | 288 | if(try_lock( this.lock __cfaabi_dbg_ctx2 )) {
|
---|
[5f5a729] | 289 | // success
|
---|
| 290 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 291 | return true;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | // failed to lock
|
---|
| 295 | ready_schedule_unlock();
|
---|
| 296 |
|
---|
| 297 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 298 | return false;
|
---|
| 299 | }
|
---|
| 300 |
|
---|
[6a9b12b] | 301 | static inline void unlock(__cluster_proc_list & this) {
|
---|
[a7504db5] | 302 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 303 |
|
---|
[a633f6f] | 304 | unlock(this.lock);
|
---|
[a7504db5] | 305 |
|
---|
| 306 | // Release the global lock, which we acquired when locking
|
---|
| 307 | ready_schedule_unlock();
|
---|
| 308 |
|
---|
| 309 | /* paranoid */ verify( ! __preemption_enabled() );
|
---|
| 310 | }
|
---|
| 311 |
|
---|
[b798713] | 312 | //=======================================================================
|
---|
| 313 | // Ready-Queue API
|
---|
[dca5802] | 314 | //-----------------------------------------------------------------------
|
---|
| 315 | // push thread onto a ready queue for a cluster
|
---|
| 316 | // returns true if the list was previously empty, false otherwise
|
---|
[24e321c] | 317 | __attribute__((hot)) void push(struct cluster * cltr, struct thread$ * thrd, unpark_hint hint);
|
---|
[dca5802] | 318 |
|
---|
| 319 | //-----------------------------------------------------------------------
|
---|
[fc59df78] | 320 | // pop thread from the local queues of a cluster
|
---|
[dca5802] | 321 | // returns 0p if empty
|
---|
[1eb239e4] | 322 | // May return 0p spuriously
|
---|
[e84ab3d] | 323 | __attribute__((hot)) struct thread$ * pop_fast(struct cluster * cltr);
|
---|
[dca5802] | 324 |
|
---|
[1eb239e4] | 325 | //-----------------------------------------------------------------------
|
---|
[fc59df78] | 326 | // pop thread from any ready queue of a cluster
|
---|
[1eb239e4] | 327 | // returns 0p if empty
|
---|
[fc59df78] | 328 | // May return 0p spuriously
|
---|
[e84ab3d] | 329 | __attribute__((hot)) struct thread$ * pop_slow(struct cluster * cltr);
|
---|
[1eb239e4] | 330 |
|
---|
[fc59df78] | 331 | //-----------------------------------------------------------------------
|
---|
| 332 | // search all ready queues of a cluster for any thread
|
---|
| 333 | // returns 0p if empty
|
---|
| 334 | // guaranteed to find any threads added before this call
|
---|
[e84ab3d] | 335 | __attribute__((hot)) struct thread$ * pop_search(struct cluster * cltr);
|
---|
[fc59df78] | 336 |
|
---|
[24e321c] | 337 | //-----------------------------------------------------------------------
|
---|
| 338 | // get preferred ready for new thread
|
---|
| 339 | unsigned ready_queue_new_preferred();
|
---|
| 340 |
|
---|
[dca5802] | 341 | //-----------------------------------------------------------------------
|
---|
| 342 | // Increase the width of the ready queue (number of lanes) by 4
|
---|
[a017ee7] | 343 | void ready_queue_grow (struct cluster * cltr);
|
---|
[dca5802] | 344 |
|
---|
| 345 | //-----------------------------------------------------------------------
|
---|
| 346 | // Decrease the width of the ready queue (number of lanes) by 4
|
---|
[a017ee7] | 347 | void ready_queue_shrink(struct cluster * cltr);
|
---|
[b798713] | 348 |
|
---|
[de94a60] | 349 |
|
---|
[75f3522] | 350 | // Local Variables: //
|
---|
| 351 | // mode: c //
|
---|
| 352 | // tab-width: 4 //
|
---|
[4aa2fb2] | 353 | // End: //
|
---|