[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"
|
---|
[f4ec5e45] | 23 | #include "containers/queueLockFree.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 |
|
---|
[f4ec5e45] | 32 | //-----------------------------------------------------------------------------
|
---|
| 33 | // Semaphore
|
---|
| 34 | struct semaphore {
|
---|
| 35 | __spinlock_t lock;
|
---|
| 36 | int count;
|
---|
[e84ab3d] | 37 | __queue_t(thread$) waiting;
|
---|
[f4ec5e45] | 38 | };
|
---|
| 39 |
|
---|
| 40 | void ?{}(semaphore & this, int count = 1);
|
---|
| 41 | void ^?{}(semaphore & this);
|
---|
| 42 | bool P (semaphore & this);
|
---|
| 43 | bool V (semaphore & this);
|
---|
| 44 | bool V (semaphore & this, unsigned count);
|
---|
[e84ab3d] | 45 | thread$ * V (semaphore & this, bool );
|
---|
[f4ec5e45] | 46 |
|
---|
[ab1b971] | 47 | //----------
|
---|
| 48 | struct single_acquisition_lock {
|
---|
| 49 | inline blocking_lock;
|
---|
| 50 | };
|
---|
| 51 |
|
---|
| 52 | static inline void ?{}( single_acquisition_lock & this ) {((blocking_lock &)this){ false, false };}
|
---|
| 53 | static inline void ^?{}( single_acquisition_lock & this ) {}
|
---|
[22b7579] | 54 | static inline void lock ( single_acquisition_lock & this ) { lock ( (blocking_lock &)this ); }
|
---|
| 55 | static inline bool try_lock ( single_acquisition_lock & this ) { return try_lock( (blocking_lock &)this ); }
|
---|
| 56 | static inline void unlock ( single_acquisition_lock & this ) { unlock ( (blocking_lock &)this ); }
|
---|
| 57 | static inline size_t on_wait ( single_acquisition_lock & this ) { return on_wait ( (blocking_lock &)this ); }
|
---|
| 58 | static inline void on_wakeup( single_acquisition_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); }
|
---|
[e84ab3d] | 59 | static inline void on_notify( single_acquisition_lock & this, struct thread$ * t ) { on_notify( (blocking_lock &)this, t ); }
|
---|
[ab1b971] | 60 |
|
---|
| 61 | //----------
|
---|
| 62 | struct owner_lock {
|
---|
| 63 | inline blocking_lock;
|
---|
| 64 | };
|
---|
| 65 |
|
---|
| 66 | static inline void ?{}( owner_lock & this ) {((blocking_lock &)this){ true, true };}
|
---|
| 67 | static inline void ^?{}( owner_lock & this ) {}
|
---|
[f19497c] | 68 | static inline void lock ( owner_lock & this ) { lock ( (blocking_lock &)this ); }
|
---|
[d27b6be] | 69 | static inline bool try_lock ( owner_lock & this ) { return try_lock( (blocking_lock &)this ); }
|
---|
[f19497c] | 70 | static inline void unlock ( owner_lock & this ) { unlock ( (blocking_lock &)this ); }
|
---|
[22b7579] | 71 | static inline size_t on_wait ( owner_lock & this ) { return on_wait ( (blocking_lock &)this ); }
|
---|
| 72 | static inline void on_wakeup( owner_lock & this, size_t v ) { on_wakeup ( (blocking_lock &)this, v ); }
|
---|
[e84ab3d] | 73 | static inline void on_notify( owner_lock & this, struct thread$ * t ) { on_notify( (blocking_lock &)this, t ); }
|
---|
[ab1b971] | 74 |
|
---|
[7f958c4] | 75 | //-----------------------------------------------------------------------------
|
---|
| 76 | // MCS Lock
|
---|
[f4ec5e45] | 77 | struct mcs_node {
|
---|
| 78 | mcs_node * volatile next;
|
---|
| 79 | single_sem sem;
|
---|
| 80 | };
|
---|
| 81 |
|
---|
[8f5576d5] | 82 | static inline void ?{}(mcs_node & this) { this.next = 0p; }
|
---|
[f4ec5e45] | 83 |
|
---|
| 84 | static inline mcs_node * volatile & ?`next ( mcs_node * node ) {
|
---|
| 85 | return node->next;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | struct mcs_lock {
|
---|
| 89 | mcs_queue(mcs_node) queue;
|
---|
| 90 | };
|
---|
| 91 |
|
---|
| 92 | static inline void lock(mcs_lock & l, mcs_node & n) {
|
---|
| 93 | if(push(l.queue, &n))
|
---|
| 94 | wait(n.sem);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | static inline void unlock(mcs_lock & l, mcs_node & n) {
|
---|
| 98 | mcs_node * next = advance(l.queue, &n);
|
---|
| 99 | if(next) post(next->sem);
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[7f958c4] | 102 | //-----------------------------------------------------------------------------
|
---|
| 103 | // Linear backoff Spinlock
|
---|
[5a46e09] | 104 | struct linear_backoff_then_block_lock {
|
---|
| 105 | // Spin lock used for mutual exclusion
|
---|
| 106 | __spinlock_t spinlock;
|
---|
| 107 |
|
---|
| 108 | // Current thread owning the lock
|
---|
[e84ab3d] | 109 | struct thread$ * owner;
|
---|
[5a46e09] | 110 |
|
---|
| 111 | // List of blocked threads
|
---|
[e84ab3d] | 112 | dlist( thread$ ) blocked_threads;
|
---|
[5a46e09] | 113 |
|
---|
| 114 | // Used for comparing and exchanging
|
---|
| 115 | volatile size_t lock_value;
|
---|
| 116 |
|
---|
| 117 | // used for linear backoff spinning
|
---|
| 118 | int spin_start;
|
---|
| 119 | int spin_end;
|
---|
| 120 | int spin_count;
|
---|
| 121 |
|
---|
| 122 | // after unsuccessful linear backoff yield this many times
|
---|
| 123 | int yield_count;
|
---|
| 124 | };
|
---|
| 125 |
|
---|
| 126 | static inline void ?{}( linear_backoff_then_block_lock & this, int spin_start, int spin_end, int spin_count, int yield_count ) {
|
---|
| 127 | this.spinlock{};
|
---|
| 128 | this.blocked_threads{};
|
---|
| 129 | this.lock_value = 0;
|
---|
| 130 | this.spin_start = spin_start;
|
---|
| 131 | this.spin_end = spin_end;
|
---|
| 132 | this.spin_count = spin_count;
|
---|
| 133 | this.yield_count = yield_count;
|
---|
| 134 | }
|
---|
[55ad35c] | 135 | static inline void ?{}( linear_backoff_then_block_lock & this ) { this{4, 1024, 16, 0}; }
|
---|
[5a46e09] | 136 | static inline void ^?{}( linear_backoff_then_block_lock & this ) {}
|
---|
[eba9d27] | 137 | static inline void ?{}( linear_backoff_then_block_lock & this, linear_backoff_then_block_lock this2 ) = void;
|
---|
| 138 | static inline void ?=?( linear_backoff_then_block_lock & this, linear_backoff_then_block_lock this2 ) = void;
|
---|
[5a46e09] | 139 |
|
---|
| 140 | static inline bool internal_try_lock(linear_backoff_then_block_lock & this, size_t & compare_val) with(this) {
|
---|
| 141 | if (__atomic_compare_exchange_n(&lock_value, &compare_val, 1, false, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
|
---|
| 142 | owner = active_thread();
|
---|
| 143 | return true;
|
---|
| 144 | }
|
---|
| 145 | return false;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | static inline bool try_lock(linear_backoff_then_block_lock & this) { size_t compare_val = 0; return internal_try_lock(this, compare_val); }
|
---|
| 149 |
|
---|
| 150 | static inline bool try_lock_contention(linear_backoff_then_block_lock & this) with(this) {
|
---|
| 151 | if (__atomic_exchange_n(&lock_value, 2, __ATOMIC_ACQUIRE) == 0) {
|
---|
| 152 | owner = active_thread();
|
---|
| 153 | return true;
|
---|
| 154 | }
|
---|
| 155 | return false;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | static inline bool block(linear_backoff_then_block_lock & this) with(this) {
|
---|
| 159 | lock( spinlock __cfaabi_dbg_ctx2 );
|
---|
| 160 | if (lock_value != 2) {
|
---|
| 161 | unlock( spinlock );
|
---|
| 162 | return true;
|
---|
| 163 | }
|
---|
| 164 | insert_last( blocked_threads, *active_thread() );
|
---|
| 165 | unlock( spinlock );
|
---|
| 166 | park( );
|
---|
| 167 | return true;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[0d4f954] | 170 | static inline void lock(linear_backoff_then_block_lock & this) with(this) {
|
---|
[5a46e09] | 171 | // if owner just return
|
---|
[0d4f954] | 172 | if (active_thread() == owner) return;
|
---|
[5a46e09] | 173 | size_t compare_val = 0;
|
---|
| 174 | int spin = spin_start;
|
---|
| 175 | // linear backoff
|
---|
| 176 | for( ;; ) {
|
---|
| 177 | compare_val = 0;
|
---|
[0d4f954] | 178 | if (internal_try_lock(this, compare_val)) return;
|
---|
[5a46e09] | 179 | if (2 == compare_val) break;
|
---|
| 180 | for (int i = 0; i < spin; i++) Pause();
|
---|
| 181 | if (spin >= spin_end) break;
|
---|
| 182 | spin += spin;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[0d4f954] | 185 | if(2 != compare_val && try_lock_contention(this)) return;
|
---|
[b7763da] | 186 | // block until signalled
|
---|
[0d4f954] | 187 | while (block(this)) if(try_lock_contention(this)) return;
|
---|
[b7763da] | 188 | }
|
---|
| 189 |
|
---|
[5a46e09] | 190 | static inline void unlock(linear_backoff_then_block_lock & this) with(this) {
|
---|
| 191 | verify(lock_value > 0);
|
---|
| 192 | owner = 0p;
|
---|
| 193 | if (__atomic_exchange_n(&lock_value, 0, __ATOMIC_RELEASE) == 1) return;
|
---|
| 194 | lock( spinlock __cfaabi_dbg_ctx2 );
|
---|
[e84ab3d] | 195 | thread$ * t = &try_pop_front( blocked_threads );
|
---|
[5a46e09] | 196 | unlock( spinlock );
|
---|
| 197 | unpark( t );
|
---|
| 198 | }
|
---|
| 199 |
|
---|
[e84ab3d] | 200 | static inline void on_notify(linear_backoff_then_block_lock & this, struct thread$ * t ) { unpark(t); }
|
---|
[dcad80a] | 201 | static inline size_t on_wait(linear_backoff_then_block_lock & this) { unlock(this); return 0; }
|
---|
[bbe3719] | 202 | static inline void on_wakeup(linear_backoff_then_block_lock & this, size_t recursion ) { lock(this); }
|
---|
[5a46e09] | 203 |
|
---|
[7f958c4] | 204 | //-----------------------------------------------------------------------------
|
---|
| 205 | // Fast Block Lock
|
---|
| 206 |
|
---|
| 207 | // High efficiency minimal blocking lock
|
---|
| 208 | // - No reacquire for cond var
|
---|
| 209 | // - No recursive acquisition
|
---|
| 210 | // - No ownership
|
---|
| 211 | struct fast_block_lock {
|
---|
| 212 | // Spin lock used for mutual exclusion
|
---|
| 213 | __spinlock_t lock;
|
---|
| 214 |
|
---|
| 215 | // List of blocked threads
|
---|
| 216 | dlist( thread$ ) blocked_threads;
|
---|
| 217 |
|
---|
| 218 | bool held:1;
|
---|
| 219 | };
|
---|
| 220 |
|
---|
| 221 | static inline void ?{}( fast_block_lock & this ) with(this) {
|
---|
| 222 | lock{};
|
---|
| 223 | blocked_threads{};
|
---|
| 224 | held = false;
|
---|
| 225 | }
|
---|
| 226 | static inline void ^?{}( fast_block_lock & this ) {}
|
---|
| 227 | static inline void ?{}( fast_block_lock & this, fast_block_lock this2 ) = void;
|
---|
| 228 | static inline void ?=?( fast_block_lock & this, fast_block_lock this2 ) = void;
|
---|
| 229 |
|
---|
| 230 | // if this is called recursively IT WILL DEADLOCK!!!!!
|
---|
| 231 | static inline void lock(fast_block_lock & this) with(this) {
|
---|
| 232 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 233 | if (held) {
|
---|
| 234 | insert_last( blocked_threads, *active_thread() );
|
---|
| 235 | unlock( lock );
|
---|
| 236 | park( );
|
---|
| 237 | return;
|
---|
| 238 | }
|
---|
| 239 | held = true;
|
---|
| 240 | unlock( lock );
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | static inline void unlock(fast_block_lock & this) with(this) {
|
---|
| 244 | lock( lock __cfaabi_dbg_ctx2 );
|
---|
| 245 | /* paranoid */ verifyf( held != false, "Attempt to release lock %p that isn't held", &this );
|
---|
| 246 | thread$ * t = &try_pop_front( blocked_threads );
|
---|
| 247 | held = ( t ? true : false );
|
---|
| 248 | unpark( t );
|
---|
| 249 | unlock( lock );
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | static inline void on_notify(fast_block_lock & this, struct thread$ * t ) { unpark(t); }
|
---|
| 253 | static inline size_t on_wait(fast_block_lock & this) { unlock(this); return 0; }
|
---|
| 254 | static inline void on_wakeup(fast_block_lock & this, size_t recursion ) { }
|
---|
| 255 |
|
---|
[ac5816d] | 256 | //-----------------------------------------------------------------------------
|
---|
| 257 | // is_blocking_lock
|
---|
[fd54fef] | 258 | trait is_blocking_lock(L & | sized(L)) {
|
---|
[ac5816d] | 259 | // For synchronization locks to use when acquiring
|
---|
[e84ab3d] | 260 | void on_notify( L &, struct thread$ * );
|
---|
[ac5816d] | 261 |
|
---|
| 262 | // For synchronization locks to use when releasing
|
---|
[22b7579] | 263 | size_t on_wait( L & );
|
---|
[ac5816d] | 264 |
|
---|
| 265 | // to set recursion count after getting signalled;
|
---|
[22b7579] | 266 | void on_wakeup( L &, size_t recursion );
|
---|
[ac5816d] | 267 | };
|
---|
[848439f] | 268 |
|
---|
[ac5816d] | 269 | //-----------------------------------------------------------------------------
|
---|
[82f4063] | 270 | // // info_thread
|
---|
| 271 | // // the info thread is a wrapper around a thread used
|
---|
| 272 | // // to store extra data for use in the condition variable
|
---|
[fd54fef] | 273 | forall(L & | is_blocking_lock(L)) {
|
---|
[ac5816d] | 274 | struct info_thread;
|
---|
[c131a02] | 275 |
|
---|
[82f4063] | 276 | // // for use by sequence
|
---|
| 277 | // info_thread(L) *& Back( info_thread(L) * this );
|
---|
| 278 | // info_thread(L) *& Next( info_thread(L) * this );
|
---|
[848439f] | 279 | }
|
---|
| 280 |
|
---|
[ac5816d] | 281 | //-----------------------------------------------------------------------------
|
---|
| 282 | // Synchronization Locks
|
---|
[fd54fef] | 283 | forall(L & | is_blocking_lock(L)) {
|
---|
[7f958c4] | 284 |
|
---|
| 285 | //-----------------------------------------------------------------------------
|
---|
| 286 | // condition_variable
|
---|
| 287 |
|
---|
| 288 | // The multi-tool condition variable
|
---|
| 289 | // - can pass timeouts to wait for either a signal or timeout
|
---|
| 290 | // - can wait without passing a lock
|
---|
| 291 | // - can have waiters reacquire different locks while waiting on the same cond var
|
---|
| 292 | // - has shadow queue
|
---|
| 293 | // - can be signalled outside of critical sections with no locks held
|
---|
[eeb5023] | 294 | struct condition_variable {
|
---|
[848439f] | 295 | // Spin lock used for mutual exclusion
|
---|
| 296 | __spinlock_t lock;
|
---|
| 297 |
|
---|
| 298 | // List of blocked threads
|
---|
[82f4063] | 299 | dlist( info_thread(L) ) blocked_threads;
|
---|
[848439f] | 300 |
|
---|
| 301 | // Count of current blocked threads
|
---|
| 302 | int count;
|
---|
| 303 | };
|
---|
[e84ab3d] | 304 |
|
---|
[848439f] | 305 |
|
---|
[ac5816d] | 306 | void ?{}( condition_variable(L) & this );
|
---|
[848439f] | 307 | void ^?{}( condition_variable(L) & this );
|
---|
| 308 |
|
---|
[eeb5023] | 309 | bool notify_one( condition_variable(L) & this );
|
---|
| 310 | bool notify_all( condition_variable(L) & this );
|
---|
[848439f] | 311 |
|
---|
[eeb5023] | 312 | uintptr_t front( condition_variable(L) & this );
|
---|
[848439f] | 313 |
|
---|
[ac5816d] | 314 | bool empty ( condition_variable(L) & this );
|
---|
| 315 | int counter( condition_variable(L) & this );
|
---|
[848439f] | 316 |
|
---|
[eeb5023] | 317 | void wait( condition_variable(L) & this );
|
---|
| 318 | void wait( condition_variable(L) & this, uintptr_t info );
|
---|
[dff1fd1] | 319 | bool wait( condition_variable(L) & this, Duration duration );
|
---|
| 320 | bool wait( condition_variable(L) & this, uintptr_t info, Duration duration );
|
---|
[848439f] | 321 |
|
---|
[eeb5023] | 322 | void wait( condition_variable(L) & this, L & l );
|
---|
| 323 | void wait( condition_variable(L) & this, L & l, uintptr_t info );
|
---|
[dff1fd1] | 324 | bool wait( condition_variable(L) & this, L & l, Duration duration );
|
---|
| 325 | bool wait( condition_variable(L) & this, L & l, uintptr_t info, Duration duration );
|
---|
[7f958c4] | 326 |
|
---|
| 327 | //-----------------------------------------------------------------------------
|
---|
| 328 | // fast_cond_var
|
---|
| 329 |
|
---|
| 330 | // The trimmed and slim condition variable
|
---|
| 331 | // - no internal lock so you must hold a lock while using this cond var
|
---|
| 332 | // - signalling without holding branded lock is UNSAFE!
|
---|
| 333 | // - only allows usage of one lock, cond var is branded after usage
|
---|
| 334 | struct fast_cond_var {
|
---|
| 335 | // List of blocked threads
|
---|
| 336 | dlist( info_thread(L) ) blocked_threads;
|
---|
| 337 |
|
---|
| 338 | #ifdef __CFA_DEBUG__
|
---|
| 339 | L * lock_used;
|
---|
| 340 | #endif
|
---|
| 341 | };
|
---|
| 342 |
|
---|
| 343 |
|
---|
| 344 | void ?{}( fast_cond_var(L) & this );
|
---|
| 345 | void ^?{}( fast_cond_var(L) & this );
|
---|
| 346 |
|
---|
| 347 | bool notify_one( fast_cond_var(L) & this );
|
---|
| 348 | bool notify_all( fast_cond_var(L) & this );
|
---|
| 349 |
|
---|
| 350 | uintptr_t front( fast_cond_var(L) & this );
|
---|
| 351 |
|
---|
| 352 | bool empty ( fast_cond_var(L) & this );
|
---|
| 353 |
|
---|
| 354 | void wait( fast_cond_var(L) & this, L & l );
|
---|
| 355 | void wait( fast_cond_var(L) & this, L & l, uintptr_t info );
|
---|
[f4ec5e45] | 356 | }
|
---|