| 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 | //
 | 
|---|
| 7 | // bits/locks.hfa -- Fast internal locks.
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Thierry Delisle
 | 
|---|
| 10 | // Created On       : Tue Oct 31 15:14:38 2017
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Tue Feb  4 13:03:19 2020
 | 
|---|
| 13 | // Update Count     : 11
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #pragma once
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "bits/debug.hfa"
 | 
|---|
| 19 | #include "bits/defs.hfa"
 | 
|---|
| 20 | #include <assert.h>
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #ifdef __cforall
 | 
|---|
| 23 |         extern "C" {
 | 
|---|
| 24 |                 #include <pthread.h>
 | 
|---|
| 25 |         }
 | 
|---|
| 26 | #endif
 | 
|---|
| 27 | 
 | 
|---|
| 28 | // pause to prevent excess processor bus usage
 | 
|---|
| 29 | #if defined( __sparc )
 | 
|---|
| 30 |         #define Pause() __asm__ __volatile__ ( "rd %ccr,%g0" )
 | 
|---|
| 31 | #elif defined( __i386 ) || defined( __x86_64 )
 | 
|---|
| 32 |         #define Pause() __asm__ __volatile__ ( "pause" : : : )
 | 
|---|
| 33 | #elif defined( __ARM_ARCH )
 | 
|---|
| 34 |         #define Pause() __asm__ __volatile__ ( "nop" : : : )
 | 
|---|
| 35 | #else
 | 
|---|
| 36 |         #error unsupported architecture
 | 
|---|
| 37 | #endif
 | 
|---|
| 38 | 
 | 
|---|
| 39 | struct __spinlock_t {
 | 
|---|
| 40 |         // Wrap in struct to prevent false sharing with debug info
 | 
|---|
| 41 |         volatile bool lock;
 | 
|---|
| 42 |         #ifdef __CFA_DEBUG__
 | 
|---|
| 43 |                 // previous function to acquire the lock
 | 
|---|
| 44 |                 const char * prev_name;
 | 
|---|
| 45 |                 // previous thread to acquire the lock
 | 
|---|
| 46 |                 void* prev_thrd;
 | 
|---|
| 47 |         #endif
 | 
|---|
| 48 | };
 | 
|---|
| 49 | 
 | 
|---|
| 50 | #ifdef __cforall
 | 
|---|
| 51 |         extern "C" {
 | 
|---|
| 52 |                 extern void disable_interrupts() OPTIONAL_THREAD;
 | 
|---|
| 53 |                 extern void enable_interrupts_noPoll() OPTIONAL_THREAD;
 | 
|---|
| 54 | 
 | 
|---|
| 55 |                 #ifdef __CFA_DEBUG__
 | 
|---|
| 56 |                         void __cfaabi_dbg_record_lock(__spinlock_t & this, const char prev_name[]);
 | 
|---|
| 57 |                 #else
 | 
|---|
| 58 |                         #define __cfaabi_dbg_record_lock(x, y)
 | 
|---|
| 59 |                 #endif
 | 
|---|
| 60 |         }
 | 
|---|
| 61 | 
 | 
|---|
| 62 |         static inline void ?{}( __spinlock_t & this ) {
 | 
|---|
| 63 |                 this.lock = 0;
 | 
|---|
| 64 |         }
 | 
|---|
| 65 | 
 | 
|---|
| 66 |         // Lock the spinlock, return false if already acquired
 | 
|---|
| 67 |         static inline bool try_lock  ( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
 | 
|---|
| 68 |                 disable_interrupts();
 | 
|---|
| 69 |                 bool result = (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0);
 | 
|---|
| 70 |                 if( result ) {
 | 
|---|
| 71 |                         __cfaabi_dbg_record_lock( this, caller );
 | 
|---|
| 72 |                 } else {
 | 
|---|
| 73 |                         enable_interrupts_noPoll();
 | 
|---|
| 74 |                 }
 | 
|---|
| 75 |                 return result;
 | 
|---|
| 76 |         }
 | 
|---|
| 77 | 
 | 
|---|
| 78 |         // Lock the spinlock, spin if already acquired
 | 
