ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change
on this file since 76de075 was
525b5ef,
checked in by Peter A. Buhr <pabuhr@…>, 5 years ago
|
add documentation
|
-
Property mode set to
100644
|
File size:
1.5 KB
|
Line | |
---|
1 | #include <stdio.h> |
---|
2 | #include <stdint.h> // uintptr_t |
---|
3 | |
---|
4 | #include "bench.h" |
---|
5 | |
---|
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 | |
---|
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 | |
---|
40 | int main( int argc, char * argv[] ) { |
---|
41 | BENCH_START() |
---|
42 | BENCH( |
---|
43 | for (size_t i = 0; i < times; i++) { |
---|
44 | do_call(); |
---|
45 | }, |
---|
46 | result |
---|
47 | ) |
---|
48 | printf( "%g\n", result ); |
---|
49 | } |
---|
50 | |
---|
51 | // Local Variables: // |
---|
52 | // tab-width: 4 // |
---|
53 | // End: // |
---|
Note: See
TracBrowser
for help on using the repository browser.