[ea7d2b0] | 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 | // |
---|
[454f478] | 7 | // bits/locks.hfa -- Basic spinlocks that are reused in the system. |
---|
| 8 | // Used for locks that aren't specific to cforall threads and can be used anywhere |
---|
| 9 | // |
---|
| 10 | // *** Must not contain code specific to libcfathread *** |
---|
[ea7d2b0] | 11 | // |
---|
| 12 | // Author : Thierry Delisle |
---|
| 13 | // Created On : Tue Oct 31 15:14:38 2017 |
---|
[b158d8f] | 14 | // Last Modified By : Peter A. Buhr |
---|
[fd9b524] | 15 | // Last Modified On : Wed Aug 12 14:18:07 2020 |
---|
| 16 | // Update Count : 13 |
---|
[ea7d2b0] | 17 | // |
---|
| 18 | |
---|
| 19 | #pragma once |
---|
| 20 | |
---|
[73abe95] | 21 | #include "bits/debug.hfa" |
---|
| 22 | #include "bits/defs.hfa" |
---|
[ea8b2f7] | 23 | #include <assert.h> |
---|
| 24 | |
---|
[ea7d2b0] | 25 | struct __spinlock_t { |
---|
[13073be] | 26 | // Wrap in struct to prevent false sharing with debug info |
---|
[3aeee3c] | 27 | volatile bool lock; |
---|
[ea7d2b0] | 28 | #ifdef __CFA_DEBUG__ |
---|
[13073be] | 29 | // previous function to acquire the lock |
---|
[ea7d2b0] | 30 | const char * prev_name; |
---|
[13073be] | 31 | // previous thread to acquire the lock |
---|
[ea7d2b0] | 32 | void* prev_thrd; |
---|
| 33 | #endif |
---|
[3aeee3c] | 34 | }; |
---|
[ea7d2b0] | 35 | |
---|
[0cf5b79] | 36 | #ifdef __cforall |
---|
[dbe9b08] | 37 | extern "C" { |
---|
[2026bb6] | 38 | extern void disable_interrupts() OPTIONAL_THREAD; |
---|
| 39 | extern void enable_interrupts_noPoll() OPTIONAL_THREAD; |
---|
[1997b4e] | 40 | |
---|
| 41 | #ifdef __CFA_DEBUG__ |
---|
[ae66348] | 42 | void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]); |
---|
[1997b4e] | 43 | #else |
---|
[ae66348] | 44 | #define __cfaabi_dbg_record_lock(x, y) |
---|
[1997b4e] | 45 | #endif |
---|
[dbe9b08] | 46 | } |
---|
| 47 | |
---|
[ea7d2b0] | 48 | static inline void ?{}( __spinlock_t & this ) { |
---|
| 49 | this.lock = 0; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | // Lock the spinlock, return false if already acquired |
---|
[93c2e0a] | 53 | static inline bool try_lock ( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) { |
---|
[3381ed7] | 54 | disable_interrupts(); |
---|
[93c2e0a] | 55 | bool result = (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0); |
---|
[dbe9b08] | 56 | if( result ) { |
---|
[ae66348] | 57 | __cfaabi_dbg_record_lock( this, caller ); |
---|
[3381ed7] | 58 | } else { |
---|
| 59 | enable_interrupts_noPoll(); |
---|
[dbe9b08] | 60 | } |
---|
[ea7d2b0] | 61 | return result; |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | // Lock the spinlock, spin if already acquired |
---|
[36982fc] | 65 | static inline void lock( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) { |
---|
[ea7d2b0] | 66 | #ifndef NOEXPBACK |
---|
| 67 | enum { SPIN_START = 4, SPIN_END = 64 * 1024, }; |
---|
| 68 | unsigned int spin = SPIN_START; |
---|
| 69 | #endif |
---|
| 70 | |
---|
[3381ed7] | 71 | disable_interrupts(); |
---|
[ea7d2b0] | 72 | for ( unsigned int i = 1;; i += 1 ) { |
---|
[13073be] | 73 | if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break; |
---|
[ea7d2b0] | 74 | #ifndef NOEXPBACK |
---|
| 75 | // exponential spin |
---|
| 76 | for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause(); |
---|
| 77 | |
---|
| 78 | // slowly increase by powers of 2 |
---|
| 79 | if ( i % 64 == 0 ) spin += spin; |
---|
| 80 | |
---|
| 81 | // prevent overflow |
---|
| 82 | if ( spin > SPIN_END ) spin = SPIN_START; |
---|
| 83 | #else |
---|
| 84 | Pause(); |
---|
| 85 | #endif |
---|
| 86 | } |
---|
[ae66348] | 87 | __cfaabi_dbg_record_lock( this, caller ); |
---|
[ea7d2b0] | 88 | } |
---|
| 89 | |
---|
| 90 | static inline void unlock( __spinlock_t & this ) { |
---|
[13073be] | 91 | __atomic_clear( &this.lock, __ATOMIC_RELEASE ); |
---|
[3381ed7] | 92 | enable_interrupts_noPoll(); |
---|
[ea7d2b0] | 93 | } |
---|
[b158d8f] | 94 | #endif |
---|