source: benchmark/schedint/pthreads.c @ ce9f9d4

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since ce9f9d4 was 26fd986, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

update benchmarks for concurrency paper

  • Property mode set to 100644
File size: 1.0 KB
Line 
1#include <pthread.h>
2#include <stdio.h>
3
4#include "bench.h"
5
6volatile int go = 0;
7
8pthread_mutex_t m;
9pthread_cond_t c;
10
11void __attribute__((noinline)) call() {
12        pthread_mutex_lock( &m );
13        pthread_cond_signal( &c );
14        pthread_mutex_unlock( &m );
15}
16
17void __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
27void * 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
38int 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.