StarPU Handbook
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
starpu_thread_util.h
Go to the documentation of this file.
1 /* StarPU --- Runtime system for heterogeneous multicore architectures.
2  *
3  * Copyright (C) 2010, 2012-2014 Université de Bordeaux 1
4  * Copyright (C) 2010, 2011, 2012, 2013 Centre National de la Recherche Scientifique
5  *
6  * StarPU is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or (at
9  * your option) any later version.
10  *
11  * StarPU is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  *
15  * See the GNU Lesser General Public License in COPYING.LGPL for more details.
16  */
17 
18 #ifndef __STARPU_THREAD_UTIL_H__
19 #define __STARPU_THREAD_UTIL_H__
20 
21 #include <starpu_util.h>
22 #include <errno.h>
23 
24 /*
25  * Encapsulation of the starpu_pthread_create_* functions.
26  */
27 
28 #define STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, where) do { \
29  int p_ret = starpu_pthread_create_on((name), (thread), (attr), (routine), (arg), (where)); \
30  if (STARPU_UNLIKELY(p_ret != 0)) { \
31  fprintf(stderr, \
32  "%s:%d starpu_pthread_create_on: %s\n", \
33  __FILE__, __LINE__, strerror(p_ret)); \
34  STARPU_ABORT(); \
35  } \
36 } while (0)
37 
38 #define STARPU_PTHREAD_CREATE(thread, attr, routine, arg) do { \
39  int p_ret = starpu_pthread_create((thread), (attr), (routine), (arg)); \
40  if (STARPU_UNLIKELY(p_ret != 0)) { \
41  fprintf(stderr, \
42  "%s:%d starpu_pthread_create: %s\n", \
43  __FILE__, __LINE__, strerror(p_ret)); \
44  STARPU_ABORT(); \
45  } \
46 } while (0)
47 
48 /*
49  * Encapsulation of the starpu_pthread_mutex_* functions.
50  */
51 
52 #define STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
53  int p_ret = starpu_pthread_mutex_init((mutex), (attr)); \
54  if (STARPU_UNLIKELY(p_ret)) { \
55  fprintf(stderr, \
56  "%s:%d starpu_pthread_mutex_init: %s\n", \
57  __FILE__, __LINE__, strerror(p_ret)); \
58  STARPU_ABORT(); \
59  } \
60 } while (0)
61 
62 #define STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
63  int p_ret = starpu_pthread_mutex_destroy(mutex); \
64  if (STARPU_UNLIKELY(p_ret)) { \
65  fprintf(stderr, \
66  "%s:%d starpu_pthread_mutex_destroy: %s\n", \
67  __FILE__, __LINE__, strerror(p_ret)); \
68  STARPU_ABORT(); \
69  } \
70 } while(0)
71 
72 #define STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
73  int p_ret = starpu_pthread_mutex_lock(mutex); \
74  if (STARPU_UNLIKELY(p_ret)) { \
75  fprintf(stderr, \
76  "%s:%d starpu_pthread_mutex_lock: %s\n", \
77  __FILE__, __LINE__, strerror(p_ret)); \
78  STARPU_ABORT(); \
79  } \
80 } while (0)
81 
82 #define STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) \
83  _STARPU_PTHREAD_MUTEX_TRYLOCK(mutex, __FILE__, __LINE__)
84 static STARPU_INLINE
85 int _STARPU_PTHREAD_MUTEX_TRYLOCK(starpu_pthread_mutex_t *mutex, char *file, int line)
86 {
87  int p_ret = starpu_pthread_mutex_trylock(mutex);
88  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
89  fprintf(stderr,
90  "%s:%d starpu_pthread_mutex_trylock: %s\n",
91  file, line, strerror(p_ret));
92  STARPU_ABORT();
93  }
94  return p_ret;
95 }
96 
97 #define STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
98  int p_ret = starpu_pthread_mutex_unlock(mutex); \
99  if (STARPU_UNLIKELY(p_ret)) { \
100  fprintf(stderr, \
101  "%s:%d starpu_pthread_mutex_unlock: %s\n", \
102  __FILE__, __LINE__, strerror(p_ret)); \
103  STARPU_ABORT(); \
104  } \
105 } while (0)
106 
107 /*
108  * Encapsulation of the starpu_pthread_key_* functions.
109  */
110 #define STARPU_PTHREAD_KEY_CREATE(key, destr) do { \
111  int p_ret = starpu_pthread_key_create((key), (destr)); \
112  if (STARPU_UNLIKELY(p_ret != 0)) { \
113  fprintf(stderr, \
114  "%s:%d starpu_pthread_key_create: %s\n", \
115  __FILE__, __LINE__, strerror(p_ret)); \
116  } \
117 } while (0)
118 
119 #define STARPU_PTHREAD_KEY_DELETE(key) do { \
120  int p_ret = starpu_pthread_key_delete((key)); \
121  if (STARPU_UNLIKELY(p_ret != 0)) { \
122  fprintf(stderr, \
123  "%s:%d starpu_pthread_key_delete: %s\n", \
124  __FILE__, __LINE__, strerror(p_ret)); \
125  } \
126 } while (0)
127 
128 #define STARPU_PTHREAD_SETSPECIFIC(key, ptr) do { \
129  int p_ret = starpu_pthread_setspecific((key), (ptr)); \
130  if (STARPU_UNLIKELY(p_ret != 0)) { \
131  fprintf(stderr, \
132  "%s:%d starpu_pthread_setspecific: %s\n", \
133  __FILE__, __LINE__, strerror(p_ret)); \
134  }; \
135 } while (0)
136 
137 #define STARPU_PTHREAD_GETSPECIFIC(key) starpu_pthread_getspecific((key))
138 
139 /*
140  * Encapsulation of the starpu_pthread_rwlock_* functions.
141  */
142 #define STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
143  int p_ret = starpu_pthread_rwlock_init((rwlock), (attr)); \
144  if (STARPU_UNLIKELY(p_ret)) { \
145  fprintf(stderr, \
146  "%s:%d starpu_pthread_rwlock_init: %s\n", \
147  __FILE__, __LINE__, strerror(p_ret)); \
148  STARPU_ABORT(); \
149  } \
150 } while (0)
151 
152 #define STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
153  int p_ret = starpu_pthread_rwlock_rdlock(rwlock); \
154  if (STARPU_UNLIKELY(p_ret)) { \
155  fprintf(stderr, \
156  "%s:%d starpu_pthread_rwlock_rdlock: %s\n", \
157  __FILE__, __LINE__, strerror(p_ret)); \
158  STARPU_ABORT(); \
159  } \
160 } while (0)
161 
162 #define STARPU_PTHREAD_RWLOCK_TRYRDLOCK(rwlock) \
163  _starpu_pthread_rwlock_tryrdlock(rwlock, __FILE__, __LINE__)
164 static STARPU_INLINE
165 int _starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
166 {
167  int p_ret = starpu_pthread_rwlock_tryrdlock(rwlock);
168  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
169  fprintf(stderr,
170  "%s:%d starpu_pthread_rwlock_tryrdlock: %s\n",
171  file, line, strerror(p_ret));
172  STARPU_ABORT();
173  }
174  return p_ret;
175 }
176 
177 #define STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
178  int p_ret = starpu_pthread_rwlock_wrlock(rwlock); \
179  if (STARPU_UNLIKELY(p_ret)) { \
180  fprintf(stderr, \
181  "%s:%d starpu_pthread_rwlock_wrlock: %s\n", \
182  __FILE__, __LINE__, strerror(p_ret)); \
183  STARPU_ABORT(); \
184  } \
185 } while (0)
186 
187 #define STARPU_PTHREAD_RWLOCK_TRYWRLOCK(rwlock) \
188  _starpu_pthread_rwlock_trywrlock(rwlock, __FILE__, __LINE__)
189 static STARPU_INLINE
190 int _starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
191 {
192  int p_ret = starpu_pthread_rwlock_trywrlock(rwlock);
193  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
194  fprintf(stderr,
195  "%s:%d starpu_pthread_rwlock_trywrlock: %s\n",
196  file, line, strerror(p_ret));
197  STARPU_ABORT();
198  }
199  return p_ret;
200 }
201 
202 #define STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
203  int p_ret = starpu_pthread_rwlock_unlock(rwlock); \
204  if (STARPU_UNLIKELY(p_ret)) { \
205  fprintf(stderr, \
206  "%s:%d starpu_pthread_rwlock_unlock: %s\n", \
207  __FILE__, __LINE__, strerror(p_ret)); \
208  STARPU_ABORT(); \
209  } \
210 } while (0)
211 
212 #define STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
213  int p_ret = starpu_pthread_rwlock_destroy(rwlock); \
214  if (STARPU_UNLIKELY(p_ret)) { \
215  fprintf(stderr, \
216  "%s:%d starpu_pthread_rwlock_destroy: %s\n", \
217  __FILE__, __LINE__, strerror(p_ret)); \
218  STARPU_ABORT(); \
219  } \
220 } while (0)
221 
222 /*
223  * Encapsulation of the starpu_pthread_cond_* functions.
224  */
225 #define STARPU_PTHREAD_COND_INIT(cond, attr) do { \
226  int p_ret = starpu_pthread_cond_init((cond), (attr)); \
227  if (STARPU_UNLIKELY(p_ret)) { \
228  fprintf(stderr, \
229  "%s:%d starpu_pthread_cond_init: %s\n", \
230  __FILE__, __LINE__, strerror(p_ret)); \
231  STARPU_ABORT(); \
232  } \
233 } while (0)
234 
235 #define STARPU_PTHREAD_COND_DESTROY(cond) do { \
236  int p_ret = starpu_pthread_cond_destroy(cond); \
237  if (STARPU_UNLIKELY(p_ret)) { \
238  fprintf(stderr, \
239  "%s:%d starpu_pthread_cond_destroy: %s\n", \
240  __FILE__, __LINE__, strerror(p_ret)); \
241  STARPU_ABORT(); \
242  } \
243 } while (0)
244 
245 #define STARPU_PTHREAD_COND_SIGNAL(cond) do { \
246  int p_ret = starpu_pthread_cond_signal(cond); \
247  if (STARPU_UNLIKELY(p_ret)) { \
248  fprintf(stderr, \
249  "%s:%d starpu_pthread_cond_signal: %s\n", \
250  __FILE__, __LINE__, strerror(p_ret)); \
251  STARPU_ABORT(); \
252  } \
253 } while (0)
254 
255 #define STARPU_PTHREAD_COND_BROADCAST(cond) do { \
256  int p_ret = starpu_pthread_cond_broadcast(cond); \
257  if (STARPU_UNLIKELY(p_ret)) { \
258  fprintf(stderr, \
259  "%s:%d starpu_pthread_cond_broadcast: %s\n", \
260  __FILE__, __LINE__, strerror(p_ret)); \
261  STARPU_ABORT(); \
262  } \
263 } while (0)
264 
265 #define STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
266  int p_ret = starpu_pthread_cond_wait((cond), (mutex)); \
267  if (STARPU_UNLIKELY(p_ret)) { \
268  fprintf(stderr, \
269  "%s:%d starpu_pthread_cond_wait: %s\n", \
270  __FILE__, __LINE__, strerror(p_ret)); \
271  STARPU_ABORT(); \
272  } \
273 } while (0)
274 
275 /*
276  * Encapsulation of the starpu_pthread_barrier_* functions.
277  */
278 
279 #define STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
280  int p_ret = starpu_pthread_barrier_init((barrier), (attr), (count)); \
281  if (STARPU_UNLIKELY(p_ret)) { \
282  fprintf(stderr, \
283  "%s:%d starpu_pthread_barrier_init: %s\n", \
284  __FILE__, __LINE__, strerror(p_ret)); \
285  STARPU_ABORT(); \
286  } \
287 } while (0)
288 
289 #define STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
290  int p_ret = starpu_pthread_barrier_destroy((barrier)); \
291  if (STARPU_UNLIKELY(p_ret)) { \
292  fprintf(stderr, \
293  "%s:%d starpu_pthread_barrier_destroy: %s\n", \
294  __FILE__, __LINE__, strerror(p_ret)); \
295  STARPU_ABORT(); \
296  } \
297 } while (0)
298 
299 #define STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
300  int p_ret = starpu_pthread_barrier_wait((barrier)); \
301  if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == STARPU_PTHREAD_BARRIER_SERIAL_THREAD)))) { \
302  fprintf(stderr, \
303  "%s:%d starpu_pthread_barrier_wait: %s\n", \
304  __FILE__, __LINE__, strerror(p_ret)); \
305  STARPU_ABORT(); \
306  } \
307 } while (0)
308 
309 #endif /* __STARPU_THREAD_UTIL_H__ */