blob: cc9266ab8ea47b191f524188e945954d7af5f7b9 (
plain)
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
|
/* { dg-options "-fprofile-arcs -ftest-coverage -pthread -fprofile-update=atomic" } */
/* { dg-do run { target native } } */
/* { dg-require-effective-target profile_update_atomic } */
#include <stdint.h>
#include <pthread.h>
#include <assert.h>
#define NR 5
pthread_mutex_t cndMs[NR];
static void *ContentionNoDeadlock_thread(void *start)
{
for (uint32_t k = 0; k < 100000; ++k) /* count(500005) */
{
int starti = *(int*)start; /* count(500000) */
for (uint32_t i = starti; i < NR; ++i)
pthread_mutex_lock (&cndMs[i]);
for (int32_t i = NR - 1; i >= starti; --i)
pthread_mutex_unlock (&cndMs[i]);
}
}
int main(int argc, char **argv) {
for (unsigned i = 0; i < NR; i++)
cndMs[i] = PTHREAD_MUTEX_INITIALIZER;
pthread_t t[NR];
int ids[NR];
for (int i = 0; i < NR; i++)
{
ids[i] = i;
int r = pthread_create (&t[i], NULL, ContentionNoDeadlock_thread, &ids[i]);
assert (r == 0); /* count(5) */
}
int ret;
for (int i = 0; i < NR; i++)
{
int r = pthread_join (t[i], (void**)&ret);
assert (r == 0); /* count(5) */
}
return 0; /* count(1) */
}
/* { dg-final { run-gcov gcov-threads-1.C } } */
|