source:
benchmark/schedint/pthreads.c@
7ac3151
| Last change on this file since 7ac3151 was 26fd986, checked in by , 6 years ago | |
|---|---|
|
|
| File size: 1.0 KB | |
| Rev | Line | |
|---|---|---|
| [3eb4541] | 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; | |
| [26fd986] | 9 | pthread_cond_t c; |
| [3eb4541] | 10 | |
| 11 | void __attribute__((noinline)) call() { | |
| [26fd986] | 12 | pthread_mutex_lock( &m ); |
| 13 | pthread_cond_signal( &c ); | |
| 14 | pthread_mutex_unlock( &m ); | |
| [3eb4541] | 15 | } |
| 16 | ||
| [26fd986] | 17 | void __attribute__((noinline)) wait() { |
| [3eb4541] | 18 | pthread_mutex_lock(&m); |
| 19 | go = 1; | |
| [26fd986] | 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 | |
| [3eb4541] | 30 | BENCH( |
| [26fd986] | 31 | while ( go == 1 ) { call(); }, |
| [3eb4541] | 32 | result |
| 33 | ) | |
| [b4107c8] | 34 | printf( "%g\n", result ); |
| [3eb4541] | 35 | return NULL; |
| 36 | } | |
| 37 | ||
| [b4107c8] | 38 | int main( int argc, char * argv[] ) { |
| 39 | BENCH_START() | |
| [3eb4541] | 40 | pthread_t thread; |
| [26fd986] | 41 | if ( pthread_create( &thread, NULL, thread_main, NULL ) < 0 ) { |
| [3eb4541] | 42 | perror( "failure" ); |
| 43 | return 1; | |
| 44 | } | |
| 45 | wait(); | |
| [26fd986] | 46 | if ( pthread_join( thread, NULL ) < 0 ) { |
| [3eb4541] | 47 | perror( "failure" ); |
| 48 | return 1; | |
| 49 | } | |
| [dc33b5b] | 50 | } |
| [b4107c8] | 51 | |
| 52 | // Local Variables: // | |
| 53 | // tab-width: 4 // | |
| 54 | // End: // |
Note:
See TracBrowser
for help on using the repository browser.