|---|
| 79 |         static inline void lock( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) {
 | 
|---|
| 80 |                 #ifndef NOEXPBACK
 | 
|---|
| 81 |                         enum { SPIN_START = 4, SPIN_END = 64 * 1024, };
 | 
|---|
| 82 |                         unsigned int spin = SPIN_START;
 | 
|---|
| 83 |                 #endif
 | 
|---|
| 84 | 
 | 
|---|
| 85 |                 disable_interrupts();
 | 
|---|
| 86 |                 for ( unsigned int i = 1;; i += 1 ) {
 | 
|---|
| 87 |                         if ( (this.lock == 0) && (__atomic_test_and_set( &this.lock, __ATOMIC_ACQUIRE ) == 0) ) break;
 | 
|---|
| 88 |                         #ifndef NOEXPBACK
 | 
|---|
| 89 |                                 // exponential spin
 | 
|---|
| 90 |                                 for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause();
 | 
|---|
| 91 | 
 | 
|---|
| 92 |                                 // slowly increase by powers of 2
 | 
|---|
| 93 |                                 if ( i % 64 == 0 ) spin += spin;
 | 
|---|
| 94 | 
 | 
|---|
| 95 |                                 // prevent overflow
 | 
|---|
| 96 |                                 if ( spin > SPIN_END ) spin = SPIN_START;
 | 
|---|
| 97 |                         #else
 | 
|---|
| 98 |                                 Pause();
 | 
|---|
| 99 |                         #endif
 | 
|---|
| 100 |                 }
 | 
|---|
| 101 |                 __cfaabi_dbg_record_lock( this, caller );
 | 
|---|
| 102 |         }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         static inline void unlock( __spinlock_t & this ) {
 | 
|---|
| 105 |                 __atomic_clear( &this.lock, __ATOMIC_RELEASE );
 | 
|---|
| 106 |                 enable_interrupts_noPoll();
 | 
|---|
| 107 |         }
 | 
|---|
| 108 | 
 | 
|---|
| 109 | 
 | 
|---|
| 110 |         #ifdef __CFA_WITH_VERIFY__
 | 
|---|
| 111 |                 extern bool __cfaabi_dbg_in_kernel();
 | 
|---|
| 112 |         #endif
 | 
|---|
| 113 | 
 | 
|---|
| 114 |         extern "C" {
 | 
|---|
| 115 |                 char * strerror(int);
 | 
|---|
| 116 |         }
 | 
|---|
| 117 |         #define CHECKED(x) { int err = x; if( err != 0 ) abort("KERNEL ERROR: Operation \"" #x "\" return error %d - %s\n", err, strerror(err)); }
 | 
|---|
| 118 | 
 | 
|---|
| 119 |         struct __bin_sem_t {
 | 
|---|
| 120 |                 pthread_mutex_t         lock;
 | 
|---|
| 121 |                 pthread_cond_t          cond;
 | 
|---|
| 122 |                 int                     val;
 | 
|---|
| 123 |         };
 | 
|---|
| 124 | 
 | 
|---|
| 125 |         static inline void ?{}(__bin_sem_t & this) with( this ) {
 | 
|---|
| 126 |                 // Create the mutex with error checking
 | 
|---|
| 127 |                 pthread_mutexattr_t mattr;
 | 
|---|
| 128 |                 pthread_mutexattr_init( &mattr );
 | 
|---|
| 129 |                 pthread_mutexattr_settype( &mattr, PTHREAD_MUTEX_ERRORCHECK_NP);
 | 
|---|
| 130 |                 pthread_mutex_init(&lock, &mattr);
 | 
|---|
| 131 | 
 | 
|---|
| 132 |                 pthread_cond_init (&cond, (const pthread_condattr_t *)0p);  // workaround trac#208: cast should not be required
 | 
|---|
| 133 |                 val = 0;
 | 
|---|
| 134 |         }
 | 
|---|
| 135 | 
 | 
|---|
| 136 |         static inline void ^?{}(__bin_sem_t & this) with( this ) {
 | 
|---|
| 137 |                 CHECKED( pthread_mutex_destroy(&lock) );
 | 
|---|
| 138 |                 CHECKED( pthread_cond_destroy (&cond) );
 | 
|---|
| 139 |         }
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         static inline void wait(__bin_sem_t & this) with( this ) {
 | 
|---|
| 142 |                 verify(__cfaabi_dbg_in_kernel());
 | 
|---|
| 143 |                 CHECKED( pthread_mutex_lock(&lock) );
 | 
|---|
| 144 |                         while(val < 1) {
 | 
|---|
| 145 |                                 pthread_cond_wait(&cond, &lock);
 | 
|---|
| 146 |                         }
 | 
|---|
| 147 |                         val -= 1;
 | 
|---|
| 148 |                 CHECKED( pthread_mutex_unlock(&lock) );
 | 
|---|
| 149 |         }
 | 
