StarPU Handbook
starpu_thread_util.h
Go to the documentation of this file.
1 /* StarPU --- Runtime system for heterogeneous multicore architectures.
2  *
3  * Copyright (C) 2012,2013 Inria
4  * Copyright (C) 2010-2013,2015,2017,2019 CNRS
5  * Copyright (C) 2010-2014,2016,2017 Université de Bordeaux
6  *
7  * StarPU is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as published by
9  * the Free Software Foundation; either version 2.1 of the License, or (at
10  * your option) any later version.
11  *
12  * StarPU is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15  *
16  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
17  */
18 
19 // The documentation for this file is in doc/doxygen/chapters/api/threads.doxy
20 
21 #ifndef __STARPU_THREAD_UTIL_H__
22 #define __STARPU_THREAD_UTIL_H__
23 
24 #include <starpu_util.h>
25 #include <starpu_thread.h>
26 #include <errno.h>
27 
28 #if !(defined(_MSC_VER) && !defined(BUILDING_STARPU))
29 /*
30  * Encapsulation of the starpu_pthread_create_* functions.
31  */
32 
33 #define STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, where) do { \
34  int p_ret = starpu_pthread_create_on((name), (thread), (attr), (routine), (arg), (where)); \
35  if (STARPU_UNLIKELY(p_ret != 0)) { \
36  fprintf(stderr, \
37  "%s:%d starpu_pthread_create_on: %s\n", \
38  __FILE__, __LINE__, strerror(p_ret)); \
39  STARPU_ABORT(); \
40  } \
41 } while (0)
42 
43 #define STARPU_PTHREAD_CREATE(thread, attr, routine, arg) do { \
44  int p_ret = starpu_pthread_create((thread), (attr), (routine), (arg)); \
45  if (STARPU_UNLIKELY(p_ret != 0)) { \
46  fprintf(stderr, \
47  "%s:%d starpu_pthread_create: %s\n", \
48  __FILE__, __LINE__, strerror(p_ret)); \
49  STARPU_ABORT(); \
50  } \
51 } while (0)
52 
53 #define STARPU_PTHREAD_JOIN(thread, retval) do { \
54  int p_ret = starpu_pthread_join((thread), (retval)); \
55  if (STARPU_UNLIKELY(p_ret != 0)) { \
56  fprintf(stderr, \
57  "%s:%d starpu_pthread_join: %s\n", \
58  __FILE__, __LINE__, strerror(p_ret)); \
59  STARPU_ABORT(); \
60  } \
61 } while (0)
62 
63 /*
64  * Encapsulation of the starpu_pthread_mutex_* functions.
65  */
66 
67 #define STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
68  int p_ret = starpu_pthread_mutex_init((mutex), (attr)); \
69  if (STARPU_UNLIKELY(p_ret)) { \
70  fprintf(stderr, \
71  "%s:%d starpu_pthread_mutex_init: %s\n", \
72  __FILE__, __LINE__, strerror(p_ret)); \
73  STARPU_ABORT(); \
74  } \
75 } while (0)
76 
77 #define STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
78  int p_ret = starpu_pthread_mutex_destroy(mutex); \
79  if (STARPU_UNLIKELY(p_ret)) { \
80  fprintf(stderr, \
81  "%s:%d starpu_pthread_mutex_destroy: %s\n", \
82  __FILE__, __LINE__, strerror(p_ret)); \
83  STARPU_ABORT(); \
84  } \
85 } while(0)
86 
87 #ifdef STARPU_DEBUG
88 #define _STARPU_CHECK_NOT_SCHED_MUTEX(mutex, file, line) \
89  starpu_pthread_mutex_check_sched((mutex), file, line)
90 #else
91 #define _STARPU_CHECK_NOT_SCHED_MUTEX(mutex, file, line)
92 #endif
93 
94 #define STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
95  int p_ret = starpu_pthread_mutex_lock(mutex); \
96  if (STARPU_UNLIKELY(p_ret)) { \
97  fprintf(stderr, \
98  "%s:%d starpu_pthread_mutex_lock: %s\n", \
99  __FILE__, __LINE__, strerror(p_ret)); \
100  STARPU_ABORT(); \
101  } \
102  _STARPU_CHECK_NOT_SCHED_MUTEX(mutex, __FILE__, __LINE__); \
103 } while (0)
104 
105 #define STARPU_PTHREAD_MUTEX_LOCK_SCHED(mutex) do { \
106  int p_ret = starpu_pthread_mutex_lock_sched(mutex); \
107  if (STARPU_UNLIKELY(p_ret)) { \
108  fprintf(stderr, \
109  "%s:%d starpu_pthread_mutex_lock_sched: %s\n", \
110  __FILE__, __LINE__, strerror(p_ret)); \
111  STARPU_ABORT(); \
112  } \
113 } while (0)
114 
115 #define STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) \
116  _starpu_pthread_mutex_trylock(mutex, __FILE__, __LINE__)
117 static STARPU_INLINE
118 int _starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex, char *file, int line)
119 {
120  int p_ret = starpu_pthread_mutex_trylock(mutex);
121  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
122  fprintf(stderr,
123  "%s:%d starpu_pthread_mutex_trylock: %s\n",
124  file, line, strerror(p_ret));
125  STARPU_ABORT();
126  }
127  _STARPU_CHECK_NOT_SCHED_MUTEX(mutex, file, line);
128  return p_ret;
129 }
130 
131 #define STARPU_PTHREAD_MUTEX_TRYLOCK_SCHED(mutex) \
132  _starpu_pthread_mutex_trylock_sched(mutex, __FILE__, __LINE__)
133 static STARPU_INLINE
134 int _starpu_pthread_mutex_trylock_sched(starpu_pthread_mutex_t *mutex, char *file, int line)
135 {
136  int p_ret = starpu_pthread_mutex_trylock_sched(mutex);
137  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
138  fprintf(stderr,
139  "%s:%d starpu_pthread_mutex_trylock_sched: %s\n",
140  file, line, strerror(p_ret));
141  STARPU_ABORT();
142  }
143  return p_ret;
144 }
145 
146 #define STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
147  _STARPU_CHECK_NOT_SCHED_MUTEX(mutex, __FILE__, __LINE__); \
148  int p_ret = starpu_pthread_mutex_unlock(mutex); \
149  if (STARPU_UNLIKELY(p_ret)) { \
150  fprintf(stderr, \
151  "%s:%d starpu_pthread_mutex_unlock: %s\n", \
152  __FILE__, __LINE__, strerror(p_ret)); \
153  STARPU_ABORT(); \
154  } \
155 } while (0)
156 
157 #define STARPU_PTHREAD_MUTEX_UNLOCK_SCHED(mutex) do { \
158  int p_ret = starpu_pthread_mutex_unlock_sched(mutex); \
159  if (STARPU_UNLIKELY(p_ret)) { \
160  fprintf(stderr, \
161  "%s:%d starpu_pthread_mutex_unlock_sched: %s\n", \
162  __FILE__, __LINE__, strerror(p_ret)); \
163  STARPU_ABORT(); \
164  } \
165 } while (0)
166 
167 /*
168  * Encapsulation of the starpu_pthread_key_* functions.
169  */
170 #define STARPU_PTHREAD_KEY_CREATE(key, destr) do { \
171  int p_ret = starpu_pthread_key_create((key), (destr)); \
172  if (STARPU_UNLIKELY(p_ret != 0)) { \
173  fprintf(stderr, \
174  "%s:%d starpu_pthread_key_create: %s\n", \
175  __FILE__, __LINE__, strerror(p_ret)); \
176  } \
177 } while (0)
178 
179 #define STARPU_PTHREAD_KEY_DELETE(key) do { \
180  int p_ret = starpu_pthread_key_delete((key)); \
181  if (STARPU_UNLIKELY(p_ret != 0)) { \
182  fprintf(stderr, \
183  "%s:%d starpu_pthread_key_delete: %s\n", \
184  __FILE__, __LINE__, strerror(p_ret)); \
185  } \
186 } while (0)
187 
188 #define STARPU_PTHREAD_SETSPECIFIC(key, ptr) do { \
189  int p_ret = starpu_pthread_setspecific((key), (ptr)); \
190  if (STARPU_UNLIKELY(p_ret != 0)) { \
191  fprintf(stderr, \
192  "%s:%d starpu_pthread_setspecific: %s\n", \
193  __FILE__, __LINE__, strerror(p_ret)); \
194  }; \
195 } while (0)
196 
197 #define STARPU_PTHREAD_GETSPECIFIC(key) starpu_pthread_getspecific((key))
198 
199 /*
200  * Encapsulation of the starpu_pthread_rwlock_* functions.
201  */
202 #define STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
203  int p_ret = starpu_pthread_rwlock_init((rwlock), (attr)); \
204  if (STARPU_UNLIKELY(p_ret)) { \
205  fprintf(stderr, \
206  "%s:%d starpu_pthread_rwlock_init: %s\n", \
207  __FILE__, __LINE__, strerror(p_ret)); \
208  STARPU_ABORT(); \
209  } \
210 } while (0)
211 
212 #define STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
213  int p_ret = starpu_pthread_rwlock_rdlock(rwlock); \
214  if (STARPU_UNLIKELY(p_ret)) { \
215  fprintf(stderr, \
216  "%s:%d starpu_pthread_rwlock_rdlock: %s\n", \
217  __FILE__, __LINE__, strerror(p_ret)); \
218  STARPU_ABORT(); \
219  } \
220 } while (0)
221 
222 #define STARPU_PTHREAD_RWLOCK_TRYRDLOCK(rwlock) \
223  _starpu_pthread_rwlock_tryrdlock(rwlock, __FILE__, __LINE__)
224 static STARPU_INLINE
225 int _starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
226 {
227  int p_ret = starpu_pthread_rwlock_tryrdlock(rwlock);
228  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
229  fprintf(stderr,
230  "%s:%d starpu_pthread_rwlock_tryrdlock: %s\n",
231  file, line, strerror(p_ret));
232  STARPU_ABORT();
233  }
234  return p_ret;
235 }
236 
237 #define STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
238  int p_ret = starpu_pthread_rwlock_wrlock(rwlock); \
239  if (STARPU_UNLIKELY(p_ret)) { \
240  fprintf(stderr, \
241  "%s:%d starpu_pthread_rwlock_wrlock: %s\n", \
242  __FILE__, __LINE__, strerror(p_ret)); \
243  STARPU_ABORT(); \
244  } \
245 } while (0)
246 
247 #define STARPU_PTHREAD_RWLOCK_TRYWRLOCK(rwlock) \
248  _starpu_pthread_rwlock_trywrlock(rwlock, __FILE__, __LINE__)
249 static STARPU_INLINE
250 int _starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
251 {
252  int p_ret = starpu_pthread_rwlock_trywrlock(rwlock);
253  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
254  fprintf(stderr,
255  "%s:%d starpu_pthread_rwlock_trywrlock: %s\n",
256  file, line, strerror(p_ret));
257  STARPU_ABORT();
258  }
259  return p_ret;
260 }
261 
262 #define STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
263  int p_ret = starpu_pthread_rwlock_unlock(rwlock); \
264  if (STARPU_UNLIKELY(p_ret)) { \
265  fprintf(stderr, \
266  "%s:%d starpu_pthread_rwlock_unlock: %s\n", \
267  __FILE__, __LINE__, strerror(p_ret)); \
268  STARPU_ABORT(); \
269  } \
270 } while (0)
271 
272 #define STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
273  int p_ret = starpu_pthread_rwlock_destroy(rwlock); \
274  if (STARPU_UNLIKELY(p_ret)) { \
275  fprintf(stderr, \
276  "%s:%d starpu_pthread_rwlock_destroy: %s\n", \
277  __FILE__, __LINE__, strerror(p_ret)); \
278  STARPU_ABORT(); \
279  } \
280 } while (0)
281 
282 /*
283  * Encapsulation of the starpu_pthread_cond_* functions.
284  */
285 #define STARPU_PTHREAD_COND_INIT(cond, attr) do { \
286  int p_ret = starpu_pthread_cond_init((cond), (attr)); \
287  if (STARPU_UNLIKELY(p_ret)) { \
288  fprintf(stderr, \
289  "%s:%d starpu_pthread_cond_init: %s\n", \
290  __FILE__, __LINE__, strerror(p_ret)); \
291  STARPU_ABORT(); \
292  } \
293 } while (0)
294 
295 #define STARPU_PTHREAD_COND_DESTROY(cond) do { \
296  int p_ret = starpu_pthread_cond_destroy(cond); \
297  if (STARPU_UNLIKELY(p_ret)) { \
298  fprintf(stderr, \
299  "%s:%d starpu_pthread_cond_destroy: %s\n", \
300  __FILE__, __LINE__, strerror(p_ret)); \
301  STARPU_ABORT(); \
302  } \
303 } while (0)
304 
305 #define STARPU_PTHREAD_COND_SIGNAL(cond) do { \
306  int p_ret = starpu_pthread_cond_signal(cond); \
307  if (STARPU_UNLIKELY(p_ret)) { \
308  fprintf(stderr, \
309  "%s:%d starpu_pthread_cond_signal: %s\n", \
310  __FILE__, __LINE__, strerror(p_ret)); \
311  STARPU_ABORT(); \
312  } \
313 } while (0)
314 
315 #define STARPU_PTHREAD_COND_BROADCAST(cond) do { \
316  int p_ret = starpu_pthread_cond_broadcast(cond); \
317  if (STARPU_UNLIKELY(p_ret)) { \
318  fprintf(stderr, \
319  "%s:%d starpu_pthread_cond_broadcast: %s\n", \
320  __FILE__, __LINE__, strerror(p_ret)); \
321  STARPU_ABORT(); \
322  } \
323 } while (0)
324 
325 #define STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
326  int p_ret = starpu_pthread_cond_wait((cond), (mutex)); \
327  if (STARPU_UNLIKELY(p_ret)) { \
328  fprintf(stderr, \
329  "%s:%d starpu_pthread_cond_wait: %s\n", \
330  __FILE__, __LINE__, strerror(p_ret)); \
331  STARPU_ABORT(); \
332  } \
333 } while (0)
334 
335 /* pthread_cond_timedwait not yet available on windows, but we don't run simgrid there anyway */
336 #ifdef STARPU_SIMGRID
337 #define STARPU_PTHREAD_COND_TIMEDWAIT(cond, mutex, abstime) \
338  _starpu_pthread_cond_timedwait(cond, mutex, abstime, __FILE__, __LINE__)
339 static STARPU_INLINE
340 int _starpu_pthread_cond_timedwait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex, const struct timespec *abstime, char *file, int line)
341 {
342  int p_ret = starpu_pthread_cond_timedwait(cond, mutex, abstime);
343  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != ETIMEDOUT)) {
344  fprintf(stderr,
345  "%s:%d starpu_pthread_cond_timedwait: %s\n",
346  file, line, strerror(p_ret));
347  STARPU_ABORT();
348  }
349  return p_ret;
350 }
351 #endif
352 
353 /*
354  * Encapsulation of the starpu_pthread_barrier_* functions.
355  */
356 
357 #define STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
358  int p_ret = starpu_pthread_barrier_init((barrier), (attr), (count)); \
359  if (STARPU_UNLIKELY(p_ret)) { \
360  fprintf(stderr, \
361  "%s:%d starpu_pthread_barrier_init: %s\n", \
362  __FILE__, __LINE__, strerror(p_ret)); \
363  STARPU_ABORT(); \
364  } \
365 } while (0)
366 
367 #define STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
368  int p_ret = starpu_pthread_barrier_destroy((barrier)); \
369  if (STARPU_UNLIKELY(p_ret)) { \
370  fprintf(stderr, \
371  "%s:%d starpu_pthread_barrier_destroy: %s\n", \
372  __FILE__, __LINE__, strerror(p_ret)); \
373  STARPU_ABORT(); \
374  } \
375 } while (0)
376 
377 #define STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
378  int p_ret = starpu_pthread_barrier_wait((barrier)); \
379  if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == STARPU_PTHREAD_BARRIER_SERIAL_THREAD)))) { \
380  fprintf(stderr, \
381  "%s:%d starpu_pthread_barrier_wait: %s\n", \
382  __FILE__, __LINE__, strerror(p_ret)); \
383  STARPU_ABORT(); \
384  } \
385 } while (0)
386 #endif /* _MSC_VER */
387 
388 #endif /* __STARPU_THREAD_UTIL_H__ */
starpu_pthread_rwlock_tryrdlock
int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
STARPU_UNLIKELY
#define STARPU_UNLIKELY(expr)
Definition: starpu_util.h:68
starpu_pthread_mutex_trylock
int starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex)
starpu_util.h
starpu_pthread_rwlock_trywrlock
int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
starpu_thread.h
STARPU_ABORT
#define STARPU_ABORT()
Definition: starpu_util.h:245
starpu_pthread_cond_timedwait
int starpu_pthread_cond_timedwait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex, const struct timespec *abstime)