| [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 | // | 
|---|
|  | 7 | // bits/locks.h -- Fast internal locks. | 
|---|
|  | 8 | // | 
|---|
|  | 9 | // Author           : Thierry Delisle | 
|---|
|  | 10 | // Created On       : Tue Oct 31 15:14:38 2017 | 
|---|
| [b158d8f] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
| [b10affd] | 12 | // Last Modified On : Fri Mar 30 18:18:13 2018 | 
|---|
|  | 13 | // Update Count     : 9 | 
|---|
| [ea7d2b0] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | #pragma once | 
|---|
|  | 17 |  | 
|---|
| [875a72f] | 18 | #include "bits/debug.h" | 
|---|
| [ea7d2b0] | 19 | #include "bits/defs.h" | 
|---|
|  | 20 |  | 
|---|
|  | 21 | // pause to prevent excess processor bus usage | 
|---|
|  | 22 | #if defined( __sparc ) | 
|---|
|  | 23 | #define Pause() __asm__ __volatile__ ( "rd %ccr,%g0" ) | 
|---|
|  | 24 | #elif defined( __i386 ) || defined( __x86_64 ) | 
|---|
|  | 25 | #define Pause() __asm__ __volatile__ ( "pause" : : : ) | 
|---|
| [b158d8f] | 26 | #elif defined( __ARM_ARCH ) | 
|---|
|  | 27 | #define Pause() __asm__ __volatile__ ( "nop" : : : ) | 
|---|
| [ea7d2b0] | 28 | #else | 
|---|
|  | 29 | #error unsupported architecture | 
|---|
|  | 30 | #endif | 
|---|
|  | 31 |  | 
|---|
| [b158d8f] | 32 | #if defined( __i386 ) || defined( __x86_64 ) || defined( __ARM_ARCH ) | 
|---|
| [ea7d2b0] | 33 | // Intel recommendation | 
|---|
|  | 34 | #define __ALIGN__ __attribute__(( aligned (128) )) | 
|---|
|  | 35 | #elif defined( __sparc ) | 
|---|
|  | 36 | #define __ALIGN__ CALIGN | 
|---|
|  | 37 | #else | 
|---|
|  | 38 | #error unsupported architecture | 
|---|
|  | 39 | #endif | 
|---|
|  | 40 |  | 
|---|
| [b158d8f] | 41 | #if __SIZEOF_SIZE_T__ == 8 | 
|---|
| [ea7d2b0] | 42 | #define __lock_test_and_test_and_set( lock ) (lock) == 0 && __sync_lock_test_and_set_8( &(lock), 1 ) == 0 | 
|---|
|  | 43 | #define __lock_release( lock ) __sync_lock_release_8( &(lock) ); | 
|---|
| [b158d8f] | 44 | #elif __SIZEOF_SIZE_T__ == 4 | 
|---|
| [ea7d2b0] | 45 | #define __lock_test_and_test_and_set( lock ) (lock) == 0 && __sync_lock_test_and_set_4( &(lock), 1 ) == 0 | 
|---|
|  | 46 | #define __lock_release( lock ) __sync_lock_release_4( &(lock) ); | 
|---|
|  | 47 | #else | 
|---|
|  | 48 | #error unsupported architecture | 
|---|
|  | 49 | #endif | 
|---|
|  | 50 |  | 
|---|
|  | 51 | struct __spinlock_t { | 
|---|
| [b158d8f] | 52 | __ALIGN__ volatile size_t lock; | 
|---|
| [ea7d2b0] | 53 | #ifdef __CFA_DEBUG__ | 
|---|
|  | 54 | const char * prev_name; | 
|---|
|  | 55 | void* prev_thrd; | 
|---|
|  | 56 | #endif | 
|---|
|  | 57 | } __ALIGN__; | 
|---|
|  | 58 |  | 
|---|
| [0cf5b79] | 59 | #ifdef __cforall | 
|---|
| [dbe9b08] | 60 | extern "C" { | 
|---|
|  | 61 | extern void disable_interrupts(); | 
|---|
|  | 62 | extern void enable_interrupts_noPoll(); | 
|---|
|  | 63 | } | 
|---|
|  | 64 |  | 
|---|
| [ea7d2b0] | 65 | extern void yield( unsigned int ); | 
|---|
|  | 66 |  | 
|---|
|  | 67 | static inline void ?{}( __spinlock_t & this ) { | 
|---|
|  | 68 | this.lock = 0; | 
|---|
|  | 69 | } | 
|---|
|  | 70 |  | 
|---|
|  | 71 | // Lock the spinlock, return false if already acquired | 
|---|
| [36982fc] | 72 | static inline _Bool try_lock  ( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) { | 
|---|
| [ea7d2b0] | 73 | _Bool result = __lock_test_and_test_and_set( this.lock ); | 
|---|
| [dbe9b08] | 74 | if( result ) { | 
|---|
|  | 75 | disable_interrupts(); | 
|---|
| [b10affd] | 76 | // __cfaabi_dbg_debug_do( | 
|---|
|  | 77 | //      this.prev_name = caller; | 
|---|
|  | 78 | //      this.prev_thrd = TL_GET( this_thread ); | 
|---|
|  | 79 | // ) | 
|---|
| [dbe9b08] | 80 | } | 
|---|
| [ea7d2b0] | 81 | return result; | 
|---|
|  | 82 | } | 
|---|
|  | 83 |  | 
|---|
|  | 84 | // Lock the spinlock, spin if already acquired | 
|---|
| [36982fc] | 85 | static inline void lock( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) { | 
|---|
| [ea7d2b0] | 86 | #ifndef NOEXPBACK | 
|---|
|  | 87 | enum { SPIN_START = 4, SPIN_END = 64 * 1024, }; | 
|---|
|  | 88 | unsigned int spin = SPIN_START; | 
|---|
|  | 89 | #endif | 
|---|
|  | 90 |  | 
|---|
|  | 91 | for ( unsigned int i = 1;; i += 1 ) { | 
|---|
|  | 92 | if ( __lock_test_and_test_and_set( this.lock ) ) break; | 
|---|
|  | 93 | #ifndef NOEXPBACK | 
|---|
|  | 94 | // exponential spin | 
|---|
|  | 95 | for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause(); | 
|---|
|  | 96 |  | 
|---|
|  | 97 | // slowly increase by powers of 2 | 
|---|
|  | 98 | if ( i % 64 == 0 ) spin += spin; | 
|---|
|  | 99 |  | 
|---|
|  | 100 | // prevent overflow | 
|---|
|  | 101 | if ( spin > SPIN_END ) spin = SPIN_START; | 
|---|
|  | 102 | #else | 
|---|
|  | 103 | Pause(); | 
|---|
|  | 104 | #endif | 
|---|
|  | 105 | } | 
|---|
| [dbe9b08] | 106 | disable_interrupts(); | 
|---|
| [b10affd] | 107 | // __cfaabi_dbg_debug_do( | 
|---|
|  | 108 | //      this.prev_name = caller; | 
|---|
|  | 109 | //      this.prev_thrd = TL_GET( this_thread ); | 
|---|
|  | 110 | // ) | 
|---|
| [ea7d2b0] | 111 | } | 
|---|
|  | 112 |  | 
|---|
| [2e9aed4] | 113 | // // Lock the spinlock, yield if already acquired | 
|---|
|  | 114 | // static inline void lock_yield( __spinlock_t & this __cfaabi_dbg_ctx_param2 ) { | 
|---|
|  | 115 | //      for ( unsigned int i = 1;; i += 1 ) { | 
|---|
|  | 116 | //              if ( __lock_test_and_test_and_set( this.lock ) ) break; | 
|---|
|  | 117 | //              yield( i ); | 
|---|
|  | 118 | //      } | 
|---|
|  | 119 | //      disable_interrupts(); | 
|---|
|  | 120 | //      __cfaabi_dbg_debug_do( | 
|---|
|  | 121 | //              this.prev_name = caller; | 
|---|
|  | 122 | //              this.prev_thrd = this_thread; | 
|---|
|  | 123 | //      ) | 
|---|
|  | 124 | // } | 
|---|
| [ea7d2b0] | 125 |  | 
|---|
|  | 126 | static inline void unlock( __spinlock_t & this ) { | 
|---|
| [dbe9b08] | 127 | enable_interrupts_noPoll(); | 
|---|
| [2e9aed4] | 128 | __lock_release( this.lock ); | 
|---|
| [ea7d2b0] | 129 | } | 
|---|
| [b158d8f] | 130 | #endif | 
|---|