File: test_pthread.c

package info (click to toggle)
eztrace 2.0%2Brepack-12
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,132 kB
  • sloc: ansic: 23,501; perl: 910; sh: 857; cpp: 771; makefile: 696; fortran: 327; f90: 320; python: 57
file content (171 lines) | stat: -rw-r--r-- 3,595 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- c-file-style: "GNU" -*- */
/*
 * Copyright (C) CNRS, INRIA, Université Bordeaux 1, Télécom SudParis
 * See COPYING in top-level directory.
 */

#include <stdlib.h>
#include <sys/time.h>
#include <time.h>
#include <semaphore.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>

// Debugging part, print out only if debugging level of the system is verbose or more
int _debug = -77;

void debug(char *fmt, ...) {
  if (_debug == -77) {
    char *buf = getenv("EZTRACE_DEBUG");
    if (buf == NULL)
      _debug = 0;
    else
      _debug = atoi(buf);
  }
  if (_debug >= 0) { // debug verbose mode
    va_list va;
    va_start(va, fmt);
    vfprintf(stdout, fmt, va);
    va_end(va);
  }
}
// end of debugging part

/* Number of iterations */
#define ITER 10
/* Number of threads */
#define NTH 2

typedef union {
  unsigned long long tick;
  struct {
    unsigned low;
    unsigned high;
  };
} tick_t;

#define TICK_DIFF(t1, t2) \
           ((t2).tick - (t1).tick)

#define TIME_DIFF(t1, t2) \
        ((t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec))

/* Only used during thread creating to make sure that the thread
 * got the correct args.
 */
sem_t thread_ready;

/* Block/unblock a thread */
sem_t sem[NTH];

pthread_mutex_t mutex;
pthread_spinlock_t spinlock;
pthread_barrier_t barrier;
pthread_cond_t cond;
pthread_rwlock_t rwlock;

/* Fake computation of usec microseconds */
void compute(int usec) {
  struct timeval tv1, tv2;
  gettimeofday(&tv1, NULL);
  do {
    gettimeofday(&tv2, NULL);
  } while (TIME_DIFF(tv1, tv2) < usec);
}

void* f_thread(void* arg) {
  int my_id = *(int*) arg;

  /* Notify the main thread that we got the args */
  sem_post(&thread_ready);

  debug("Running thread #%d\n", my_id);

  pthread_barrier_wait(&barrier);

  int i;
  for (i = 0; i < ITER; i++) {
    /* Wait until the previous thread has finished his job */
    sem_wait(&sem[my_id]);
    debug("[thread #%d] loop %d\n", my_id, i);

    /* test mutex */
    pthread_mutex_lock(&mutex);
    compute(5000);
    pthread_mutex_unlock(&mutex);

    compute(5000);

    /* test spinlock */
    pthread_spin_lock(&spinlock);
    compute(5000);
    pthread_spin_unlock(&spinlock);

    compute(5000);

    /* test spinlock */
    pthread_spin_lock(&spinlock);
    compute(5000);
    pthread_spin_unlock(&spinlock);

    compute(5000);

    /* test rwlock */
    pthread_rwlock_rdlock(&rwlock);
    compute(5000);
    pthread_rwlock_unlock(&rwlock);

    compute(5000);

    pthread_rwlock_wrlock(&rwlock);
    compute(5000);
    pthread_rwlock_unlock(&rwlock);

    compute(5000);

    /* Wake up the next thread */
    sem_post(&sem[(my_id + 1) % NTH]);
  }

  pthread_barrier_wait(&barrier);

  debug("End of thread #%d\n", my_id);
  return NULL;
}

int main(int argc, char**argv) {
  pthread_t tid[NTH];
  int i;

  pthread_mutex_init(&mutex, NULL);
  pthread_spin_init(&spinlock, 0);
  pthread_barrier_init(&barrier, NULL, NTH);
  pthread_cond_init(&cond, NULL);
  pthread_rwlock_init(&rwlock, NULL);

  sem_init(&thread_ready, 0, 0);
  for (i = 0; i < NTH; i++)
    sem_init(&sem[i], 0, 0);

  for (i = 0; i < NTH; i++) {
    pthread_create(&tid[i], NULL, f_thread, &i);
    sem_wait(&thread_ready);
  }

  /* Unblock the first thread so that it can start working */
  sem_post(&sem[0]);

  for (i = 0; i < NTH; i++) {
    pthread_join(tid[i], NULL);
  }

  pthread_mutex_destroy(&mutex);
  pthread_spin_destroy(&spinlock);
  pthread_barrier_destroy(&barrier);
  pthread_cond_destroy(&cond);
  pthread_rwlock_destroy(&rwlock);

  return 0;
}