| 1 |
/* { dg-options "-fprofile-arcs -ftest-coverage -pthread -fprofile-update=atomic" } */ |
| 2 |
/* { dg-do run { target native } } */ |
| 3 |
/* { dg-require-effective-target profile_update_atomic } */ |
| 4 |
|
| 5 |
#include <stdint.h> |
| 6 |
#include <pthread.h> |
| 7 |
#include <assert.h> |
| 8 |
|
| 9 |
#define NR 5 |
| 10 |
|
| 11 |
pthread_mutex_t cndMs[NR]; |
| 12 |
static void *ContentionNoDeadlock_thread(void *start) |
| 13 |
{ |
| 14 |
for (uint32_t k = 0; k < 100000; ++k) /* count(500005) */ |
| 15 |
{ |
| 16 |
int starti = *(int*)start; /* count(500000) */ |
| 17 |
for (uint32_t i = starti; i < NR; ++i) |
| 18 |
pthread_mutex_lock (&cndMs[i]); |
| 19 |
for (int32_t i = NR - 1; i >= starti; --i) |
| 20 |
pthread_mutex_unlock (&cndMs[i]); |
| 21 |
} |
| 22 |
|
| 23 |
return 0; |
| 24 |
} |
| 25 |
int main(int argc, char **argv) { |
| 26 |
for (unsigned i = 0; i < NR; i++) |
| 27 |
cndMs[i] = PTHREAD_MUTEX_INITIALIZER; |
| 28 |
|
| 29 |
pthread_t t[NR]; |
| 30 |
int ids[NR]; |
| 31 |
|
| 32 |
for (int i = 0; i < NR; i++) |
| 33 |
{ |
| 34 |
ids[i] = i; |
| 35 |
int r = pthread_create (&t[i], NULL, ContentionNoDeadlock_thread, &ids[i]); |
| 36 |
assert (r == 0); /* count(5*) */ |
| 37 |
} |
| 38 |
|
| 39 |
void *ret; |
| 40 |
for (int i = 0; i < NR; i++) |
| 41 |
{ |
| 42 |
int r = pthread_join (t[i], (void**)&ret); |
| 43 |
assert (r == 0); /* count(5*) */ |
| 44 |
} |
| 45 |
|
| 46 |
return 0; /* count(1) */ |
| 47 |
} |
| 48 |
|
| 49 |
/* { dg-final { run-gcov gcov-threads-1.C } } */ |