source: benchmark/schedint/pthreads.c @ c7978c0

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since c7978c0 was 26fd986, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

update benchmarks for concurrency paper

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