/ tests / lowlevel / mutex / test.c
test.c
 1  #include <rtapi_mutex.h>
 2  #include <pthread.h>
 3  #include <assert.h>
 4  #include <stdio.h>
 5  
 6  // to make this test fail, you can...
 7  #if 0
 8  #define rtapi_mutex_give(x) (void)0
 9  #define rtapi_mutex_get(x) (void)0
10  #endif
11  
12  const unsigned int N = 100000;
13  unsigned long mut;
14  unsigned long count;
15  
16  void *thread_fun(void *unused) {
17      for(unsigned long i=0; i<N; i++) {
18          rtapi_mutex_get(&mut);
19              unsigned long tmp = count;
20              tmp++;
21              sched_yield();
22              count = tmp;
23          rtapi_mutex_give(&mut);
24      }
25      return unused;
26  }
27  
28  int main() {
29      pthread_t t1, t2;
30      pthread_create(&t1, NULL, thread_fun, NULL);
31      pthread_create(&t2, NULL, thread_fun, NULL);
32      pthread_join(t1, NULL);
33      pthread_join(t2, NULL);
34      printf("count=%lu\n", count);
35      assert(count == 2*N);
36      printf("test passed\n");
37      return 0;
38  }