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