1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker #include <pthread.h>
7*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker #define TST_NO_DEFAULT_MAIN
10*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
11*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_create(const char * file,const int lineno,pthread_t * thread_id,const pthread_attr_t * attr,void * (* thread_fn)(void *),void * arg)12*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_create(const char *file, const int lineno,
13*49cdfc7eSAndroid Build Coastguard Worker pthread_t *thread_id, const pthread_attr_t *attr,
14*49cdfc7eSAndroid Build Coastguard Worker void *(*thread_fn)(void *), void *arg)
15*49cdfc7eSAndroid Build Coastguard Worker {
16*49cdfc7eSAndroid Build Coastguard Worker int rval;
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker rval = pthread_create(thread_id, attr, thread_fn, arg);
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker if (rval) {
21*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
22*49cdfc7eSAndroid Build Coastguard Worker "pthread_create(%p,%p,%p,%p) failed: %s", thread_id,
23*49cdfc7eSAndroid Build Coastguard Worker attr, thread_fn, arg, tst_strerrno(rval));
24*49cdfc7eSAndroid Build Coastguard Worker }
25*49cdfc7eSAndroid Build Coastguard Worker
26*49cdfc7eSAndroid Build Coastguard Worker return rval;
27*49cdfc7eSAndroid Build Coastguard Worker }
28*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_join(const char * file,const int lineno,pthread_t thread_id,void ** retval)29*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_join(const char *file, const int lineno,
30*49cdfc7eSAndroid Build Coastguard Worker pthread_t thread_id, void **retval)
31*49cdfc7eSAndroid Build Coastguard Worker {
32*49cdfc7eSAndroid Build Coastguard Worker int rval;
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker rval = pthread_join(thread_id, retval);
35*49cdfc7eSAndroid Build Coastguard Worker
36*49cdfc7eSAndroid Build Coastguard Worker if (rval) {
37*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
38*49cdfc7eSAndroid Build Coastguard Worker "pthread_join(..., %p) failed: %s",
39*49cdfc7eSAndroid Build Coastguard Worker retval, tst_strerrno(rval));
40*49cdfc7eSAndroid Build Coastguard Worker }
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker return rval;
43*49cdfc7eSAndroid Build Coastguard Worker }
44*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_barrier_wait(const char * file,const int lineno,pthread_barrier_t * barrier)45*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_barrier_wait(const char *file, const int lineno,
46*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t *barrier)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker int rval;
49*49cdfc7eSAndroid Build Coastguard Worker
50*49cdfc7eSAndroid Build Coastguard Worker rval = pthread_barrier_wait(barrier);
51*49cdfc7eSAndroid Build Coastguard Worker
52*49cdfc7eSAndroid Build Coastguard Worker if (rval && rval != PTHREAD_BARRIER_SERIAL_THREAD) {
53*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
54*49cdfc7eSAndroid Build Coastguard Worker "pthread_barrier_wait(%p) failed: %s",
55*49cdfc7eSAndroid Build Coastguard Worker barrier, tst_strerrno(rval));
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker return rval;
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_barrier_destroy(const char * file,const int lineno,pthread_barrier_t * barrier)61*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_barrier_destroy(const char *file, const int lineno,
62*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t *barrier)
63*49cdfc7eSAndroid Build Coastguard Worker {
64*49cdfc7eSAndroid Build Coastguard Worker int rval;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker rval = pthread_barrier_destroy(barrier);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker if (rval) {
69*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
70*49cdfc7eSAndroid Build Coastguard Worker "pthread_barrier_destroy(%p) failed: %s",
71*49cdfc7eSAndroid Build Coastguard Worker barrier, tst_strerrno(rval));
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker return rval;
75*49cdfc7eSAndroid Build Coastguard Worker }
76*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_cancel(const char * file,const int lineno,pthread_t thread_id)77*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_cancel(const char *file, const int lineno,
78*49cdfc7eSAndroid Build Coastguard Worker pthread_t thread_id)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker int rval;
81*49cdfc7eSAndroid Build Coastguard Worker
82*49cdfc7eSAndroid Build Coastguard Worker rval = pthread_cancel(thread_id);
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker if (rval) {
85*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
86*49cdfc7eSAndroid Build Coastguard Worker "pthread_cancel(...) failed: %s", tst_strerrno(rval));
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker return rval;
90*49cdfc7eSAndroid Build Coastguard Worker }
91*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_barrier_init(const char * file,const int lineno,pthread_barrier_t * barrier,const pthread_barrierattr_t * attr,unsigned count)92*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_barrier_init(const char *file, const int lineno,
93*49cdfc7eSAndroid Build Coastguard Worker pthread_barrier_t *barrier,
94*49cdfc7eSAndroid Build Coastguard Worker const pthread_barrierattr_t *attr,
95*49cdfc7eSAndroid Build Coastguard Worker unsigned count)
96*49cdfc7eSAndroid Build Coastguard Worker {
97*49cdfc7eSAndroid Build Coastguard Worker int rval;
98*49cdfc7eSAndroid Build Coastguard Worker
99*49cdfc7eSAndroid Build Coastguard Worker rval = pthread_barrier_init(barrier, attr, count);
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker if (rval) {
102*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
103*49cdfc7eSAndroid Build Coastguard Worker "pthread_barrier_init(%p, %p, %u)failed: %s",
104*49cdfc7eSAndroid Build Coastguard Worker barrier, attr, count, tst_strerrno(rval));
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker
107*49cdfc7eSAndroid Build Coastguard Worker return rval;
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutexattr_init(const char * file,const int lineno,pthread_mutexattr_t * attr)110*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutexattr_init(const char *file, const int lineno,
111*49cdfc7eSAndroid Build Coastguard Worker pthread_mutexattr_t *attr)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker int ret;
114*49cdfc7eSAndroid Build Coastguard Worker
115*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutexattr_init(attr);
116*49cdfc7eSAndroid Build Coastguard Worker
117*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
118*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
119*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutexattr_init(%p) failed: %s",
120*49cdfc7eSAndroid Build Coastguard Worker attr, tst_strerrno(ret));
121*49cdfc7eSAndroid Build Coastguard Worker }
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker return ret;
124*49cdfc7eSAndroid Build Coastguard Worker }
125*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutexattr_destroy(const char * file,const int lineno,pthread_mutexattr_t * attr)126*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutexattr_destroy(const char *file, const int lineno,
127*49cdfc7eSAndroid Build Coastguard Worker pthread_mutexattr_t *attr)
128*49cdfc7eSAndroid Build Coastguard Worker {
129*49cdfc7eSAndroid Build Coastguard Worker int ret;
130*49cdfc7eSAndroid Build Coastguard Worker
131*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutexattr_destroy(attr);
132*49cdfc7eSAndroid Build Coastguard Worker
133*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
134*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
135*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutexattr_destroy(%p) failed: %s",
136*49cdfc7eSAndroid Build Coastguard Worker attr, tst_strerrno(ret));
137*49cdfc7eSAndroid Build Coastguard Worker }
138*49cdfc7eSAndroid Build Coastguard Worker
139*49cdfc7eSAndroid Build Coastguard Worker return ret;
140*49cdfc7eSAndroid Build Coastguard Worker }
141*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutexattr_settype(const char * file,const int lineno,pthread_mutexattr_t * attr,int type)142*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutexattr_settype(const char *file, const int lineno,
143*49cdfc7eSAndroid Build Coastguard Worker pthread_mutexattr_t *attr, int type)
144*49cdfc7eSAndroid Build Coastguard Worker {
145*49cdfc7eSAndroid Build Coastguard Worker int ret;
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutexattr_settype(attr, type);
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
150*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
151*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutexattr_settype(%p, %d) failed: %s",
152*49cdfc7eSAndroid Build Coastguard Worker attr, type, tst_strerrno(ret));
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker return ret;
156*49cdfc7eSAndroid Build Coastguard Worker }
157*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutex_init(const char * file,const int lineno,pthread_mutex_t * mutex,const pthread_mutexattr_t * attr)158*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_init(const char *file, const int lineno,
159*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
160*49cdfc7eSAndroid Build Coastguard Worker {
161*49cdfc7eSAndroid Build Coastguard Worker int ret;
162*49cdfc7eSAndroid Build Coastguard Worker
163*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutex_init(mutex, attr);
164*49cdfc7eSAndroid Build Coastguard Worker
165*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
166*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
167*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutex_init(%p, %p) failed: %s",
168*49cdfc7eSAndroid Build Coastguard Worker mutex, attr, tst_strerrno(ret));
169*49cdfc7eSAndroid Build Coastguard Worker }
170*49cdfc7eSAndroid Build Coastguard Worker
171*49cdfc7eSAndroid Build Coastguard Worker return ret;
172*49cdfc7eSAndroid Build Coastguard Worker }
173*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutex_destroy(const char * file,const int lineno,pthread_mutex_t * mutex)174*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_destroy(const char *file, const int lineno,
175*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex)
176*49cdfc7eSAndroid Build Coastguard Worker {
177*49cdfc7eSAndroid Build Coastguard Worker int ret;
178*49cdfc7eSAndroid Build Coastguard Worker
179*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutex_destroy(mutex);
180*49cdfc7eSAndroid Build Coastguard Worker
181*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
182*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
183*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutex_destroy(%p) failed: %s",
184*49cdfc7eSAndroid Build Coastguard Worker mutex, tst_strerrno(ret));
185*49cdfc7eSAndroid Build Coastguard Worker }
186*49cdfc7eSAndroid Build Coastguard Worker
187*49cdfc7eSAndroid Build Coastguard Worker return ret;
188*49cdfc7eSAndroid Build Coastguard Worker }
189*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutex_lock(const char * file,const int lineno,pthread_mutex_t * mutex)190*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_lock(const char *file, const int lineno,
191*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex)
192*49cdfc7eSAndroid Build Coastguard Worker {
193*49cdfc7eSAndroid Build Coastguard Worker int ret;
194*49cdfc7eSAndroid Build Coastguard Worker
195*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutex_lock(mutex);
196*49cdfc7eSAndroid Build Coastguard Worker
197*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
198*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
199*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutex_lock(%p) failed: %s",
200*49cdfc7eSAndroid Build Coastguard Worker mutex, tst_strerrno(ret));
201*49cdfc7eSAndroid Build Coastguard Worker }
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker return ret;
204*49cdfc7eSAndroid Build Coastguard Worker }
205*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutex_trylock(const char * file,const int lineno,pthread_mutex_t * mutex)206*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_trylock(const char *file, const int lineno,
207*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex)
208*49cdfc7eSAndroid Build Coastguard Worker {
209*49cdfc7eSAndroid Build Coastguard Worker int ret;
210*49cdfc7eSAndroid Build Coastguard Worker
211*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutex_trylock(mutex);
212*49cdfc7eSAndroid Build Coastguard Worker
213*49cdfc7eSAndroid Build Coastguard Worker if (ret && ret != EBUSY) {
214*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
215*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutex_trylock(%p) failed: %s",
216*49cdfc7eSAndroid Build Coastguard Worker mutex, tst_strerrno(ret));
217*49cdfc7eSAndroid Build Coastguard Worker }
218*49cdfc7eSAndroid Build Coastguard Worker
219*49cdfc7eSAndroid Build Coastguard Worker return ret;
220*49cdfc7eSAndroid Build Coastguard Worker }
221*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutex_timedlock(const char * file,const int lineno,pthread_mutex_t * mutex,const struct timespec * abstime)222*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_timedlock(const char *file, const int lineno,
223*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex, const struct timespec *abstime)
224*49cdfc7eSAndroid Build Coastguard Worker {
225*49cdfc7eSAndroid Build Coastguard Worker int ret;
226*49cdfc7eSAndroid Build Coastguard Worker
227*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutex_timedlock(mutex, abstime);
228*49cdfc7eSAndroid Build Coastguard Worker
229*49cdfc7eSAndroid Build Coastguard Worker if (ret && ret != ETIMEDOUT) {
230*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
231*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutex_timedlock(%p, {%lld, %ld}) failed: %s",
232*49cdfc7eSAndroid Build Coastguard Worker mutex, (long long)abstime->tv_sec, abstime->tv_nsec,
233*49cdfc7eSAndroid Build Coastguard Worker tst_strerrno(ret));
234*49cdfc7eSAndroid Build Coastguard Worker }
235*49cdfc7eSAndroid Build Coastguard Worker
236*49cdfc7eSAndroid Build Coastguard Worker return ret;
237*49cdfc7eSAndroid Build Coastguard Worker }
238*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_mutex_unlock(const char * file,const int lineno,pthread_mutex_t * mutex)239*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_mutex_unlock(const char *file, const int lineno,
240*49cdfc7eSAndroid Build Coastguard Worker pthread_mutex_t *mutex)
241*49cdfc7eSAndroid Build Coastguard Worker {
242*49cdfc7eSAndroid Build Coastguard Worker int ret;
243*49cdfc7eSAndroid Build Coastguard Worker
244*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_mutex_unlock(mutex);
245*49cdfc7eSAndroid Build Coastguard Worker
246*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
247*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
248*49cdfc7eSAndroid Build Coastguard Worker "pthread_mutex_unlock(%p) failed: %s",
249*49cdfc7eSAndroid Build Coastguard Worker mutex, tst_strerrno(ret));
250*49cdfc7eSAndroid Build Coastguard Worker }
251*49cdfc7eSAndroid Build Coastguard Worker
252*49cdfc7eSAndroid Build Coastguard Worker return ret;
253*49cdfc7eSAndroid Build Coastguard Worker }
254*49cdfc7eSAndroid Build Coastguard Worker
safe_pthread_kill(const char * file,const int lineno,pthread_t thread,int sig)255*49cdfc7eSAndroid Build Coastguard Worker int safe_pthread_kill(const char *file, const int lineno,
256*49cdfc7eSAndroid Build Coastguard Worker pthread_t thread, int sig)
257*49cdfc7eSAndroid Build Coastguard Worker {
258*49cdfc7eSAndroid Build Coastguard Worker int ret;
259*49cdfc7eSAndroid Build Coastguard Worker
260*49cdfc7eSAndroid Build Coastguard Worker ret = pthread_kill(thread, sig);
261*49cdfc7eSAndroid Build Coastguard Worker
262*49cdfc7eSAndroid Build Coastguard Worker if (ret) {
263*49cdfc7eSAndroid Build Coastguard Worker tst_brk_(file, lineno, TBROK,
264*49cdfc7eSAndroid Build Coastguard Worker "pthread_kill(..., %d) failed: %s",
265*49cdfc7eSAndroid Build Coastguard Worker sig, tst_strerrno(ret));
266*49cdfc7eSAndroid Build Coastguard Worker }
267*49cdfc7eSAndroid Build Coastguard Worker
268*49cdfc7eSAndroid Build Coastguard Worker return ret;
269*49cdfc7eSAndroid Build Coastguard Worker }
270