thread.c 22 KB

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