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