thread.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 CNRS
  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. #include <starpu.h>
  18. #include <core/simgrid.h>
  19. #include <core/workers.h>
  20. #include <limits.h>
  21. #ifdef STARPU_SIMGRID
  22. #ifdef STARPU_HAVE_XBT_SYNCHRO_H
  23. #include <xbt/synchro.h>
  24. #else
  25. #include <xbt/synchro_core.h>
  26. #endif
  27. #include <smpi/smpi.h>
  28. #else
  29. #if defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  30. #include <linux/futex.h>
  31. #include <sys/syscall.h>
  32. /* Private futexes are not so old, cope with old kernels. */
  33. #ifdef FUTEX_WAIT_PRIVATE
  34. static int _starpu_futex_wait = FUTEX_WAIT_PRIVATE;
  35. static int _starpu_futex_wake = FUTEX_WAKE_PRIVATE;
  36. #else
  37. static int _starpu_futex_wait = FUTEX_WAIT;
  38. static int _starpu_futex_wake = FUTEX_WAKE;
  39. #endif
  40. #endif
  41. #endif /* !STARPU_SIMGRID */
  42. #ifdef STARPU_SIMGRID
  43. extern int _starpu_simgrid_thread_start(int argc, char *argv[]);
  44. int starpu_pthread_equal(starpu_pthread_t t1, starpu_pthread_t t2)
  45. {
  46. return t1 == t2;
  47. }
  48. starpu_pthread_t starpu_pthread_self(void)
  49. {
  50. return MSG_process_self();
  51. }
  52. int starpu_pthread_create_on(char *name, starpu_pthread_t *thread, const starpu_pthread_attr_t *attr STARPU_ATTRIBUTE_UNUSED, void *(*start_routine) (void *), void *arg, msg_host_t host)
  53. {
  54. char **_args;
  55. _STARPU_MALLOC(_args, 3*sizeof(char*));
  56. asprintf(&_args[0], "%p", start_routine);
  57. asprintf(&_args[1], "%p", arg);
  58. _args[2] = NULL;
  59. if (!host)
  60. host = MSG_get_host_by_name("MAIN");
  61. void *tsd;
  62. _STARPU_CALLOC(tsd, MAX_TSD+1, sizeof(void*));
  63. *thread = MSG_process_create_with_arguments(name, _starpu_simgrid_thread_start, tsd, host, 2, _args);
  64. return 0;
  65. }
  66. int starpu_pthread_create(starpu_pthread_t *thread, const starpu_pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
  67. {
  68. return starpu_pthread_create_on("", thread, attr, start_routine, arg, NULL);
  69. }
  70. int starpu_pthread_join(starpu_pthread_t thread STARPU_ATTRIBUTE_UNUSED, void **retval STARPU_ATTRIBUTE_UNUSED)
  71. {
  72. #if SIMGRID_VERSION_MAJOR > 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR >= 14)
  73. MSG_process_join(thread, 1000000);
  74. #else
  75. MSG_process_sleep(1);
  76. #endif
  77. return 0;
  78. }
  79. int starpu_pthread_exit(void *retval STARPU_ATTRIBUTE_UNUSED)
  80. {
  81. MSG_process_kill(MSG_process_self());
  82. STARPU_ABORT_MSG("MSG_process_kill(MSG_process_self()) returned?!");
  83. }
  84. int starpu_pthread_attr_init(starpu_pthread_attr_t *attr STARPU_ATTRIBUTE_UNUSED)
  85. {
  86. return 0;
  87. }
  88. int starpu_pthread_attr_destroy(starpu_pthread_attr_t *attr STARPU_ATTRIBUTE_UNUSED)
  89. {
  90. return 0;
  91. }
  92. int starpu_pthread_attr_setdetachstate(starpu_pthread_attr_t *attr STARPU_ATTRIBUTE_UNUSED, int detachstate STARPU_ATTRIBUTE_UNUSED)
  93. {
  94. return 0;
  95. }
  96. int starpu_pthread_mutex_init(starpu_pthread_mutex_t *mutex, const starpu_pthread_mutexattr_t *mutexattr STARPU_ATTRIBUTE_UNUSED)
  97. {
  98. *mutex = xbt_mutex_init();
  99. return 0;
  100. }
  101. int starpu_pthread_mutex_destroy(starpu_pthread_mutex_t *mutex)
  102. {
  103. if (*mutex)
  104. xbt_mutex_destroy(*mutex);
  105. return 0;
  106. }
  107. int starpu_pthread_mutex_lock(starpu_pthread_mutex_t *mutex)
  108. {
  109. _STARPU_TRACE_LOCKING_MUTEX();
  110. /* Note: this is actually safe, because simgrid only preempts within
  111. * simgrid functions */
  112. if (!*mutex)
  113. {
  114. /* Here we may get preempted */
  115. xbt_mutex_t new_mutex = xbt_mutex_init();
  116. if (!*mutex)
  117. *mutex = new_mutex;
  118. else
  119. /* Somebody already initialized it while we were
  120. * calling xbt_mutex_init, this one is now useless */
  121. xbt_mutex_destroy(new_mutex);
  122. }
  123. xbt_mutex_acquire(*mutex);
  124. _STARPU_TRACE_MUTEX_LOCKED();
  125. return 0;
  126. }
  127. int starpu_pthread_mutex_unlock(starpu_pthread_mutex_t *mutex)
  128. {
  129. _STARPU_TRACE_UNLOCKING_MUTEX();
  130. xbt_mutex_release(*mutex);
  131. _STARPU_TRACE_MUTEX_UNLOCKED();
  132. return 0;
  133. }
  134. int starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex)
  135. {
  136. int ret;
  137. _STARPU_TRACE_TRYLOCK_MUTEX();
  138. #ifdef HAVE_XBT_MUTEX_TRY_ACQUIRE
  139. ret = xbt_mutex_try_acquire(*mutex);
  140. #else
  141. ret = simcall_mutex_trylock((smx_mutex_t)*mutex);
  142. #endif
  143. ret = ret ? 0 : EBUSY;
  144. _STARPU_TRACE_MUTEX_LOCKED();
  145. return ret;
  146. }
  147. int starpu_pthread_mutexattr_gettype(const starpu_pthread_mutexattr_t *attr STARPU_ATTRIBUTE_UNUSED, int *type STARPU_ATTRIBUTE_UNUSED)
  148. {
  149. return 0;
  150. }
  151. int starpu_pthread_mutexattr_settype(starpu_pthread_mutexattr_t *attr STARPU_ATTRIBUTE_UNUSED, int type STARPU_ATTRIBUTE_UNUSED)
  152. {
  153. return 0;
  154. }
  155. int starpu_pthread_mutexattr_destroy(starpu_pthread_mutexattr_t *attr STARPU_ATTRIBUTE_UNUSED)
  156. {
  157. return 0;
  158. }
  159. int starpu_pthread_mutexattr_init(starpu_pthread_mutexattr_t *attr STARPU_ATTRIBUTE_UNUSED)
  160. {
  161. return 0;
  162. }
  163. /* Indexed by key-1 */
  164. static int used_key[MAX_TSD];
  165. int starpu_pthread_key_create(starpu_pthread_key_t *key, void (*destr_function) (void *) STARPU_ATTRIBUTE_UNUSED)
  166. {
  167. unsigned i;
  168. /* Note: no synchronization here, we are actually monothreaded anyway. */
  169. for (i = 0; i < MAX_TSD; i++)
  170. {
  171. if (!used_key[i])
  172. {
  173. used_key[i] = 1;
  174. break;
  175. }
  176. }
  177. STARPU_ASSERT(i < MAX_TSD);
  178. /* key 0 is for process pointer argument */
  179. *key = i+1;
  180. return 0;
  181. }
  182. int starpu_pthread_key_delete(starpu_pthread_key_t key)
  183. {
  184. used_key[key-1] = 0;
  185. return 0;
  186. }
  187. int starpu_pthread_setspecific(starpu_pthread_key_t key, const void *pointer)
  188. {
  189. void **array;
  190. #ifdef STARPU_SIMGRID_HAVE_SIMIX_PROCESS_GET_CODE
  191. if ((SIMIX_process_get_code() == _starpu_mpi_simgrid_init) || (!strcmp(SIMIX_process_self_get_name(),"wait for mpi transfer")))
  192. /* Special-case the SMPI process */
  193. array = smpi_process_get_user_data();
  194. else
  195. #endif
  196. array = MSG_process_get_data(MSG_process_self());
  197. array[key] = (void*) pointer;
  198. return 0;
  199. }
  200. void* starpu_pthread_getspecific(starpu_pthread_key_t key)
  201. {
  202. void **array;
  203. #ifdef STARPU_SIMGRID_HAVE_SIMIX_PROCESS_GET_CODE
  204. if ((SIMIX_process_get_code() == _starpu_mpi_simgrid_init) || (!strcmp(SIMIX_process_self_get_name(),"wait for mpi transfer")))
  205. /* Special-case the SMPI process */
  206. array = smpi_process_get_user_data();
  207. else
  208. #endif
  209. array = MSG_process_get_data(MSG_process_self());
  210. if (!array)
  211. return NULL;
  212. return array[key];
  213. }
  214. int starpu_pthread_cond_init(starpu_pthread_cond_t *cond, starpu_pthread_condattr_t *cond_attr STARPU_ATTRIBUTE_UNUSED)
  215. {
  216. *cond = xbt_cond_init();
  217. return 0;
  218. }
  219. static void _starpu_pthread_cond_auto_init(starpu_pthread_cond_t *cond)
  220. {
  221. /* Note: this is actually safe, because simgrid only preempts within
  222. * simgrid functions */
  223. if (!*cond)
  224. {
  225. /* Here we may get preempted */
  226. xbt_cond_t new_cond = xbt_cond_init();
  227. if (!*cond)
  228. *cond = new_cond;
  229. else
  230. /* Somebody already initialized it while we were
  231. * calling xbt_cond_init, this one is now useless */
  232. xbt_cond_destroy(new_cond);
  233. }
  234. }
  235. int starpu_pthread_cond_signal(starpu_pthread_cond_t *cond)
  236. {
  237. _starpu_pthread_cond_auto_init(cond);
  238. xbt_cond_signal(*cond);
  239. return 0;
  240. }
  241. int starpu_pthread_cond_broadcast(starpu_pthread_cond_t *cond)
  242. {
  243. _starpu_pthread_cond_auto_init(cond);
  244. xbt_cond_broadcast(*cond);
  245. return 0;
  246. }
  247. int starpu_pthread_cond_wait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  248. {
  249. _STARPU_TRACE_COND_WAIT_BEGIN();
  250. _starpu_pthread_cond_auto_init(cond);
  251. xbt_cond_wait(*cond, *mutex);
  252. _STARPU_TRACE_COND_WAIT_END();
  253. return 0;
  254. }
  255. int starpu_pthread_cond_destroy(starpu_pthread_cond_t *cond)
  256. {
  257. if (*cond)
  258. xbt_cond_destroy(*cond);
  259. return 0;
  260. }
  261. int starpu_pthread_rwlock_init(starpu_pthread_rwlock_t *restrict rwlock, const starpu_pthread_rwlockattr_t *restrict attr STARPU_ATTRIBUTE_UNUSED)
  262. {
  263. return starpu_pthread_mutex_init(rwlock, NULL);
  264. }
  265. int starpu_pthread_rwlock_destroy(starpu_pthread_rwlock_t *rwlock)
  266. {
  267. return starpu_pthread_mutex_destroy(rwlock);
  268. }
  269. int starpu_pthread_rwlock_rdlock(starpu_pthread_rwlock_t *rwlock)
  270. {
  271. _STARPU_TRACE_RDLOCKING_RWLOCK();
  272. int p_ret = starpu_pthread_mutex_lock(rwlock);
  273. _STARPU_TRACE_RWLOCK_RDLOCKED();
  274. return p_ret;
  275. }
  276. int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
  277. {
  278. int p_ret = starpu_pthread_mutex_trylock(rwlock);
  279. if (!p_ret)
  280. _STARPU_TRACE_RWLOCK_RDLOCKED();
  281. return p_ret;
  282. }
  283. int starpu_pthread_rwlock_wrlock(starpu_pthread_rwlock_t *rwlock)
  284. {
  285. _STARPU_TRACE_WRLOCKING_RWLOCK();
  286. int p_ret = starpu_pthread_mutex_lock(rwlock);
  287. _STARPU_TRACE_RWLOCK_WRLOCKED();
  288. return p_ret;
  289. }
  290. int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
  291. {
  292. int p_ret = starpu_pthread_mutex_trylock(rwlock);
  293. if (!p_ret)
  294. _STARPU_TRACE_RWLOCK_RDLOCKED();
  295. return p_ret;
  296. }
  297. int starpu_pthread_rwlock_unlock(starpu_pthread_rwlock_t *rwlock)
  298. {
  299. _STARPU_TRACE_UNLOCKING_RWLOCK();
  300. int p_ret = starpu_pthread_mutex_unlock(rwlock);
  301. _STARPU_TRACE_RWLOCK_UNLOCKED();
  302. return p_ret;
  303. }
  304. #if defined(STARPU_SIMGRID_HAVE_XBT_BARRIER_INIT)
  305. int starpu_pthread_barrier_init(starpu_pthread_barrier_t *restrict barrier, const starpu_pthread_barrierattr_t *restrict attr STARPU_ATTRIBUTE_UNUSED, unsigned count)
  306. {
  307. *barrier = xbt_barrier_init(count);
  308. return 0;
  309. }
  310. int starpu_pthread_barrier_destroy(starpu_pthread_barrier_t *barrier)
  311. {
  312. if (*barrier)
  313. xbt_barrier_destroy(*barrier);
  314. return 0;
  315. }
  316. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  317. {
  318. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  319. xbt_barrier_wait(*barrier);
  320. _STARPU_TRACE_BARRIER_WAIT_END();
  321. return 0;
  322. }
  323. #endif /* defined(STARPU_SIMGRID_HAVE_XBT_BARRIER_INIT) */
  324. int starpu_pthread_queue_init(starpu_pthread_queue_t *q)
  325. {
  326. STARPU_PTHREAD_MUTEX_INIT(&q->mutex, NULL);
  327. q->queue = NULL;
  328. q->allocqueue = 0;
  329. q->nqueue = 0;
  330. return 0;
  331. }
  332. int starpu_pthread_wait_init(starpu_pthread_wait_t *w)
  333. {
  334. STARPU_PTHREAD_MUTEX_INIT(&w->mutex, NULL);
  335. STARPU_PTHREAD_COND_INIT(&w->cond, NULL);
  336. w->block = 1;
  337. return 0;
  338. }
  339. int starpu_pthread_queue_register(starpu_pthread_wait_t *w, starpu_pthread_queue_t *q)
  340. {
  341. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  342. if (q->nqueue == q->allocqueue)
  343. {
  344. /* Make room for the new waiter */
  345. unsigned newalloc;
  346. newalloc = q->allocqueue * 2;
  347. if (!newalloc)
  348. newalloc = 1;
  349. _STARPU_REALLOC(q->queue, newalloc * sizeof(*(q->queue)));
  350. q->allocqueue = newalloc;
  351. }
  352. q->queue[q->nqueue++] = w;
  353. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  354. return 0;
  355. }
  356. int starpu_pthread_queue_unregister(starpu_pthread_wait_t *w, starpu_pthread_queue_t *q)
  357. {
  358. unsigned i;
  359. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  360. for (i = 0; i < q->nqueue; i++)
  361. {
  362. if (q->queue[i] == w)
  363. {
  364. memmove(&q->queue[i], &q->queue[i+1], (q->nqueue - i - 1) * sizeof(*(q->queue)));
  365. break;
  366. }
  367. }
  368. STARPU_ASSERT(i < q->nqueue);
  369. q->nqueue--;
  370. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  371. return 0;
  372. }
  373. int starpu_pthread_wait_reset(starpu_pthread_wait_t *w)
  374. {
  375. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  376. w->block = 1;
  377. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  378. return 0;
  379. }
  380. int starpu_pthread_wait_wait(starpu_pthread_wait_t *w)
  381. {
  382. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  383. while (w->block == 1)
  384. STARPU_PTHREAD_COND_WAIT(&w->cond, &w->mutex);
  385. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  386. return 0;
  387. }
  388. int starpu_pthread_queue_signal(starpu_pthread_queue_t *q)
  389. {
  390. starpu_pthread_wait_t *w;
  391. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  392. if (q->nqueue)
  393. {
  394. /* TODO: better try to wake a sleeping one if possible */
  395. w = q->queue[0];
  396. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  397. w->block = 0;
  398. STARPU_PTHREAD_COND_SIGNAL(&w->cond);
  399. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  400. }
  401. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  402. return 0;
  403. }
  404. int starpu_pthread_queue_broadcast(starpu_pthread_queue_t *q)
  405. {
  406. unsigned i;
  407. starpu_pthread_wait_t *w;
  408. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  409. for (i = 0; i < q->nqueue; i++)
  410. {
  411. w = q->queue[i];
  412. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  413. w->block = 0;
  414. STARPU_PTHREAD_COND_SIGNAL(&w->cond);
  415. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  416. }
  417. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  418. return 0;
  419. }
  420. int starpu_pthread_wait_destroy(starpu_pthread_wait_t *w)
  421. {
  422. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  423. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  424. STARPU_PTHREAD_MUTEX_DESTROY(&w->mutex);
  425. STARPU_PTHREAD_COND_DESTROY(&w->cond);
  426. return 0;
  427. }
  428. int starpu_pthread_queue_destroy(starpu_pthread_queue_t *q)
  429. {
  430. STARPU_ASSERT(!q->nqueue);
  431. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  432. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  433. STARPU_PTHREAD_MUTEX_DESTROY(&q->mutex);
  434. free(q->queue);
  435. return 0;
  436. }
  437. #endif /* STARPU_SIMGRID */
  438. #if (defined(STARPU_SIMGRID) && !defined(STARPU_SIMGRID_HAVE_XBT_BARRIER_INIT)) || (!defined(STARPU_SIMGRID) && !defined(STARPU_HAVE_PTHREAD_BARRIER))
  439. int starpu_pthread_barrier_init(starpu_pthread_barrier_t *restrict barrier, const starpu_pthread_barrierattr_t *restrict attr, unsigned count)
  440. {
  441. int ret = starpu_pthread_mutex_init(&barrier->mutex, NULL);
  442. if (!ret)
  443. ret = starpu_pthread_cond_init(&barrier->cond, NULL);
  444. if (!ret)
  445. ret = starpu_pthread_cond_init(&barrier->cond_destroy, NULL);
  446. barrier->count = count;
  447. barrier->done = 0;
  448. barrier->busy = 0;
  449. return ret;
  450. }
  451. int starpu_pthread_barrier_destroy(starpu_pthread_barrier_t *barrier)
  452. {
  453. starpu_pthread_mutex_lock(&barrier->mutex);
  454. while (barrier->busy)
  455. {
  456. starpu_pthread_cond_wait(&barrier->cond_destroy, &barrier->mutex);
  457. }
  458. starpu_pthread_mutex_unlock(&barrier->mutex);
  459. int ret = starpu_pthread_mutex_destroy(&barrier->mutex);
  460. if (!ret)
  461. ret = starpu_pthread_cond_destroy(&barrier->cond);
  462. if (!ret)
  463. ret = starpu_pthread_cond_destroy(&barrier->cond_destroy);
  464. return ret;
  465. }
  466. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  467. {
  468. int ret = 0;
  469. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  470. starpu_pthread_mutex_lock(&barrier->mutex);
  471. barrier->done++;
  472. if (barrier->done == barrier->count)
  473. {
  474. barrier->done = 0;
  475. starpu_pthread_cond_broadcast(&barrier->cond);
  476. ret = STARPU_PTHREAD_BARRIER_SERIAL_THREAD;
  477. }
  478. else
  479. {
  480. barrier->busy++;
  481. starpu_pthread_cond_wait(&barrier->cond, &barrier->mutex);
  482. barrier->busy--;
  483. starpu_pthread_cond_broadcast(&barrier->cond_destroy);
  484. }
  485. starpu_pthread_mutex_unlock(&barrier->mutex);
  486. _STARPU_TRACE_BARRIER_WAIT_END();
  487. return ret;
  488. }
  489. #endif /* defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_BARRIER) */
  490. #ifdef STARPU_FXT_LOCK_TRACES
  491. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) /* !STARPU_SIMGRID */
  492. int starpu_pthread_mutex_lock(starpu_pthread_mutex_t *mutex)
  493. {
  494. _STARPU_TRACE_LOCKING_MUTEX();
  495. int p_ret = pthread_mutex_lock(mutex);
  496. _STARPU_TRACE_MUTEX_LOCKED();
  497. return p_ret;
  498. }
  499. int starpu_pthread_mutex_unlock(starpu_pthread_mutex_t *mutex)
  500. {
  501. _STARPU_TRACE_UNLOCKING_MUTEX();
  502. int p_ret = pthread_mutex_unlock(mutex);
  503. _STARPU_TRACE_MUTEX_UNLOCKED();
  504. return p_ret;
  505. }
  506. int starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex)
  507. {
  508. int ret;
  509. _STARPU_TRACE_TRYLOCK_MUTEX();
  510. ret = pthread_mutex_trylock(mutex);
  511. if (!ret)
  512. _STARPU_TRACE_MUTEX_LOCKED();
  513. return ret;
  514. }
  515. int starpu_pthread_cond_wait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  516. {
  517. _STARPU_TRACE_COND_WAIT_BEGIN();
  518. int p_ret = pthread_cond_wait(cond, mutex);
  519. _STARPU_TRACE_COND_WAIT_END();
  520. return p_ret;
  521. }
  522. int starpu_pthread_rwlock_rdlock(starpu_pthread_rwlock_t *rwlock)
  523. {
  524. _STARPU_TRACE_RDLOCKING_RWLOCK();
  525. int p_ret = pthread_rwlock_rdlock(rwlock);
  526. _STARPU_TRACE_RWLOCK_RDLOCKED();
  527. return p_ret;
  528. }
  529. int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
  530. {
  531. _STARPU_TRACE_RDLOCKING_RWLOCK();
  532. int p_ret = pthread_rwlock_tryrdlock(rwlock);
  533. if (!p_ret)
  534. _STARPU_TRACE_RWLOCK_RDLOCKED();
  535. return p_ret;
  536. }
  537. int starpu_pthread_rwlock_wrlock(starpu_pthread_rwlock_t *rwlock)
  538. {
  539. _STARPU_TRACE_WRLOCKING_RWLOCK();
  540. int p_ret = pthread_rwlock_wrlock(rwlock);
  541. _STARPU_TRACE_RWLOCK_WRLOCKED();
  542. return p_ret;
  543. }
  544. int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
  545. {
  546. _STARPU_TRACE_WRLOCKING_RWLOCK();
  547. int p_ret = pthread_rwlock_trywrlock(rwlock);
  548. if (!p_ret)
  549. _STARPU_TRACE_RWLOCK_WRLOCKED();
  550. return p_ret;
  551. }
  552. int starpu_pthread_rwlock_unlock(starpu_pthread_rwlock_t *rwlock)
  553. {
  554. _STARPU_TRACE_UNLOCKING_RWLOCK();
  555. int p_ret = pthread_rwlock_unlock(rwlock);
  556. _STARPU_TRACE_RWLOCK_UNLOCKED();
  557. return p_ret;
  558. }
  559. #endif /* !defined(STARPU_SIMGRID) && !defined(_MSC_VER) */
  560. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) && defined(STARPU_HAVE_PTHREAD_BARRIER)
  561. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  562. {
  563. int ret;
  564. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  565. ret = pthread_barrier_wait(barrier);
  566. _STARPU_TRACE_BARRIER_WAIT_END();
  567. return ret;
  568. }
  569. #endif /* STARPU_SIMGRID, _MSC_VER, STARPU_HAVE_PTHREAD_BARRIER */
  570. #endif /* STARPU_FXT_LOCK_TRACES */
  571. /* "sched" variants, to be used (through the STARPU_PTHREAD_MUTEX_*LOCK_SCHED
  572. * macros of course) which record when the mutex is held or not */
  573. int starpu_pthread_mutex_lock_sched(starpu_pthread_mutex_t *mutex)
  574. {
  575. return starpu_pthread_mutex_lock(mutex);
  576. }
  577. int starpu_pthread_mutex_unlock_sched(starpu_pthread_mutex_t *mutex)
  578. {
  579. return starpu_pthread_mutex_unlock(mutex);
  580. }
  581. int starpu_pthread_mutex_trylock_sched(starpu_pthread_mutex_t *mutex)
  582. {
  583. return starpu_pthread_mutex_trylock(mutex);
  584. }
  585. #ifdef STARPU_DEBUG
  586. void starpu_pthread_mutex_check_sched(starpu_pthread_mutex_t *mutex, char *file, int line)
  587. {
  588. int workerid = starpu_worker_get_id();
  589. STARPU_ASSERT_MSG(workerid == -1 || !_starpu_worker_mutex_is_sched_mutex(workerid, mutex), "%s:%d is locking/unlocking a sched mutex but not using STARPU_PTHREAD_MUTEX_LOCK_SCHED", file, line);
  590. }
  591. #endif
  592. #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(HAVE_PTHREAD_SPIN_LOCK)
  593. #undef starpu_pthread_spin_init
  594. int starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared)
  595. {
  596. return _starpu_pthread_spin_init(lock, pshared);
  597. }
  598. #undef starpu_pthread_spin_destroy
  599. int starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  600. {
  601. return _starpu_pthread_spin_destroy(lock);
  602. }
  603. #undef starpu_pthread_spin_lock
  604. int starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
  605. {
  606. return _starpu_pthread_spin_lock(lock);
  607. }
  608. #endif
  609. #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)
  610. #if !defined(STARPU_SIMGRID) && defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  611. int _starpu_pthread_spin_do_lock(starpu_pthread_spinlock_t *lock)
  612. {
  613. if (STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0)
  614. /* Got it on first try! */
  615. return 0;
  616. /* Busy, spin a bit. */
  617. unsigned i;
  618. for (i = 0; i < 128; i++)
  619. {
  620. /* Pause a bit before retrying */
  621. STARPU_UYIELD();
  622. /* And synchronize with other threads */
  623. STARPU_SYNCHRONIZE();
  624. if (!lock->taken)
  625. /* Holder released it, try again */
  626. if (STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0)
  627. /* Got it! */
  628. return 0;
  629. }
  630. /* We have spent enough time with spinning, let's block */
  631. /* This avoids typical 10ms pauses when the application thread tries to submit tasks. */
  632. while (1)
  633. {
  634. /* Tell releaser to wake us */
  635. unsigned prev = starpu_xchg(&lock->taken, 2);
  636. if (prev == 0)
  637. /* Ah, it just got released and we actually acquired
  638. * it!
  639. * Note: the sad thing is that we have just written 2,
  640. * so will spuriously try to wake a thread on unlock,
  641. * but we can not avoid it since we do not know whether
  642. * there are other threads sleeping or not.
  643. */
  644. return 0;
  645. /* Now start sleeping (unless it was released in between)
  646. * We are sure to get woken because either
  647. * - some thread has not released the lock yet, and lock->taken
  648. * is 2, so it will wake us.
  649. * - some other thread started blocking, and will set
  650. * lock->taken back to 2
  651. */
  652. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wait, 2, NULL, NULL, 0))
  653. if (errno == ENOSYS)
  654. _starpu_futex_wait = FUTEX_WAIT;
  655. }
  656. }
  657. #endif
  658. #undef starpu_pthread_spin_trylock
  659. int starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
  660. {
  661. return _starpu_pthread_spin_trylock(lock);
  662. }
  663. #undef starpu_pthread_spin_unlock
  664. int starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
  665. {
  666. return _starpu_pthread_spin_unlock(lock);
  667. }
  668. #if !defined(STARPU_SIMGRID) && defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  669. void _starpu_pthread_spin_do_unlock(starpu_pthread_spinlock_t *lock)
  670. {
  671. /*
  672. * Somebody to wake. Clear 'taken' and wake him.
  673. * Note that he may not be sleeping yet, but if he is not, we won't
  674. * since the value of 'taken' will have changed.
  675. */
  676. lock->taken = 0;
  677. STARPU_SYNCHRONIZE();
  678. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wake, 1, NULL, NULL, 0) == -1)
  679. switch (errno)
  680. {
  681. case ENOSYS:
  682. _starpu_futex_wake = FUTEX_WAKE;
  683. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wake, 1, NULL, NULL, 0) == -1)
  684. STARPU_ASSERT_MSG(0, "futex(wake) returned %d!", errno);
  685. break;
  686. case 0:
  687. break;
  688. default:
  689. STARPU_ASSERT_MSG(0, "futex returned %d!", errno);
  690. break;
  691. }
  692. }
  693. #endif
  694. #endif /* defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK) */