|---|
| 150 | 
 | 
|---|
| 151 |         static inline bool post(__bin_sem_t & this) with( this ) {
 | 
|---|
| 152 |                 bool needs_signal = false;
 | 
|---|
| 153 | 
 | 
|---|
| 154 |                 CHECKED( pthread_mutex_lock(&lock) );
 | 
|---|
| 155 |                         if(val < 1) {
 | 
|---|
| 156 |                                 val += 1;
 | 
|---|
| 157 |                                 pthread_cond_signal(&cond);
 | 
|---|
| 158 |                                 needs_signal = true;
 | 
|---|
| 159 |                         }
 | 
|---|
| 160 |                 CHECKED( pthread_mutex_unlock(&lock) );
 | 
|---|
| 161 | 
 | 
|---|
| 162 |                 return needs_signal;
 | 
|---|
| 163 |         }
 | 
|---|
| 164 | 
 | 
|---|
| 165 |         #undef CHECKED
 | 
|---|
| 166 | 
 | 
|---|
| 167 |         struct $thread;
 | 
|---|
| 168 |         extern void park( __cfaabi_dbg_ctx_param );
 | 
|---|
| 169 |         extern void unpark( struct $thread * this __cfaabi_dbg_ctx_param2 );
 | 
|---|
| 170 |         static inline struct $thread * active_thread ();
 | 
|---|
| 171 | 
 | 
|---|
| 172 |         // Semaphore which only supports a single thread
 | 
|---|
| 173 |         struct single_sem {
 | 
|---|
| 174 |                 struct $thread * volatile ptr;
 | 
|---|
| 175 |         };
 | 
|---|
| 176 | 
 | 
|---|
| 177 |         static inline {
 | 
|---|
| 178 |                 void  ?{}(single_sem & this) {
 | 
|---|
| 179 |                         this.ptr = 0p;
 | 
|---|
| 180 |                 }
 | 
|---|
| 181 | 
 | 
|---|
| 182 |                 void ^?{}(single_sem & this) {}
 | 
|---|
| 183 | 
 | 
|---|
| 184 |                 bool wait(single_sem & this) {
 | 
|---|
| 185 |                         for() {
 | 
|---|
| 186 |                                 struct $thread * expected = this.ptr;
 | 
|---|
| 187 |                                 if(expected == 1p) {
 | 
|---|
| 188 |                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
 | 
|---|
| 189 |                                                 return false;
 | 
|---|
| 190 |                                         }
 | 
|---|
| 191 |                                 }
 | 
|---|
| 192 |                                 else {
 | 
|---|
| 193 |                                         /* paranoid */ verify( expected == 0p );
 | 
|---|
| 194 |                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
 | 
|---|
| 195 |                                                 park( __cfaabi_dbg_ctx );
 | 
|---|
| 196 |                                                 return true;
 | 
|---|
| 197 |                                         }
 | 
|---|
| 198 |                                 }
 | 
|---|
| 199 | 
 | 
|---|
| 200 |                         }
 | 
|---|
| 201 |                 }
 | 
|---|
| 202 | 
 | 
|---|
| 203 |                 bool post(single_sem & this) {
 | 
|---|
| 204 |                         for() {
 | 
|---|
| 205 |                                 struct $thread * expected = this.ptr;
 | 
|---|
| 206 |                                 if(expected == 1p) return false;
 | 
|---|
| 207 |                                 if(expected == 0p) {
 | 
|---|
| 208 |                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, 1p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
 | 
|---|
| 209 |                                                 return false;
 | 
|---|
| 210 |                                         }
 | 
|---|
| 211 |                                 }
 | 
|---|
| 212 |                                 else {
 | 
|---|
| 213 |                                         if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) {
 | 
|---|
| 214 |                                                 unpark( expected __cfaabi_dbg_ctx2 );
 | 
|---|
| 215 |                                                 return true;
 | 
|---|
| 216 |                                         }
 | 
|---|
| 217 |                                 }
 | 
|---|
| 218 |                         }
 | 
|---|
| 219 |                 }
 | 
|---|
| 220 |         }
 | 
|---|
| 221 | #endif
 | 
|---|