Merge branch 'locking-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / tools / lib / lockdep / tests / ABBA_2threads.c
blob39325ef8a2ac176f9ebbbc7aaa6e799de171f8c1
1 // SPDX-License-Identifier: GPL-2.0
2 #include <stdio.h>
3 #include <pthread.h>
5 pthread_mutex_t a = PTHREAD_MUTEX_INITIALIZER;
6 pthread_mutex_t b = PTHREAD_MUTEX_INITIALIZER;
7 pthread_barrier_t bar;
9 void *ba_lock(void *arg)
11 int ret, i;
13 pthread_mutex_lock(&b);
15 if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD)
16 pthread_barrier_destroy(&bar);
18 pthread_mutex_lock(&a);
20 pthread_mutex_unlock(&a);
21 pthread_mutex_unlock(&b);
24 int main(void)
26 pthread_t t;
28 pthread_barrier_init(&bar, NULL, 2);
30 if (pthread_create(&t, NULL, ba_lock, NULL)) {
31 fprintf(stderr, "pthread_create() failed\n");
32 return 1;
34 pthread_mutex_lock(&a);
36 if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD)
37 pthread_barrier_destroy(&bar);
39 pthread_mutex_lock(&b);
41 pthread_mutex_unlock(&b);
42 pthread_mutex_unlock(&a);
44 pthread_join(t, NULL);
46 return 0;