thread.c 24 KB

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