source: benchmark/basic/ttst_lock.c @ a21dec4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since a21dec4 was b4107c8, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

update existing benchmarks for changes to bench.h, add new benchmarks in new programming languages

  • Property mode set to 100644
File size: 1.4 KB
RevLine 
[fe065c3]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
10typedef uintptr_t TYPE;                                                                 // addressable word-size
11static volatile TYPE lock __attribute__(( aligned (128) )); // Intel recommendation
12static TYPE PAD CALIGN __attribute__(( unused ));               // protect further false sharing
13
14static 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
27static inline void spin_unlock( volatile TYPE *lock ) {
28        __atomic_clear( lock, __ATOMIC_RELEASE );
29} // spin_unlock
30
31void __attribute__((noinline)) do_call() {
32        spin_lock( &lock );
33//      asm volatile ("");
34        spin_unlock( &lock );
35}
36
[b4107c8]37int main( int argc, char * argv[] ) {
38        BENCH_START()
[fe065c3]39        BENCH(
[b4107c8]40                for (size_t i = 0; i < times; i++) {
[fe065c3]41                        do_call();
42                },
43                result
[b4107c8]44        )
45        printf( "%g\n", result );
[fe065c3]46}
47
48// Local Variables: //
49// tab-width: 4 //
50// End: //
Note: See TracBrowser for help on using the repository browser.