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