ADT
        arm-eh
        ast-experimental
        enum
        forall-pointer-decay
        jacob/cs343-translation
        new-ast-unique-expr
        pthread-emulation
        qualifiedEnum
      
      
        
          | Last change
 on this file since fd2f4a9 was             525b5ef, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago | 
        
          | 
add documentation
 | 
        
          | 
              
Property                 mode
 set to                 100644 | 
        
          | File size:
            1.5 KB | 
      
      
| Rev | Line |  | 
|---|
| [fe065c3] | 1 | #include <stdio.h> | 
|---|
|  | 2 | #include <stdint.h>                                                                             // uintptr_t | 
|---|
|  | 3 |  | 
|---|
|  | 4 | #include "bench.h" | 
|---|
|  | 5 |  | 
|---|
| [525b5ef] | 6 | // Does a "lock xchg" on entry but a simple "mov" on exit => cheaper as 0 contention. While it has much more code, the | 
|---|
|  | 7 | // bulk is never run. | 
|---|
|  | 8 |  | 
|---|
| [fe065c3] | 9 | #define CALIGN __attribute__(( aligned (CACHE_ALIGN) )) | 
|---|
|  | 10 | #define CACHE_ALIGN 128 | 
|---|
|  | 11 | #define Pause() __asm__ __volatile__ ( "pause" : : : ) | 
|---|
|  | 12 |  | 
|---|
|  | 13 | typedef uintptr_t TYPE;                                                                 // addressable word-size | 
|---|
|  | 14 | static volatile TYPE lock __attribute__(( aligned (128) )); // Intel recommendation | 
|---|
|  | 15 | static TYPE PAD CALIGN __attribute__(( unused ));               // protect further false sharing | 
|---|
|  | 16 |  | 
|---|
|  | 17 | static inline void spin_lock( volatile TYPE *lock ) { | 
|---|
|  | 18 | enum { SPIN_START = 4, SPIN_END = 64 * 1024, }; | 
|---|
|  | 19 | unsigned int spin = SPIN_START; | 
|---|
|  | 20 |  | 
|---|
|  | 21 | for ( unsigned int i = 1;; i += 1 ) { | 
|---|
|  | 22 | if ( *lock == 0 && __atomic_test_and_set( lock, __ATOMIC_ACQUIRE ) == 0 ) break; | 
|---|
|  | 23 | for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause(); // exponential spin | 
|---|
|  | 24 | //spin += spin;                                                                 // powers of 2 | 
|---|
|  | 25 | if ( i % 64 == 0 ) spin += spin;                                // slowly increase by powers of 2 | 
|---|
|  | 26 | if ( spin > SPIN_END ) spin = SPIN_START;               // prevent overflow | 
|---|
|  | 27 | } // for | 
|---|
|  | 28 | } // spin_lock | 
|---|
|  | 29 |  | 
|---|
|  | 30 | static inline void spin_unlock( volatile TYPE *lock ) { | 
|---|
|  | 31 | __atomic_clear( lock, __ATOMIC_RELEASE ); | 
|---|
|  | 32 | } // spin_unlock | 
|---|
|  | 33 |  | 
|---|
|  | 34 | void __attribute__((noinline)) do_call() { | 
|---|
|  | 35 | spin_lock( &lock ); | 
|---|
|  | 36 | //      asm volatile (""); | 
|---|
|  | 37 | spin_unlock( &lock ); | 
|---|
|  | 38 | } | 
|---|
|  | 39 |  | 
|---|
| [b4107c8] | 40 | int main( int argc, char * argv[] ) { | 
|---|
|  | 41 | BENCH_START() | 
|---|
| [fe065c3] | 42 | BENCH( | 
|---|
| [b4107c8] | 43 | for (size_t i = 0; i < times; i++) { | 
|---|
| [fe065c3] | 44 | do_call(); | 
|---|
|  | 45 | }, | 
|---|
|  | 46 | result | 
|---|
| [b4107c8] | 47 | ) | 
|---|
|  | 48 | printf( "%g\n", result ); | 
|---|
| [fe065c3] | 49 | } | 
|---|
|  | 50 |  | 
|---|
|  | 51 | // Local Variables: // | 
|---|
|  | 52 | // tab-width: 4 // | 
|---|
|  | 53 | // End: // | 
|---|
       
      
  Note:
 See   
TracBrowser
 for help on using the repository browser.