source:
benchmark/schedint/pthreads.c@
13f066d
| Last change on this file since 13f066d was 26fd986, checked in by , 6 years ago | |
|---|---|
|
|
| File size: 1.0 KB | |
| Line | |
|---|---|
| 1 | #include <pthread.h> |
| 2 | #include <stdio.h> |
| 3 | |
| 4 | #include "bench.h" |
| 5 | |
| 6 | volatile int go = 0; |
| 7 | |
| 8 | pthread_mutex_t m; |
| 9 | pthread_cond_t c; |
| 10 | |
| 11 | void __attribute__((noinline)) call() { |
| 12 | pthread_mutex_lock( &m ); |
| 13 | pthread_cond_signal( &c ); |
| 14 | pthread_mutex_unlock( &m ); |
| 15 | } |
| 16 | |
| 17 | void __attribute__((noinline)) wait() { |
| 18 | pthread_mutex_lock(&m); |
| 19 | go = 1; |
| 20 | for ( size_t i = 0; i < times; i++ ) { |
| 21 | pthread_cond_wait( &c, &m ); |
| 22 | } |
| 23 | go = 0; |
| 24 | pthread_mutex_unlock( &m ); |
| 25 | } |
| 26 | |
| 27 | void * thread_main( __attribute__((unused)) void * arg ) { |
| 28 | while ( go == 0 ) { sched_yield(); } // waiter must start first |
| 29 | // barging for lock acquire => may not execute N times |
| 30 | BENCH( |
| 31 | while ( go == 1 ) { call(); }, |
| 32 | result |
| 33 | ) |
| 34 | printf( "%g\n", result ); |
| 35 | return NULL; |
| 36 | } |
| 37 | |
| 38 | int main( int argc, char * argv[] ) { |
| 39 | BENCH_START() |
| 40 | pthread_t thread; |
| 41 | if ( pthread_create( &thread, NULL, thread_main, NULL ) < 0 ) { |
| 42 | perror( "failure" ); |
| 43 | return 1; |
| 44 | } |
| 45 | wait(); |
| 46 | if ( pthread_join( thread, NULL ) < 0 ) { |
| 47 | perror( "failure" ); |
| 48 | return 1; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // Local Variables: // |
| 53 | // tab-width: 4 // |
| 54 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.