[ab1b971] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2021 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 | // |
---|
| 7 | // locks.hfa -- PUBLIC |
---|
| 8 | // Runtime locks that used with the runtime thread system. |
---|
| 9 | // |
---|
| 10 | // Author : Colby Alexander Parsons |
---|
| 11 | // Created On : Thu Jan 21 19:46:50 2021 |
---|
| 12 | // Last Modified By : |
---|
| 13 | // Last Modified On : |
---|
| 14 | // Update Count : |
---|
| 15 | // |
---|
| 16 | |
---|
[f4e35326] | 17 | #pragma once |
---|
| 18 | |
---|
[848439f] | 19 | #include <stdbool.h> |
---|
[5a46e09] | 20 | #include <stdio.h> |
---|
[848439f] | 21 | |
---|
[ab1b971] | 22 | #include "bits/weakso_locks.hfa" |
---|
[88ac843e] | 23 | #include "containers/lockfree.hfa" |
---|
[82f4063] | 24 | #include "containers/list.hfa" |
---|
[f4ec5e45] | 25 | |
---|
[07033ce] | 26 | #include "limits.hfa" |
---|
[f4ec5e45] | 27 | #include "thread.hfa" |
---|
[848439f] | 28 | |
---|
| 29 | #include "time_t.hfa" |
---|
| 30 | #include "time.hfa" |
---|
| 31 | |
---|
[b77f0e1] | 32 | #include <fstream.hfa> |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | // futex headers |
---|
| 36 | #include <linux/futex.h> /* Definition of FUTEX_* constants */ |
---|
| 37 | #include <sys/syscall.h> /* Definition of SYS_* constants */ |
---|
| 38 | #include <unistd.h> |
---|
| 39 | |
---|
[0cee082] | 40 | // C_TODO: cleanup this and locks.cfa |
---|
| 41 | // - appropriate separation of interface and impl |
---|
| 42 | // - clean up unused/unneeded locks |
---|
| 43 | // - change messy big blocking lock from inheritance to composition to remove need for flags |
---|
[1ab773e0] | 44 | |
---|
[f4ec5e45] | 45 | //----------------------------------------------------------------------------- |
---|
| 46 | // Semaphore |
---|
| 47 | struct semaphore { |
---|
| 48 | __spinlock_t lock; |
---|
| 49 | int count; |
---|
[e84ab3d] | 50 | __queue_t(thread$) waiting; |
---|
[f4ec5e45] | 51 | }; |
---|
| 52 | |
---|
| 53 | void ?{}(semaphore & this, int count = 1); |
---|
| 54 | void ^?{}(semaphore & this); |
---|
| 55 | bool P (semaphore & this); |
---|
| 56 | bool V (semaphore & this); |
---|
| 57 | bool V (semaphore & this, unsigned count); |
---|
[e84ab3d] | 58 | thread$ * V (semaphore & this, bool ); |
---|
[f4ec5e45] | 59 | |
---|
[ab1b971] | 60 | //---------- |
---|
| 61 | struct single_acquisition_lock { |
---|
| 62 | inline blocking_lock; |
---|
| 63 | }; |
---|
| 64 | |
---|
| 65 | static inline void ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };} |
---|
| 66 | static inline void ^?{}( single_acquisition_lock & this ) {} |
---|
[22b7579] | 67 | static inline void lock ( single_acquisition_lock & this ) { lock ( (blocking_lock &)this ); } |
---|
| 68 | static inline bool try_lock ( single_acquisition_lock & this ) { return try_lock( (blocking_lock &)this ); } |
---|
| 69 | static inline void unlock ( single_acquisition_lock & this ) { unlock ( (blocking_lock &)this ); } |
---|
| 70 | static inline size_t on_wait ( single_acquisition_lock & this ) { return on_wait ( (blocking_lock &)this ); } |
---|
| 71 | static inline void on_wakeup( single_acquisition_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); } |
---|
[e84ab3d] | 72 | static inline void on_notify( single_acquisition_lock & this, struct thread$ * t ) { on_notify( (blocking_lock &)this, t ); } |
---|
[ab1b971] | 73 | |
---|
| 74 | //---------- |
---|
| 75 | struct owner_lock { |
---|
| 76 | inline blocking_lock; |
---|
| 77 | }; |
---|
| 78 | |
---|
| 79 | static inline void ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };} |
---|
| 80 | static inline void ^?{}( owner_lock & this ) {} |
---|
[f19497c] | 81 | static inline void lock ( owner_lock & this ) { lock ( (blocking_lock &)this ); } |
---|
[d27b6be] | 82 | static inline bool try_lock ( owner_lock & this ) { return try_lock( (blocking_lock &)this ); } |
---|
[f19497c] | 83 | static inline void unlock ( owner_lock & this ) { unlock ( (blocking_lock &)this ); } |
---|
[22b7579] | 84 | static inline size_t on_wait ( owner_lock & this ) { return on_wait ( (blocking_lock &)this ); } |
---|
| 85 | static inline void on_wakeup( owner_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); } |
---|
[e84ab3d] | 86 | static inline void on_notify( owner_lock & this, struct thread$ * t ) { on_notify( (blocking_lock &)this, t ); } |
---|
[ab1b971] | 87 | |
---|
[7f958c4] | 88 | //----------------------------------------------------------------------------- |
---|
| 89 | // MCS Lock |
---|
[f4ec5e45] | 90 | struct mcs_node { |
---|
| 91 | mcs_node * volatile next; |
---|
| 92 | single_sem sem; |
---|
| 93 | }; |
---|
| 94 | |
---|
[8f5576d5] | 95 | static inline void ?{}(mcs_node & this) { this.next = 0p; } |
---|
[f4ec5e45] | 96 | |
---|
| 97 | static inline mcs_node * volatile & ?`next ( mcs_node * node ) { |
---|
| 98 | return node->next; |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | struct mcs_lock { |
---|
| 102 | mcs_queue(mcs_node) queue; |
---|
| 103 | }; |
---|
| 104 | |
---|
| 105 | static inline void lock(mcs_lock & l, mcs_node & n) { |
---|
| 106 | if(push(l.queue, &n)) |
---|
| 107 | wait(n.sem); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | static inline void unlock(mcs_lock & l, mcs_node & n) { |
---|
| 111 | mcs_node * next = advance(l.queue, &n); |
---|
| 112 | if(next) post(next->sem); |
---|
| 113 | } |
---|
| 114 | |
---|
[f835806] | 115 | //----------------------------------------------------------------------------- |
---|
| 116 | // MCS Spin Lock |
---|
| 117 | // - No recursive acquisition |
---|
| 118 | // - Needs to be released by owner |
---|
| 119 | |
---|
| 120 | struct mcs_spin_node { |
---|
| 121 | mcs_spin_node * volatile next; |
---|
[db7a3ad] | 122 | volatile bool locked; |
---|
[f835806] | 123 | }; |
---|
| 124 | |
---|
| 125 | struct mcs_spin_queue { |
---|
| 126 | mcs_spin_node * volatile tail; |
---|
| 127 | }; |
---|
| 128 | |
---|
| 129 | static inline void ?{}(mcs_spin_node & this) { this.next = 0p; this.locked = true; } |
---|
| 130 | |
---|
| 131 | static inline mcs_spin_node * volatile & ?`next ( mcs_spin_node * node ) { |
---|
| 132 | return node->next; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | struct mcs_spin_lock { |
---|
| 136 | mcs_spin_queue queue; |
---|
| 137 | }; |
---|
| 138 | |
---|
| 139 | static inline void lock(mcs_spin_lock & l, mcs_spin_node & n) { |
---|
| 140 | mcs_spin_node * prev = __atomic_exchange_n(&l.queue.tail, &n, __ATOMIC_SEQ_CST); |
---|
[76a798d] | 141 | n.locked = true; |
---|
[9e3d123] | 142 | if(prev == 0p) return; |
---|
| 143 | prev->next = &n; |
---|
[76a798d] | 144 | while(__atomic_load_n(&n.locked, __ATOMIC_RELAXED)) Pause(); |
---|
[f835806] | 145 | } |
---|
| 146 | |
---|
| 147 | static inline void unlock(mcs_spin_lock & l, mcs_spin_node & n) { |
---|
| 148 | mcs_spin_node * n_ptr = &n; |
---|
[9e3d123] | 149 | if (__atomic_compare_exchange_n(&l.queue.tail, &n_ptr, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) return; |
---|
[76a798d] | 150 | while (__atomic_load_n(&n.next, __ATOMIC_RELAXED) == 0p) {} |
---|
[9e3d123] | 151 | n.next->locked = false; |
---|
[f835806] | 152 | } |
---|
| 153 | |
---|
[b77f0e1] | 154 | //----------------------------------------------------------------------------- |
---|
| 155 | // futex_mutex |
---|
| 156 | |
---|
| 157 | // - No cond var support |
---|
| 158 | // - Kernel thd blocking alternative to the spinlock |
---|
| 159 | // - No ownership (will deadlock on reacq) |
---|
| 160 | struct futex_mutex { |
---|
| 161 | // lock state any state other than UNLOCKED is locked |
---|
| 162 | // enum LockState { UNLOCKED = 0, UNCONTENDED = 1, CONTENDED = 2 }; |
---|
| 163 | |
---|
| 164 | // stores a lock state |
---|
| 165 | int val; |
---|
| 166 | }; |
---|
| 167 | |
---|
| 168 | // to use for FUTEX_WAKE and FUTEX_WAIT (other futex calls will need more params) |
---|
[7d9598d] | 169 | static inline int futex(int *uaddr, int futex_op, int val) { |
---|
[b77f0e1] | 170 | return syscall(SYS_futex, uaddr, futex_op, val, NULL, NULL, 0); |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | static inline void ?{}( futex_mutex & this ) with(this) { val = 0; } |
---|
| 174 | |
---|
| 175 | static inline bool internal_try_lock(futex_mutex & this, int & compare_val) with(this) { |
---|
| 176 | return __atomic_compare_exchange_n((int*)&val, (int*)&compare_val, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_ACQUIRE); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | static inline int internal_exchange(futex_mutex & this) with(this) { |
---|
| 180 | return __atomic_exchange_n((int*)&val, 2, __ATOMIC_ACQUIRE); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | // if this is called recursively IT WILL DEADLOCK!!!!! |
---|
| 184 | static inline void lock(futex_mutex & this) with(this) { |
---|
| 185 | int state; |
---|
| 186 | |
---|
| 187 | |
---|
[7d9598d] | 188 | // // linear backoff omitted for now |
---|
| 189 | // for( int spin = 4; spin < 1024; spin += spin) { |
---|
| 190 | // state = 0; |
---|
| 191 | // // if unlocked, lock and return |
---|
| 192 | // if (internal_try_lock(this, state)) return; |
---|
| 193 | // if (2 == state) break; |
---|
| 194 | // for (int i = 0; i < spin; i++) Pause(); |
---|
| 195 | // } |
---|
| 196 | |
---|
| 197 | // no contention try to acquire |
---|
| 198 | if (internal_try_lock(this, state)) return; |
---|
[b77f0e1] | 199 | |
---|
| 200 | // if not in contended state, set to be in contended state |
---|
| 201 | if (state != 2) state = internal_exchange(this); |
---|
| 202 | |
---|
| 203 | // block and spin until we win the lock |
---|
| 204 | while (state != 0) { |
---|
| 205 | futex((int*)&val, FUTEX_WAIT, 2); // if val is not 2 this returns with EWOULDBLOCK |
---|
| 206 | state = internal_exchange(this); |
---|
| 207 | } |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | static inline void unlock(futex_mutex & this) with(this) { |
---|
| 211 | // if uncontended do atomice unlock and then return |
---|
| 212 | if (__atomic_fetch_sub(&val, 1, __ATOMIC_RELEASE) == 1) return; // TODO: try acq/rel |
---|
| 213 | |
---|
| 214 | // otherwise threads are blocked so we must wake one |
---|
| 215 | __atomic_store_n((int *)&val, 0, __ATOMIC_RELEASE); |
---|
| 216 | futex((int *)&val, FUTEX_WAKE, 1); |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | static inline void on_notify( futex_mutex & f, thread$ * t){ unpark(t); } |
---|
| 220 | static inline size_t on_wait( futex_mutex & f ) {unlock(f); return 0;} |
---|
| 221 | |
---|
| 222 | // to set recursion count after getting signalled; |
---|
| 223 | static inline void on_wakeup( futex_mutex & f, size_t recursion ) {} |
---|
| 224 | |
---|
[f835806] | 225 | //----------------------------------------------------------------------------- |
---|
| 226 | // CLH Spinlock |
---|
| 227 | // - No recursive acquisition |
---|
| 228 | // - Needs to be released by owner |
---|
| 229 | |
---|
| 230 | struct clh_lock { |
---|
| 231 | volatile bool * volatile tail; |
---|
[0348fd8] | 232 | volatile bool * volatile head; |
---|
[f835806] | 233 | }; |
---|
| 234 | |
---|
| 235 | static inline void ?{}( clh_lock & this ) { this.tail = malloc(); *this.tail = true; } |
---|
| 236 | static inline void ^?{}( clh_lock & this ) { free(this.tail); } |
---|
| 237 | |
---|
| 238 | static inline void lock(clh_lock & l) { |
---|
| 239 | thread$ * curr_thd = active_thread(); |
---|
| 240 | *(curr_thd->clh_node) = false; |
---|
| 241 | volatile bool * prev = __atomic_exchange_n((bool **)(&l.tail), (bool *)(curr_thd->clh_node), __ATOMIC_SEQ_CST); |
---|
[0348fd8] | 242 | while(!__atomic_load_n(prev, __ATOMIC_SEQ_CST)) Pause(); |
---|
| 243 | __atomic_store_n((bool **)(&l.head), (bool *)curr_thd->clh_node, __ATOMIC_SEQ_CST); |
---|
| 244 | curr_thd->clh_node = prev; |
---|
[f835806] | 245 | } |
---|
| 246 | |
---|
| 247 | static inline void unlock(clh_lock & l) { |
---|
[0348fd8] | 248 | __atomic_store_n((bool *)(l.head), true, __ATOMIC_SEQ_CST); |
---|
[f835806] | 249 | } |
---|
| 250 | |
---|
[b77f0e1] | 251 | static inline void on_notify(clh_lock & this, struct thread$ * t ) { unpark(t); } |
---|
| 252 | static inline size_t on_wait(clh_lock & this) { unlock(this); return 0; } |
---|
[0cee082] | 253 | static inline void on_wakeup(clh_lock & this, size_t recursion ) { lock(this); } |
---|
[b77f0e1] | 254 | |
---|
| 255 | |
---|
[7f958c4] | 256 | //----------------------------------------------------------------------------- |
---|
[0cee082] | 257 | // Exponential backoff then block lock |
---|
| 258 | struct exp_backoff_then_block_lock { |
---|
[5a46e09] | 259 | // Spin lock used for mutual exclusion |
---|
| 260 | __spinlock_t spinlock; |
---|
| 261 | |
---|
| 262 | // List of blocked threads |
---|
[e84ab3d] | 263 | dlist( thread$ ) blocked_threads; |
---|
[5a46e09] | 264 | |
---|
| 265 | // Used for comparing and exchanging |
---|
| 266 | volatile size_t lock_value; |
---|
| 267 | }; |
---|
| 268 | |
---|
[0cee082] | 269 | static inline void ?{}( exp_backoff_then_block_lock & this ) { |
---|
[5a46e09] | 270 | this.spinlock{}; |
---|
| 271 | this.blocked_threads{}; |
---|
| 272 | this.lock_value = 0; |
---|
| 273 | } |
---|
[0cee082] | 274 | static inline void ^?{}( exp_backoff_then_block_lock & this ) {} |
---|
| 275 | // static inline void ?{}( exp_backoff_then_block_lock & this, exp_backoff_then_block_lock this2 ) = void; |
---|
| 276 | // static inline void ?=?( exp_backoff_then_block_lock & this, exp_backoff_then_block_lock this2 ) = void; |
---|
[5a46e09] | 277 | |
---|
[0cee082] | 278 | static inline bool internal_try_lock(exp_backoff_then_block_lock & this, size_t & compare_val) with(this) { |
---|
[5a46e09] | 279 | if (__atomic_compare_exchange_n(&lock_value, &compare_val, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) { |
---|
| 280 | return true; |
---|
| 281 | } |
---|
| 282 | return false; |
---|
| 283 | } |
---|
| 284 | |
---|
[0cee082] | 285 | static inline bool try_lock(exp_backoff_then_block_lock & this) { size_t compare_val = 0; return internal_try_lock(this, compare_val); } |
---|
[5a46e09] | 286 | |
---|
[0cee082] | 287 | static inline bool try_lock_contention(exp_backoff_then_block_lock & this) with(this) { |
---|
[5a46e09] | 288 | if (__atomic_exchange_n(&lock_value, 2, __ATOMIC_ACQUIRE) == 0) { |
---|
| 289 | return true; |
---|
| 290 | } |
---|
| 291 | return false; |
---|
| 292 | } |
---|
| 293 | |
---|
[0cee082] | 294 | static inline bool block(exp_backoff_then_block_lock & this) with(this) { |
---|
[7d9598d] | 295 | lock( spinlock __cfaabi_dbg_ctx2 ); // TODO change to lockfree queue (MPSC) |
---|
[5a46e09] | 296 | if (lock_value != 2) { |
---|
| 297 | unlock( spinlock ); |
---|
| 298 | return true; |
---|
| 299 | } |
---|
| 300 | insert_last( blocked_threads, *active_thread() ); |
---|
| 301 | unlock( spinlock ); |
---|
| 302 | park( ); |
---|
| 303 | return true; |
---|
| 304 | } |
---|
| 305 | |
---|
[0cee082] | 306 | static inline void lock(exp_backoff_then_block_lock & this) with(this) { |
---|
[5a46e09] | 307 | size_t compare_val = 0; |
---|
[b77f0e1] | 308 | int spin = 4; |
---|
[5a46e09] | 309 | // linear backoff |
---|
| 310 | for( ;; ) { |
---|
| 311 | compare_val = 0; |
---|
[0d4f954] | 312 | if (internal_try_lock(this, compare_val)) return; |
---|
[5a46e09] | 313 | if (2 == compare_val) break; |
---|
| 314 | for (int i = 0; i < spin; i++) Pause(); |
---|
[b77f0e1] | 315 | if (spin >= 1024) break; |
---|
[5a46e09] | 316 | spin += spin; |
---|
| 317 | } |
---|
| 318 | |
---|
[0d4f954] | 319 | if(2 != compare_val && try_lock_contention(this)) return; |
---|
[b7763da] | 320 | // block until signalled |
---|
[0d4f954] | 321 | while (block(this)) if(try_lock_contention(this)) return; |
---|
[b7763da] | 322 | } |
---|
| 323 | |
---|
[0cee082] | 324 | static inline void unlock(exp_backoff_then_block_lock & this) with(this) { |
---|
[5a46e09] | 325 | if (__atomic_exchange_n(&lock_value, 0, __ATOMIC_RELEASE) == 1) return; |
---|
| 326 | lock( spinlock __cfaabi_dbg_ctx2 ); |
---|
[e84ab3d] | 327 | thread$ * t = &try_pop_front( blocked_threads ); |
---|
[5a46e09] | 328 | unlock( spinlock ); |
---|
| 329 | unpark( t ); |
---|
| 330 | } |
---|
| 331 | |
---|
[0cee082] | 332 | static inline void on_notify(exp_backoff_then_block_lock & this, struct thread$ * t ) { unpark(t); } |
---|
| 333 | static inline size_t on_wait(exp_backoff_then_block_lock & this) { unlock(this); return 0; } |
---|
| 334 | static inline void on_wakeup(exp_backoff_then_block_lock & this, size_t recursion ) { lock(this); } |
---|
[5a46e09] | 335 | |
---|
[7f958c4] | 336 | //----------------------------------------------------------------------------- |
---|
| 337 | // Fast Block Lock |
---|
| 338 | |
---|
[f835806] | 339 | // minimal blocking lock |
---|
[7f958c4] | 340 | // - No reacquire for cond var |
---|
| 341 | // - No recursive acquisition |
---|
| 342 | // - No ownership |
---|
| 343 | struct fast_block_lock { |
---|
| 344 | // List of blocked threads |
---|
| 345 | dlist( thread$ ) blocked_threads; |
---|
| 346 | |
---|
[f835806] | 347 | // Spin lock used for mutual exclusion |
---|
| 348 | __spinlock_t lock; |
---|
| 349 | |
---|
| 350 | // flag showing if lock is held |
---|
[7f958c4] | 351 | bool held:1; |
---|
| 352 | }; |
---|
| 353 | |
---|
| 354 | static inline void ?{}( fast_block_lock & this ) with(this) { |
---|
| 355 | lock{}; |
---|
| 356 | blocked_threads{}; |
---|
| 357 | held = false; |
---|
| 358 | } |
---|
| 359 | static inline void ^?{}( fast_block_lock & this ) {} |
---|
| 360 | static inline void ?{}( fast_block_lock & this, fast_block_lock this2 ) = void; |
---|
| 361 | static inline void ?=?( fast_block_lock & this, fast_block_lock this2 ) = void; |
---|
| 362 | |
---|
| 363 | // if this is called recursively IT WILL DEADLOCK!!!!! |
---|
| 364 | static inline void lock(fast_block_lock & this) with(this) { |
---|
| 365 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
[b77f0e1] | 366 | if ( held ) { |
---|
[7f958c4] | 367 | insert_last( blocked_threads, *active_thread() ); |
---|
| 368 | unlock( lock ); |
---|
| 369 | park( ); |
---|
| 370 | return; |
---|
| 371 | } |
---|
| 372 | held = true; |
---|
| 373 | unlock( lock ); |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | static inline void unlock(fast_block_lock & this) with(this) { |
---|
| 377 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 378 | /* paranoid */ verifyf( held != false, "Attempt to release lock %p that isn't held", &this ); |
---|
| 379 | thread$ * t = &try_pop_front( blocked_threads ); |
---|
| 380 | held = ( t ? true : false ); |
---|
| 381 | unpark( t ); |
---|
| 382 | unlock( lock ); |
---|
| 383 | } |
---|
| 384 | |
---|
[b77f0e1] | 385 | static inline void on_notify(fast_block_lock & this, struct thread$ * t ) with(this) { |
---|
[0cee082] | 386 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 387 | insert_last( blocked_threads, *t ); |
---|
| 388 | unlock( lock ); |
---|
[b77f0e1] | 389 | } |
---|
[7f958c4] | 390 | static inline size_t on_wait(fast_block_lock & this) { unlock(this); return 0; } |
---|
| 391 | static inline void on_wakeup(fast_block_lock & this, size_t recursion ) { } |
---|
| 392 | |
---|
[f835806] | 393 | //----------------------------------------------------------------------------- |
---|
| 394 | // simple_owner_lock |
---|
| 395 | |
---|
| 396 | // pthread owner lock |
---|
| 397 | // - reacquire for cond var |
---|
| 398 | // - recursive acquisition |
---|
| 399 | // - ownership |
---|
| 400 | struct simple_owner_lock { |
---|
| 401 | // List of blocked threads |
---|
| 402 | dlist( thread$ ) blocked_threads; |
---|
| 403 | |
---|
| 404 | // Spin lock used for mutual exclusion |
---|
| 405 | __spinlock_t lock; |
---|
| 406 | |
---|
| 407 | // owner showing if lock is held |
---|
| 408 | struct thread$ * owner; |
---|
| 409 | |
---|
| 410 | size_t recursion_count; |
---|
| 411 | }; |
---|
| 412 | |
---|
| 413 | static inline void ?{}( simple_owner_lock & this ) with(this) { |
---|
| 414 | lock{}; |
---|
| 415 | blocked_threads{}; |
---|
| 416 | owner = 0p; |
---|
| 417 | recursion_count = 0; |
---|
| 418 | } |
---|
| 419 | static inline void ^?{}( simple_owner_lock & this ) {} |
---|
| 420 | static inline void ?{}( simple_owner_lock & this, simple_owner_lock this2 ) = void; |
---|
| 421 | static inline void ?=?( simple_owner_lock & this, simple_owner_lock this2 ) = void; |
---|
| 422 | |
---|
[ae06e0b] | 423 | static inline void lock(simple_owner_lock & this) with(this) { |
---|
| 424 | if (owner == active_thread()) { |
---|
| 425 | recursion_count++; |
---|
| 426 | return; |
---|
| 427 | } |
---|
| 428 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 429 | |
---|
| 430 | if (owner != 0p) { |
---|
| 431 | insert_last( blocked_threads, *active_thread() ); |
---|
| 432 | unlock( lock ); |
---|
| 433 | park( ); |
---|
| 434 | return; |
---|
| 435 | } |
---|
| 436 | owner = active_thread(); |
---|
| 437 | recursion_count = 1; |
---|
| 438 | unlock( lock ); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | // TODO: fix duplicate def issue and bring this back |
---|
| 442 | // void pop_and_set_new_owner( simple_owner_lock & this ) with( this ) { |
---|
| 443 | // thread$ * t = &try_pop_front( blocked_threads ); |
---|
| 444 | // owner = t; |
---|
| 445 | // recursion_count = ( t ? 1 : 0 ); |
---|
| 446 | // unpark( t ); |
---|
| 447 | // } |
---|
| 448 | |
---|
| 449 | static inline void unlock(simple_owner_lock & this) with(this) { |
---|
| 450 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 451 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this ); |
---|
| 452 | /* paranoid */ verifyf( owner == active_thread(), "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this ); |
---|
| 453 | // if recursion count is zero release lock and set new owner if one is waiting |
---|
| 454 | recursion_count--; |
---|
| 455 | if ( recursion_count == 0 ) { |
---|
| 456 | // pop_and_set_new_owner( this ); |
---|
| 457 | thread$ * t = &try_pop_front( blocked_threads ); |
---|
| 458 | owner = t; |
---|
| 459 | recursion_count = ( t ? 1 : 0 ); |
---|
| 460 | unpark( t ); |
---|
| 461 | } |
---|
| 462 | unlock( lock ); |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | static inline void on_notify(simple_owner_lock & this, struct thread$ * t ) with(this) { |
---|
| 466 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 467 | // lock held |
---|
| 468 | if ( owner != 0p ) { |
---|
| 469 | insert_last( blocked_threads, *t ); |
---|
| 470 | } |
---|
| 471 | // lock not held |
---|
| 472 | else { |
---|
| 473 | owner = t; |
---|
| 474 | recursion_count = 1; |
---|
| 475 | unpark( t ); |
---|
| 476 | } |
---|
[b77f0e1] | 477 | unlock( lock ); |
---|
[ae06e0b] | 478 | } |
---|
| 479 | |
---|
[88ac843e] | 480 | static inline size_t on_wait(simple_owner_lock & this) with(this) { |
---|
[ae06e0b] | 481 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 482 | /* paranoid */ verifyf( owner != 0p, "Attempt to release lock %p that isn't held", &this ); |
---|
| 483 | /* paranoid */ verifyf( owner == active_thread(), "Thread %p other than the owner %p attempted to release owner lock %p", owner, active_thread(), &this ); |
---|
| 484 | |
---|
| 485 | size_t ret = recursion_count; |
---|
| 486 | |
---|
| 487 | // pop_and_set_new_owner( this ); |
---|
| 488 | |
---|
| 489 | thread$ * t = &try_pop_front( blocked_threads ); |
---|
| 490 | owner = t; |
---|
| 491 | recursion_count = ( t ? 1 : 0 ); |
---|
| 492 | unpark( t ); |
---|
| 493 | |
---|
| 494 | unlock( lock ); |
---|
| 495 | return ret; |
---|
| 496 | } |
---|
| 497 | |
---|
| 498 | static inline void on_wakeup(simple_owner_lock & this, size_t recursion ) with(this) { recursion_count = recursion; } |
---|
| 499 | |
---|
[f835806] | 500 | //----------------------------------------------------------------------------- |
---|
| 501 | // Spin Queue Lock |
---|
| 502 | |
---|
| 503 | // - No reacquire for cond var |
---|
| 504 | // - No recursive acquisition |
---|
| 505 | // - No ownership |
---|
| 506 | // - spin lock with no locking/atomics in unlock |
---|
| 507 | struct spin_queue_lock { |
---|
| 508 | // Spin lock used for mutual exclusion |
---|
| 509 | mcs_spin_lock lock; |
---|
| 510 | |
---|
| 511 | // flag showing if lock is held |
---|
[db7a3ad] | 512 | volatile bool held; |
---|
[f835806] | 513 | }; |
---|
| 514 | |
---|
| 515 | static inline void ?{}( spin_queue_lock & this ) with(this) { |
---|
| 516 | lock{}; |
---|
| 517 | held = false; |
---|
| 518 | } |
---|
| 519 | static inline void ^?{}( spin_queue_lock & this ) {} |
---|
| 520 | static inline void ?{}( spin_queue_lock & this, spin_queue_lock this2 ) = void; |
---|
| 521 | static inline void ?=?( spin_queue_lock & this, spin_queue_lock this2 ) = void; |
---|
| 522 | |
---|
[378de69] | 523 | // if this is called recursively IT WILL DEADLOCK! |
---|
[f835806] | 524 | static inline void lock(spin_queue_lock & this) with(this) { |
---|
| 525 | mcs_spin_node node; |
---|
| 526 | lock( lock, node ); |
---|
[df932552] | 527 | while(__atomic_load_n(&held, __ATOMIC_SEQ_CST)) Pause(); |
---|
| 528 | __atomic_store_n(&held, true, __ATOMIC_SEQ_CST); |
---|
[f835806] | 529 | unlock( lock, node ); |
---|
| 530 | } |
---|
| 531 | |
---|
| 532 | static inline void unlock(spin_queue_lock & this) with(this) { |
---|
[2ed32fa7] | 533 | __atomic_store_n(&held, false, __ATOMIC_RELEASE); |
---|
[f835806] | 534 | } |
---|
| 535 | |
---|
[b77f0e1] | 536 | static inline void on_notify(spin_queue_lock & this, struct thread$ * t ) { |
---|
| 537 | unpark(t); |
---|
| 538 | } |
---|
[f835806] | 539 | static inline size_t on_wait(spin_queue_lock & this) { unlock(this); return 0; } |
---|
[0cee082] | 540 | static inline void on_wakeup(spin_queue_lock & this, size_t recursion ) { lock(this); } |
---|
[f835806] | 541 | |
---|
| 542 | |
---|
| 543 | //----------------------------------------------------------------------------- |
---|
| 544 | // MCS Block Spin Lock |
---|
| 545 | |
---|
| 546 | // - No reacquire for cond var |
---|
| 547 | // - No recursive acquisition |
---|
| 548 | // - No ownership |
---|
| 549 | // - Blocks but first node spins (like spin queue but blocking for not first thd) |
---|
| 550 | struct mcs_block_spin_lock { |
---|
| 551 | // Spin lock used for mutual exclusion |
---|
| 552 | mcs_lock lock; |
---|
| 553 | |
---|
| 554 | // flag showing if lock is held |
---|
[db7a3ad] | 555 | volatile bool held; |
---|
[f835806] | 556 | }; |
---|
| 557 | |
---|
| 558 | static inline void ?{}( mcs_block_spin_lock & this ) with(this) { |
---|
| 559 | lock{}; |
---|
| 560 | held = false; |
---|
| 561 | } |
---|
| 562 | static inline void ^?{}( mcs_block_spin_lock & this ) {} |
---|
| 563 | static inline void ?{}( mcs_block_spin_lock & this, mcs_block_spin_lock this2 ) = void; |
---|
| 564 | static inline void ?=?( mcs_block_spin_lock & this, mcs_block_spin_lock this2 ) = void; |
---|
| 565 | |
---|
| 566 | // if this is called recursively IT WILL DEADLOCK!!!!! |
---|
| 567 | static inline void lock(mcs_block_spin_lock & this) with(this) { |
---|
| 568 | mcs_node node; |
---|
| 569 | lock( lock, node ); |
---|
[fd365da] | 570 | while(__atomic_load_n(&held, __ATOMIC_SEQ_CST)) Pause(); |
---|
| 571 | __atomic_store_n(&held, true, __ATOMIC_SEQ_CST); |
---|
[f835806] | 572 | unlock( lock, node ); |
---|
| 573 | } |
---|
| 574 | |
---|
| 575 | static inline void unlock(mcs_block_spin_lock & this) with(this) { |
---|
[fd365da] | 576 | __atomic_store_n(&held, false, __ATOMIC_SEQ_CST); |
---|
[f835806] | 577 | } |
---|
| 578 | |
---|
| 579 | static inline void on_notify(mcs_block_spin_lock & this, struct thread$ * t ) { unpark(t); } |
---|
| 580 | static inline size_t on_wait(mcs_block_spin_lock & this) { unlock(this); return 0; } |
---|
[0cee082] | 581 | static inline void on_wakeup(mcs_block_spin_lock & this, size_t recursion ) {lock(this); } |
---|
[f835806] | 582 | |
---|
| 583 | //----------------------------------------------------------------------------- |
---|
| 584 | // Block Spin Lock |
---|
| 585 | |
---|
| 586 | // - No reacquire for cond var |
---|
| 587 | // - No recursive acquisition |
---|
| 588 | // - No ownership |
---|
| 589 | // - Blocks but first node spins (like spin queue but blocking for not first thd) |
---|
| 590 | struct block_spin_lock { |
---|
| 591 | // Spin lock used for mutual exclusion |
---|
| 592 | fast_block_lock lock; |
---|
| 593 | |
---|
| 594 | // flag showing if lock is held |
---|
[db7a3ad] | 595 | volatile bool held; |
---|
[f835806] | 596 | }; |
---|
| 597 | |
---|
| 598 | static inline void ?{}( block_spin_lock & this ) with(this) { |
---|
| 599 | lock{}; |
---|
| 600 | held = false; |
---|
| 601 | } |
---|
| 602 | static inline void ^?{}( block_spin_lock & this ) {} |
---|
| 603 | static inline void ?{}( block_spin_lock & this, block_spin_lock this2 ) = void; |
---|
| 604 | static inline void ?=?( block_spin_lock & this, block_spin_lock this2 ) = void; |
---|
| 605 | |
---|
| 606 | // if this is called recursively IT WILL DEADLOCK!!!!! |
---|
| 607 | static inline void lock(block_spin_lock & this) with(this) { |
---|
| 608 | lock( lock ); |
---|
[fd365da] | 609 | while(__atomic_load_n(&held, __ATOMIC_SEQ_CST)) Pause(); |
---|
| 610 | __atomic_store_n(&held, true, __ATOMIC_RELEASE); |
---|
[f835806] | 611 | unlock( lock ); |
---|
| 612 | } |
---|
| 613 | |
---|
| 614 | static inline void unlock(block_spin_lock & this) with(this) { |
---|
[fd365da] | 615 | __atomic_store_n(&held, false, __ATOMIC_RELEASE); |
---|
[f835806] | 616 | } |
---|
| 617 | |
---|
[b77f0e1] | 618 | static inline void on_notify(block_spin_lock & this, struct thread$ * t ) with(this.lock) { |
---|
| 619 | // first we acquire internal fast_block_lock |
---|
| 620 | lock( lock __cfaabi_dbg_ctx2 ); |
---|
| 621 | if ( held ) { // if internal fast_block_lock is held |
---|
| 622 | insert_last( blocked_threads, *t ); |
---|
| 623 | unlock( lock ); |
---|
| 624 | return; |
---|
| 625 | } |
---|
| 626 | // if internal fast_block_lock is not held |
---|
| 627 | held = true; |
---|
| 628 | unlock( lock ); |
---|
| 629 | |
---|
| 630 | unpark(t); |
---|
| 631 | } |
---|
[f835806] | 632 | static inline size_t on_wait(block_spin_lock & this) { unlock(this); return 0; } |
---|
[b77f0e1] | 633 | static inline void on_wakeup(block_spin_lock & this, size_t recursion ) with(this) { |
---|
| 634 | // now we acquire the entire block_spin_lock upon waking up |
---|
| 635 | while(__atomic_load_n(&held, __ATOMIC_SEQ_CST)) Pause(); |
---|
| 636 | __atomic_store_n(&held, true, __ATOMIC_RELEASE); |
---|
| 637 | unlock( lock ); // Now we release the internal fast_spin_lock |
---|
| 638 | } |
---|
[f835806] | 639 | |
---|
[ac5816d] | 640 | //----------------------------------------------------------------------------- |
---|
| 641 | // is_blocking_lock |
---|
[fd54fef] | 642 | trait is_blocking_lock(L & | sized(L)) { |
---|
[ac5816d] | 643 | // For synchronization locks to use when acquiring |
---|
[e84ab3d] | 644 | void on_notify( L &, struct thread$ * ); |
---|
[ac5816d] | 645 | |
---|
| 646 | // For synchronization locks to use when releasing |
---|
[22b7579] | 647 | size_t on_wait( L & ); |
---|
[ac5816d] | 648 | |
---|
| 649 | // to set recursion count after getting signalled; |
---|
[22b7579] | 650 | void on_wakeup( L &, size_t recursion ); |
---|
[ac5816d] | 651 | }; |
---|
[848439f] | 652 | |
---|
[ac5816d] | 653 | //----------------------------------------------------------------------------- |
---|
[82f4063] | 654 | // // info_thread |
---|
| 655 | // // the info thread is a wrapper around a thread used |
---|
| 656 | // // to store extra data for use in the condition variable |
---|
[fd54fef] | 657 | forall(L & | is_blocking_lock(L)) { |
---|
[ac5816d] | 658 | struct info_thread; |
---|
[c131a02] | 659 | |
---|
[82f4063] | 660 | // // for use by sequence |
---|
| 661 | // info_thread(L) *& Back( info_thread(L) * this ); |
---|
| 662 | // info_thread(L) *& Next( info_thread(L) * this ); |
---|
[848439f] | 663 | } |
---|
| 664 | |
---|
[ac5816d] | 665 | //----------------------------------------------------------------------------- |
---|
| 666 | // Synchronization Locks |
---|
[fd54fef] | 667 | forall(L & | is_blocking_lock(L)) { |
---|
[7f958c4] | 668 | |
---|
| 669 | //----------------------------------------------------------------------------- |
---|
| 670 | // condition_variable |
---|
| 671 | |
---|
| 672 | // The multi-tool condition variable |
---|
| 673 | // - can pass timeouts to wait for either a signal or timeout |
---|
| 674 | // - can wait without passing a lock |
---|
| 675 | // - can have waiters reacquire different locks while waiting on the same cond var |
---|
| 676 | // - has shadow queue |
---|
| 677 | // - can be signalled outside of critical sections with no locks held |
---|
[eeb5023] | 678 | struct condition_variable { |
---|
[848439f] | 679 | // Spin lock used for mutual exclusion |
---|
| 680 | __spinlock_t lock; |
---|
| 681 | |
---|
| 682 | // List of blocked threads |
---|
[82f4063] | 683 | dlist( info_thread(L) ) blocked_threads; |
---|
[848439f] | 684 | |
---|
| 685 | // Count of current blocked threads |
---|
| 686 | int count; |
---|
| 687 | }; |
---|
[e84ab3d] | 688 | |
---|
[848439f] | 689 | |
---|
[ac5816d] | 690 | void ?{}( condition_variable(L) & this ); |
---|
[848439f] | 691 | void ^?{}( condition_variable(L) & this ); |
---|
| 692 | |
---|
[eeb5023] | 693 | bool notify_one( condition_variable(L) & this ); |
---|
| 694 | bool notify_all( condition_variable(L) & this ); |
---|
[848439f] | 695 | |
---|
[eeb5023] | 696 | uintptr_t front( condition_variable(L) & this ); |
---|
[848439f] | 697 | |
---|
[ac5816d] | 698 | bool empty ( condition_variable(L) & this ); |
---|
| 699 | int counter( condition_variable(L) & this ); |
---|
[848439f] | 700 | |
---|
[eeb5023] | 701 | void wait( condition_variable(L) & this ); |
---|
| 702 | void wait( condition_variable(L) & this, uintptr_t info ); |
---|
[dff1fd1] | 703 | bool wait( condition_variable(L) & this, Duration duration ); |
---|
| 704 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration ); |
---|
[848439f] | 705 | |
---|
[eeb5023] | 706 | void wait( condition_variable(L) & this, L & l ); |
---|
| 707 | void wait( condition_variable(L) & this, L & l, uintptr_t info ); |
---|
[dff1fd1] | 708 | bool wait( condition_variable(L) & this, L & l, Duration duration ); |
---|
| 709 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration ); |
---|
[7f958c4] | 710 | |
---|
| 711 | //----------------------------------------------------------------------------- |
---|
| 712 | // fast_cond_var |
---|
| 713 | |
---|
| 714 | // The trimmed and slim condition variable |
---|
| 715 | // - no internal lock so you must hold a lock while using this cond var |
---|
| 716 | // - signalling without holding branded lock is UNSAFE! |
---|
| 717 | // - only allows usage of one lock, cond var is branded after usage |
---|
[ae06e0b] | 718 | |
---|
[7f958c4] | 719 | struct fast_cond_var { |
---|
| 720 | // List of blocked threads |
---|
| 721 | dlist( info_thread(L) ) blocked_threads; |
---|
| 722 | #ifdef __CFA_DEBUG__ |
---|
| 723 | L * lock_used; |
---|
| 724 | #endif |
---|
| 725 | }; |
---|
| 726 | |
---|
| 727 | void ?{}( fast_cond_var(L) & this ); |
---|
| 728 | void ^?{}( fast_cond_var(L) & this ); |
---|
| 729 | |
---|
| 730 | bool notify_one( fast_cond_var(L) & this ); |
---|
| 731 | bool notify_all( fast_cond_var(L) & this ); |
---|
| 732 | |
---|
| 733 | uintptr_t front( fast_cond_var(L) & this ); |
---|
| 734 | bool empty ( fast_cond_var(L) & this ); |
---|
| 735 | |
---|
| 736 | void wait( fast_cond_var(L) & this, L & l ); |
---|
| 737 | void wait( fast_cond_var(L) & this, L & l, uintptr_t info ); |
---|
[ae06e0b] | 738 | |
---|
| 739 | |
---|
| 740 | //----------------------------------------------------------------------------- |
---|
| 741 | // pthread_cond_var |
---|
| 742 | // |
---|
| 743 | // - cond var with minimal footprint |
---|
| 744 | // - supports operations needed for phthread cond |
---|
| 745 | |
---|
| 746 | struct pthread_cond_var { |
---|
| 747 | dlist( info_thread(L) ) blocked_threads; |
---|
| 748 | __spinlock_t lock; |
---|
| 749 | }; |
---|
| 750 | |
---|
| 751 | void ?{}( pthread_cond_var(L) & this ); |
---|
| 752 | void ^?{}( pthread_cond_var(L) & this ); |
---|
| 753 | |
---|
| 754 | bool notify_one( pthread_cond_var(L) & this ); |
---|
| 755 | bool notify_all( pthread_cond_var(L) & this ); |
---|
| 756 | |
---|
| 757 | uintptr_t front( pthread_cond_var(L) & this ); |
---|
| 758 | bool empty ( pthread_cond_var(L) & this ); |
---|
| 759 | |
---|
| 760 | void wait( pthread_cond_var(L) & this, L & l ); |
---|
| 761 | void wait( pthread_cond_var(L) & this, L & l, uintptr_t info ); |
---|
| 762 | bool wait( pthread_cond_var(L) & this, L & l, timespec t ); |
---|
| 763 | bool wait( pthread_cond_var(L) & this, L & l, uintptr_t info, timespec t ); |
---|
[f4ec5e45] | 764 | } |
---|