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
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 #if !(defined(_MSC_VER) && !defined(BUILDING_STARPU))
25 /*
26  * Encapsulation of the starpu_pthread_create_* functions.
27  */
28 
29 #define STARPU_PTHREAD_CREATE_ON(name, thread, attr, routine, arg, where) do { \
30  int p_ret = starpu_pthread_create_on((name), (thread), (attr), (routine), (arg), (where)); \
31  if (STARPU_UNLIKELY(p_ret != 0)) { \
32  fprintf(stderr, \
33  "%s:%d starpu_pthread_create_on: %s\n", \
34  __FILE__, __LINE__, strerror(p_ret)); \
35  STARPU_ABORT(); \
36  } \
37 } while (0)
38 
39 #define STARPU_PTHREAD_CREATE(thread, attr, routine, arg) do { \
40  int p_ret = starpu_pthread_create((thread), (attr), (routine), (arg)); \
41  if (STARPU_UNLIKELY(p_ret != 0)) { \
42  fprintf(stderr, \
43  "%s:%d starpu_pthread_create: %s\n", \
44  __FILE__, __LINE__, strerror(p_ret)); \
45  STARPU_ABORT(); \
46  } \
47 } while (0)
48 
49 /*
50  * Encapsulation of the starpu_pthread_mutex_* functions.
51  */
52 
53 #define STARPU_PTHREAD_MUTEX_INIT(mutex, attr) do { \
54  int p_ret = starpu_pthread_mutex_init((mutex), (attr)); \
55  if (STARPU_UNLIKELY(p_ret)) { \
56  fprintf(stderr, \
57  "%s:%d starpu_pthread_mutex_init: %s\n", \
58  __FILE__, __LINE__, strerror(p_ret)); \
59  STARPU_ABORT(); \
60  } \
61 } while (0)
62 
63 #define STARPU_PTHREAD_MUTEX_DESTROY(mutex) do { \
64  int p_ret = starpu_pthread_mutex_destroy(mutex); \
65  if (STARPU_UNLIKELY(p_ret)) { \
66  fprintf(stderr, \
67  "%s:%d starpu_pthread_mutex_destroy: %s\n", \
68  __FILE__, __LINE__, strerror(p_ret)); \
69  STARPU_ABORT(); \
70  } \
71 } while(0)
72 
73 #define STARPU_PTHREAD_MUTEX_LOCK(mutex) do { \
74  int p_ret = starpu_pthread_mutex_lock(mutex); \
75  if (STARPU_UNLIKELY(p_ret)) { \
76  fprintf(stderr, \
77  "%s:%d starpu_pthread_mutex_lock: %s\n", \
78  __FILE__, __LINE__, strerror(p_ret)); \
79  STARPU_ABORT(); \
80  } \
81 } while (0)
82 
83 #define STARPU_PTHREAD_MUTEX_TRYLOCK(mutex) \
84  _starpu_pthread_mutex_trylock(mutex, __FILE__, __LINE__)
85 static STARPU_INLINE
86 int _starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex, char *file, int line)
87 {
88  int p_ret = starpu_pthread_mutex_trylock(mutex);
89  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
90  fprintf(stderr,
91  "%s:%d starpu_pthread_mutex_trylock: %s\n",
92  file, line, strerror(p_ret));
93  STARPU_ABORT();
94  }
95  return p_ret;
96 }
97 
98 #define STARPU_PTHREAD_MUTEX_UNLOCK(mutex) do { \
99  int p_ret = starpu_pthread_mutex_unlock(mutex); \
100  if (STARPU_UNLIKELY(p_ret)) { \
101  fprintf(stderr, \
102  "%s:%d starpu_pthread_mutex_unlock: %s\n", \
103  __FILE__, __LINE__, strerror(p_ret)); \
104  STARPU_ABORT(); \
105  } \
106 } while (0)
107 
108 /*
109  * Encapsulation of the starpu_pthread_key_* functions.
110  */
111 #define STARPU_PTHREAD_KEY_CREATE(key, destr) do { \
112  int p_ret = starpu_pthread_key_create((key), (destr)); \
113  if (STARPU_UNLIKELY(p_ret != 0)) { \
114  fprintf(stderr, \
115  "%s:%d starpu_pthread_key_create: %s\n", \
116  __FILE__, __LINE__, strerror(p_ret)); \
117  } \
118 } while (0)
119 
120 #define STARPU_PTHREAD_KEY_DELETE(key) do { \
121  int p_ret = starpu_pthread_key_delete((key)); \
122  if (STARPU_UNLIKELY(p_ret != 0)) { \
123  fprintf(stderr, \
124  "%s:%d starpu_pthread_key_delete: %s\n", \
125  __FILE__, __LINE__, strerror(p_ret)); \
126  } \
127 } while (0)
128 
129 #define STARPU_PTHREAD_SETSPECIFIC(key, ptr) do { \
130  int p_ret = starpu_pthread_setspecific((key), (ptr)); \
131  if (STARPU_UNLIKELY(p_ret != 0)) { \
132  fprintf(stderr, \
133  "%s:%d starpu_pthread_setspecific: %s\n", \
134  __FILE__, __LINE__, strerror(p_ret)); \
135  }; \
136 } while (0)
137 
138 #define STARPU_PTHREAD_GETSPECIFIC(key) starpu_pthread_getspecific((key))
139 
140 /*
141  * Encapsulation of the starpu_pthread_rwlock_* functions.
142  */
143 #define STARPU_PTHREAD_RWLOCK_INIT(rwlock, attr) do { \
144  int p_ret = starpu_pthread_rwlock_init((rwlock), (attr)); \
145  if (STARPU_UNLIKELY(p_ret)) { \
146  fprintf(stderr, \
147  "%s:%d starpu_pthread_rwlock_init: %s\n", \
148  __FILE__, __LINE__, strerror(p_ret)); \
149  STARPU_ABORT(); \
150  } \
151 } while (0)
152 
153 #define STARPU_PTHREAD_RWLOCK_RDLOCK(rwlock) do { \
154  int p_ret = starpu_pthread_rwlock_rdlock(rwlock); \
155  if (STARPU_UNLIKELY(p_ret)) { \
156  fprintf(stderr, \
157  "%s:%d starpu_pthread_rwlock_rdlock: %s\n", \
158  __FILE__, __LINE__, strerror(p_ret)); \
159  STARPU_ABORT(); \
160  } \
161 } while (0)
162 
163 #define STARPU_PTHREAD_RWLOCK_TRYRDLOCK(rwlock) \
164  _starpu_pthread_rwlock_tryrdlock(rwlock, __FILE__, __LINE__)
165 static STARPU_INLINE
166 int _starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
167 {
168  int p_ret = starpu_pthread_rwlock_tryrdlock(rwlock);
169  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
170  fprintf(stderr,
171  "%s:%d starpu_pthread_rwlock_tryrdlock: %s\n",
172  file, line, strerror(p_ret));
173  STARPU_ABORT();
174  }
175  return p_ret;
176 }
177 
178 #define STARPU_PTHREAD_RWLOCK_WRLOCK(rwlock) do { \
179  int p_ret = starpu_pthread_rwlock_wrlock(rwlock); \
180  if (STARPU_UNLIKELY(p_ret)) { \
181  fprintf(stderr, \
182  "%s:%d starpu_pthread_rwlock_wrlock: %s\n", \
183  __FILE__, __LINE__, strerror(p_ret)); \
184  STARPU_ABORT(); \
185  } \
186 } while (0)
187 
188 #define STARPU_PTHREAD_RWLOCK_TRYWRLOCK(rwlock) \
189  _starpu_pthread_rwlock_trywrlock(rwlock, __FILE__, __LINE__)
190 static STARPU_INLINE
191 int _starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock, char *file, int line)
192 {
193  int p_ret = starpu_pthread_rwlock_trywrlock(rwlock);
194  if (STARPU_UNLIKELY(p_ret != 0 && p_ret != EBUSY)) {
195  fprintf(stderr,
196  "%s:%d starpu_pthread_rwlock_trywrlock: %s\n",
197  file, line, strerror(p_ret));
198  STARPU_ABORT();
199  }
200  return p_ret;
201 }
202 
203 #define STARPU_PTHREAD_RWLOCK_UNLOCK(rwlock) do { \
204  int p_ret = starpu_pthread_rwlock_unlock(rwlock); \
205  if (STARPU_UNLIKELY(p_ret)) { \
206  fprintf(stderr, \
207  "%s:%d starpu_pthread_rwlock_unlock: %s\n", \
208  __FILE__, __LINE__, strerror(p_ret)); \
209  STARPU_ABORT(); \
210  } \
211 } while (0)
212 
213 #define STARPU_PTHREAD_RWLOCK_DESTROY(rwlock) do { \
214  int p_ret = starpu_pthread_rwlock_destroy(rwlock); \
215  if (STARPU_UNLIKELY(p_ret)) { \
216  fprintf(stderr, \
217  "%s:%d starpu_pthread_rwlock_destroy: %s\n", \
218  __FILE__, __LINE__, strerror(p_ret)); \
219  STARPU_ABORT(); \
220  } \
221 } while (0)
222 
223 /*
224  * Encapsulation of the starpu_pthread_cond_* functions.
225  */
226 #define STARPU_PTHREAD_COND_INIT(cond, attr) do { \
227  int p_ret = starpu_pthread_cond_init((cond), (attr)); \
228  if (STARPU_UNLIKELY(p_ret)) { \
229  fprintf(stderr, \
230  "%s:%d starpu_pthread_cond_init: %s\n", \
231  __FILE__, __LINE__, strerror(p_ret)); \
232  STARPU_ABORT(); \
233  } \
234 } while (0)
235 
236 #define STARPU_PTHREAD_COND_DESTROY(cond) do { \
237  int p_ret = starpu_pthread_cond_destroy(cond); \
238  if (STARPU_UNLIKELY(p_ret)) { \
239  fprintf(stderr, \
240  "%s:%d starpu_pthread_cond_destroy: %s\n", \
241  __FILE__, __LINE__, strerror(p_ret)); \
242  STARPU_ABORT(); \
243  } \
244 } while (0)
245 
246 #define STARPU_PTHREAD_COND_SIGNAL(cond) do { \
247  int p_ret = starpu_pthread_cond_signal(cond); \
248  if (STARPU_UNLIKELY(p_ret)) { \
249  fprintf(stderr, \
250  "%s:%d starpu_pthread_cond_signal: %s\n", \
251  __FILE__, __LINE__, strerror(p_ret)); \
252  STARPU_ABORT(); \
253  } \
254 } while (0)
255 
256 #define STARPU_PTHREAD_COND_BROADCAST(cond) do { \
257  int p_ret = starpu_pthread_cond_broadcast(cond); \
258  if (STARPU_UNLIKELY(p_ret)) { \
259  fprintf(stderr, \
260  "%s:%d starpu_pthread_cond_broadcast: %s\n", \
261  __FILE__, __LINE__, strerror(p_ret)); \
262  STARPU_ABORT(); \
263  } \
264 } while (0)
265 
266 #define STARPU_PTHREAD_COND_WAIT(cond, mutex) do { \
267  int p_ret = starpu_pthread_cond_wait((cond), (mutex)); \
268  if (STARPU_UNLIKELY(p_ret)) { \
269  fprintf(stderr, \
270  "%s:%d starpu_pthread_cond_wait: %s\n", \
271  __FILE__, __LINE__, strerror(p_ret)); \
272  STARPU_ABORT(); \
273  } \
274 } while (0)
275 
276 /*
277  * Encapsulation of the starpu_pthread_barrier_* functions.
278  */
279 
280 #define STARPU_PTHREAD_BARRIER_INIT(barrier, attr, count) do { \
281  int p_ret = starpu_pthread_barrier_init((barrier), (attr), (count)); \
282  if (STARPU_UNLIKELY(p_ret)) { \
283  fprintf(stderr, \
284  "%s:%d starpu_pthread_barrier_init: %s\n", \
285  __FILE__, __LINE__, strerror(p_ret)); \
286  STARPU_ABORT(); \
287  } \
288 } while (0)
289 
290 #define STARPU_PTHREAD_BARRIER_DESTROY(barrier) do { \
291  int p_ret = starpu_pthread_barrier_destroy((barrier)); \
292  if (STARPU_UNLIKELY(p_ret)) { \
293  fprintf(stderr, \
294  "%s:%d starpu_pthread_barrier_destroy: %s\n", \
295  __FILE__, __LINE__, strerror(p_ret)); \
296  STARPU_ABORT(); \
297  } \
298 } while (0)
299 
300 #define STARPU_PTHREAD_BARRIER_WAIT(barrier) do { \
301  int p_ret = starpu_pthread_barrier_wait((barrier)); \
302  if (STARPU_UNLIKELY(!((p_ret == 0) || (p_ret == STARPU_PTHREAD_BARRIER_SERIAL_THREAD)))) { \
303  fprintf(stderr, \
304  "%s:%d starpu_pthread_barrier_wait: %s\n", \
305  __FILE__, __LINE__, strerror(p_ret)); \
306  STARPU_ABORT(); \
307  } \
308 } while (0)
309 #endif /* _MSC_VER */
310 
311 #endif /* __STARPU_THREAD_UTIL_H__ */