| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | #include <stdio.h> |
| 3 | #include <pthread.h> |
| 4 | |
| 5 | pthread_mutex_t a = PTHREAD_MUTEX_INITIALIZER; |
| 6 | pthread_mutex_t b = PTHREAD_MUTEX_INITIALIZER; |
| 7 | pthread_barrier_t bar; |
| 8 | |
| 9 | void *ba_lock(void *arg) |
| 10 | { |
| 11 | int ret, i; |
| 12 | |
| 13 | pthread_mutex_lock(&b); |
| 14 | |
| 15 | if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD) |
| 16 | pthread_barrier_destroy(&bar); |
| 17 | |
| 18 | pthread_mutex_lock(&a); |
| 19 | |
| 20 | pthread_mutex_unlock(&a); |
| 21 | pthread_mutex_unlock(&b); |
| 22 | } |
| 23 | |
| 24 | int main(void) |
| 25 | { |
| 26 | pthread_t t; |
| 27 | |
| 28 | pthread_barrier_init(&bar, NULL, 2); |
| 29 | |
| 30 | if (pthread_create(&t, NULL, ba_lock, NULL)) { |
| 31 | fprintf(stderr, "pthread_create() failed\n"); |
| 32 | return 1; |
| 33 | } |
| 34 | pthread_mutex_lock(&a); |
| 35 | |
| 36 | if (pthread_barrier_wait(&bar) == PTHREAD_BARRIER_SERIAL_THREAD) |
| 37 | pthread_barrier_destroy(&bar); |
| 38 | |
| 39 | pthread_mutex_lock(&b); |
| 40 | |
| 41 | pthread_mutex_unlock(&b); |
| 42 | pthread_mutex_unlock(&a); |
| 43 | |
| 44 | pthread_join(t, NULL); |
| 45 | |
| 46 | return 0; |
| 47 | } |