]>
git.saurik.com Git - apple/libpthread.git/blob - tests/mutex_try.c
8 #include "darwintest_defaults.h"
11 volatile int last_holder
;
18 job(struct ctx
*ctx
, int idx
)
22 ret
= pthread_mutex_trylock(&ctx
->l
);
23 T_QUIET
; T_ASSERT_TRUE(ret
== EBUSY
|| ret
== 0, "trylock");
26 T_QUIET
; T_ASSERT_POSIX_ZERO(pthread_mutex_lock(&ctx
->l
),
27 "pthread_mutex_lock");
28 // we know that the other thread was just holding the lock
29 T_QUIET
; T_ASSERT_EQ(ctx
->last_holder
, !idx
,
30 "expecting oppsosite last holder after failed trylock");
33 ctx
->last_holder
= idx
;
34 ctx
->iterations
[idx
]++;
36 T_QUIET
; T_ASSERT_POSIX_ZERO(pthread_mutex_unlock(&ctx
->l
),
37 "pthread_mutex_unlock");
45 return job((struct ctx
*)ctx
, 0);
51 return job((struct ctx
*)ctx
, 1);
54 T_DECL(mutex_trylock
, "pthread_mutex_trylock",
55 T_META_ALL_VALID_ARCHS(YES
))
57 // This testcase spins up two threads with identical jobs. They try-lock
58 // the same mutex. If that fails, they check that the last holder of the
59 // lock is the other thread.
60 const int test_duration
= 10; // sec
65 T_ASSERT_POSIX_ZERO(pthread_mutex_init(&ctx
.l
, NULL
),
66 "pthread_mutex_init");
67 T_ASSERT_POSIX_ZERO(pthread_create(&t1
, NULL
, job1
, &ctx
),
69 T_ASSERT_POSIX_ZERO(pthread_create(&t2
, NULL
, job2
, &ctx
),
75 T_ASSERT_POSIX_ZERO(pthread_join(t1
, NULL
), "pthread join 1");
76 T_ASSERT_POSIX_ZERO(pthread_join(t2
, NULL
), "pthread join 2");
78 T_LOG("after %d seconds iterations 0: %d, 1: %d. Exiting\n",
79 test_duration
, ctx
.iterations
[0], ctx
.iterations
[1]);