thread.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2013,2015,2017 Inria
  4. * Copyright (C) 2010-2017 CNRS
  5. * Copyright (C) 2010,2012-2018 Université de Bordeaux
  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 !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. #if SIMGRID_VERSION_MAJOR > 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR >= 18)
  293. struct timespec now, delta;
  294. double delay;
  295. int ret = 0;
  296. _starpu_clock_gettime(&now);
  297. delta.tv_sec = abstime->tv_sec - now.tv_sec;
  298. delta.tv_nsec = abstime->tv_nsec - now.tv_nsec;
  299. delay = (double) delta.tv_sec + (double) delta.tv_nsec / 1000000000.;
  300. _STARPU_TRACE_COND_WAIT_BEGIN();
  301. _starpu_pthread_cond_auto_init(cond);
  302. ret = xbt_cond_timedwait(*cond, *mutex, delay) ? ETIMEDOUT : 0;
  303. _STARPU_TRACE_COND_WAIT_END();
  304. return ret;
  305. #else
  306. STARPU_ASSERT_MSG(0, "simgrid version is too old for this");
  307. #endif
  308. }
  309. int starpu_pthread_cond_destroy(starpu_pthread_cond_t *cond)
  310. {
  311. if (*cond)
  312. xbt_cond_destroy(*cond);
  313. return 0;
  314. }
  315. int starpu_pthread_rwlock_init(starpu_pthread_rwlock_t *restrict rwlock, const starpu_pthread_rwlockattr_t *restrict attr STARPU_ATTRIBUTE_UNUSED)
  316. {
  317. return starpu_pthread_mutex_init(rwlock, NULL);
  318. }
  319. int starpu_pthread_rwlock_destroy(starpu_pthread_rwlock_t *rwlock)
  320. {
  321. return starpu_pthread_mutex_destroy(rwlock);
  322. }
  323. int starpu_pthread_rwlock_rdlock(starpu_pthread_rwlock_t *rwlock)
  324. {
  325. _STARPU_TRACE_RDLOCKING_RWLOCK();
  326. int p_ret = starpu_pthread_mutex_lock(rwlock);
  327. _STARPU_TRACE_RWLOCK_RDLOCKED();
  328. return p_ret;
  329. }
  330. int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
  331. {
  332. int p_ret = starpu_pthread_mutex_trylock(rwlock);
  333. if (!p_ret)
  334. _STARPU_TRACE_RWLOCK_RDLOCKED();
  335. return p_ret;
  336. }
  337. int starpu_pthread_rwlock_wrlock(starpu_pthread_rwlock_t *rwlock)
  338. {
  339. _STARPU_TRACE_WRLOCKING_RWLOCK();
  340. int p_ret = starpu_pthread_mutex_lock(rwlock);
  341. _STARPU_TRACE_RWLOCK_WRLOCKED();
  342. return p_ret;
  343. }
  344. int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
  345. {
  346. int p_ret = starpu_pthread_mutex_trylock(rwlock);
  347. if (!p_ret)
  348. _STARPU_TRACE_RWLOCK_RDLOCKED();
  349. return p_ret;
  350. }
  351. int starpu_pthread_rwlock_unlock(starpu_pthread_rwlock_t *rwlock)
  352. {
  353. _STARPU_TRACE_UNLOCKING_RWLOCK();
  354. int p_ret = starpu_pthread_mutex_unlock(rwlock);
  355. _STARPU_TRACE_RWLOCK_UNLOCKED();
  356. return p_ret;
  357. }
  358. #if defined(STARPU_SIMGRID_HAVE_XBT_BARRIER_INIT)
  359. int starpu_pthread_barrier_init(starpu_pthread_barrier_t *restrict barrier, const starpu_pthread_barrierattr_t *restrict attr STARPU_ATTRIBUTE_UNUSED, unsigned count)
  360. {
  361. *barrier = xbt_barrier_init(count);
  362. return 0;
  363. }
  364. int starpu_pthread_barrier_destroy(starpu_pthread_barrier_t *barrier)
  365. {
  366. if (*barrier)
  367. xbt_barrier_destroy(*barrier);
  368. return 0;
  369. }
  370. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  371. {
  372. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  373. xbt_barrier_wait(*barrier);
  374. _STARPU_TRACE_BARRIER_WAIT_END();
  375. return 0;
  376. }
  377. #endif /* defined(STARPU_SIMGRID_HAVE_XBT_BARRIER_INIT) */
  378. int starpu_pthread_queue_init(starpu_pthread_queue_t *q)
  379. {
  380. STARPU_PTHREAD_MUTEX_INIT(&q->mutex, NULL);
  381. q->queue = NULL;
  382. q->allocqueue = 0;
  383. q->nqueue = 0;
  384. return 0;
  385. }
  386. int starpu_pthread_wait_init(starpu_pthread_wait_t *w)
  387. {
  388. STARPU_PTHREAD_MUTEX_INIT(&w->mutex, NULL);
  389. STARPU_PTHREAD_COND_INIT(&w->cond, NULL);
  390. w->block = 1;
  391. return 0;
  392. }
  393. int starpu_pthread_queue_register(starpu_pthread_wait_t *w, starpu_pthread_queue_t *q)
  394. {
  395. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  396. if (q->nqueue == q->allocqueue)
  397. {
  398. /* Make room for the new waiter */
  399. unsigned newalloc;
  400. newalloc = q->allocqueue * 2;
  401. if (!newalloc)
  402. newalloc = 1;
  403. _STARPU_REALLOC(q->queue, newalloc * sizeof(*(q->queue)));
  404. q->allocqueue = newalloc;
  405. }
  406. q->queue[q->nqueue++] = w;
  407. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  408. return 0;
  409. }
  410. int starpu_pthread_queue_unregister(starpu_pthread_wait_t *w, starpu_pthread_queue_t *q)
  411. {
  412. unsigned i;
  413. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  414. for (i = 0; i < q->nqueue; i++)
  415. {
  416. if (q->queue[i] == w)
  417. {
  418. memmove(&q->queue[i], &q->queue[i+1], (q->nqueue - i - 1) * sizeof(*(q->queue)));
  419. break;
  420. }
  421. }
  422. STARPU_ASSERT(i < q->nqueue);
  423. q->nqueue--;
  424. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  425. return 0;
  426. }
  427. int starpu_pthread_wait_reset(starpu_pthread_wait_t *w)
  428. {
  429. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  430. w->block = 1;
  431. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  432. return 0;
  433. }
  434. int starpu_pthread_wait_wait(starpu_pthread_wait_t *w)
  435. {
  436. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  437. while (w->block == 1)
  438. STARPU_PTHREAD_COND_WAIT(&w->cond, &w->mutex);
  439. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  440. return 0;
  441. }
  442. /* pthread_cond_timedwait not yet available on windows, but we don't run simgrid there anyway */
  443. #ifdef STARPU_SIMGRID
  444. int starpu_pthread_wait_timedwait(starpu_pthread_wait_t *w, const struct timespec *abstime)
  445. {
  446. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  447. while (w->block == 1)
  448. STARPU_PTHREAD_COND_TIMEDWAIT(&w->cond, &w->mutex, abstime);
  449. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  450. return 0;
  451. }
  452. #endif
  453. int starpu_pthread_queue_signal(starpu_pthread_queue_t *q)
  454. {
  455. starpu_pthread_wait_t *w;
  456. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  457. if (q->nqueue)
  458. {
  459. /* TODO: better try to wake a sleeping one if possible */
  460. w = q->queue[0];
  461. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  462. w->block = 0;
  463. STARPU_PTHREAD_COND_SIGNAL(&w->cond);
  464. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  465. }
  466. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  467. return 0;
  468. }
  469. int starpu_pthread_queue_broadcast(starpu_pthread_queue_t *q)
  470. {
  471. unsigned i;
  472. starpu_pthread_wait_t *w;
  473. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  474. for (i = 0; i < q->nqueue; i++)
  475. {
  476. w = q->queue[i];
  477. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  478. w->block = 0;
  479. STARPU_PTHREAD_COND_SIGNAL(&w->cond);
  480. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  481. }
  482. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  483. return 0;
  484. }
  485. int starpu_pthread_wait_destroy(starpu_pthread_wait_t *w)
  486. {
  487. STARPU_PTHREAD_MUTEX_LOCK(&w->mutex);
  488. STARPU_PTHREAD_MUTEX_UNLOCK(&w->mutex);
  489. STARPU_PTHREAD_MUTEX_DESTROY(&w->mutex);
  490. STARPU_PTHREAD_COND_DESTROY(&w->cond);
  491. return 0;
  492. }
  493. int starpu_pthread_queue_destroy(starpu_pthread_queue_t *q)
  494. {
  495. STARPU_ASSERT(!q->nqueue);
  496. STARPU_PTHREAD_MUTEX_LOCK(&q->mutex);
  497. STARPU_PTHREAD_MUTEX_UNLOCK(&q->mutex);
  498. STARPU_PTHREAD_MUTEX_DESTROY(&q->mutex);
  499. free(q->queue);
  500. return 0;
  501. }
  502. #endif /* STARPU_SIMGRID */
  503. #if (defined(STARPU_SIMGRID) && !defined(STARPU_SIMGRID_HAVE_XBT_BARRIER_INIT)) || (!defined(STARPU_SIMGRID) && !defined(STARPU_HAVE_PTHREAD_BARRIER))
  504. int starpu_pthread_barrier_init(starpu_pthread_barrier_t *restrict barrier, const starpu_pthread_barrierattr_t *restrict attr STARPU_ATTRIBUTE_UNUSED, unsigned count)
  505. {
  506. int ret = starpu_pthread_mutex_init(&barrier->mutex, NULL);
  507. if (!ret)
  508. ret = starpu_pthread_cond_init(&barrier->cond, NULL);
  509. if (!ret)
  510. ret = starpu_pthread_cond_init(&barrier->cond_destroy, NULL);
  511. barrier->count = count;
  512. barrier->done = 0;
  513. barrier->busy = 0;
  514. return ret;
  515. }
  516. int starpu_pthread_barrier_destroy(starpu_pthread_barrier_t *barrier)
  517. {
  518. starpu_pthread_mutex_lock(&barrier->mutex);
  519. while (barrier->busy)
  520. {
  521. starpu_pthread_cond_wait(&barrier->cond_destroy, &barrier->mutex);
  522. }
  523. starpu_pthread_mutex_unlock(&barrier->mutex);
  524. int ret = starpu_pthread_mutex_destroy(&barrier->mutex);
  525. if (!ret)
  526. ret = starpu_pthread_cond_destroy(&barrier->cond);
  527. if (!ret)
  528. ret = starpu_pthread_cond_destroy(&barrier->cond_destroy);
  529. return ret;
  530. }
  531. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  532. {
  533. int ret = 0;
  534. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  535. starpu_pthread_mutex_lock(&barrier->mutex);
  536. barrier->done++;
  537. if (barrier->done == barrier->count)
  538. {
  539. barrier->done = 0;
  540. starpu_pthread_cond_broadcast(&barrier->cond);
  541. ret = STARPU_PTHREAD_BARRIER_SERIAL_THREAD;
  542. }
  543. else
  544. {
  545. barrier->busy++;
  546. starpu_pthread_cond_wait(&barrier->cond, &barrier->mutex);
  547. barrier->busy--;
  548. starpu_pthread_cond_broadcast(&barrier->cond_destroy);
  549. }
  550. starpu_pthread_mutex_unlock(&barrier->mutex);
  551. _STARPU_TRACE_BARRIER_WAIT_END();
  552. return ret;
  553. }
  554. #endif /* defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_BARRIER) */
  555. #ifdef STARPU_FXT_LOCK_TRACES
  556. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) /* !STARPU_SIMGRID */
  557. int starpu_pthread_mutex_lock(starpu_pthread_mutex_t *mutex)
  558. {
  559. _STARPU_TRACE_LOCKING_MUTEX();
  560. int p_ret = pthread_mutex_lock(mutex);
  561. _STARPU_TRACE_MUTEX_LOCKED();
  562. return p_ret;
  563. }
  564. int starpu_pthread_mutex_unlock(starpu_pthread_mutex_t *mutex)
  565. {
  566. _STARPU_TRACE_UNLOCKING_MUTEX();
  567. int p_ret = pthread_mutex_unlock(mutex);
  568. _STARPU_TRACE_MUTEX_UNLOCKED();
  569. return p_ret;
  570. }
  571. int starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex)
  572. {
  573. int ret;
  574. _STARPU_TRACE_TRYLOCK_MUTEX();
  575. ret = pthread_mutex_trylock(mutex);
  576. if (!ret)
  577. _STARPU_TRACE_MUTEX_LOCKED();
  578. return ret;
  579. }
  580. int starpu_pthread_cond_wait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  581. {
  582. _STARPU_TRACE_COND_WAIT_BEGIN();
  583. int p_ret = pthread_cond_wait(cond, mutex);
  584. _STARPU_TRACE_COND_WAIT_END();
  585. return p_ret;
  586. }
  587. int starpu_pthread_rwlock_rdlock(starpu_pthread_rwlock_t *rwlock)
  588. {
  589. _STARPU_TRACE_RDLOCKING_RWLOCK();
  590. int p_ret = pthread_rwlock_rdlock(rwlock);
  591. _STARPU_TRACE_RWLOCK_RDLOCKED();
  592. return p_ret;
  593. }
  594. int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
  595. {
  596. _STARPU_TRACE_RDLOCKING_RWLOCK();
  597. int p_ret = pthread_rwlock_tryrdlock(rwlock);
  598. if (!p_ret)
  599. _STARPU_TRACE_RWLOCK_RDLOCKED();
  600. return p_ret;
  601. }
  602. int starpu_pthread_rwlock_wrlock(starpu_pthread_rwlock_t *rwlock)
  603. {
  604. _STARPU_TRACE_WRLOCKING_RWLOCK();
  605. int p_ret = pthread_rwlock_wrlock(rwlock);
  606. _STARPU_TRACE_RWLOCK_WRLOCKED();
  607. return p_ret;
  608. }
  609. int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
  610. {
  611. _STARPU_TRACE_WRLOCKING_RWLOCK();
  612. int p_ret = pthread_rwlock_trywrlock(rwlock);
  613. if (!p_ret)
  614. _STARPU_TRACE_RWLOCK_WRLOCKED();
  615. return p_ret;
  616. }
  617. int starpu_pthread_rwlock_unlock(starpu_pthread_rwlock_t *rwlock)
  618. {
  619. _STARPU_TRACE_UNLOCKING_RWLOCK();
  620. int p_ret = pthread_rwlock_unlock(rwlock);
  621. _STARPU_TRACE_RWLOCK_UNLOCKED();
  622. return p_ret;
  623. }
  624. #endif /* !defined(STARPU_SIMGRID) && !defined(_MSC_VER) */
  625. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) && defined(STARPU_HAVE_PTHREAD_BARRIER)
  626. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  627. {
  628. int ret;
  629. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  630. ret = pthread_barrier_wait(barrier);
  631. _STARPU_TRACE_BARRIER_WAIT_END();
  632. return ret;
  633. }
  634. #endif /* STARPU_SIMGRID, _MSC_VER, STARPU_HAVE_PTHREAD_BARRIER */
  635. #endif /* STARPU_FXT_LOCK_TRACES */
  636. /* "sched" variants, to be used (through the STARPU_PTHREAD_MUTEX_*LOCK_SCHED
  637. * macros of course) which record when the mutex is held or not */
  638. int starpu_pthread_mutex_lock_sched(starpu_pthread_mutex_t *mutex)
  639. {
  640. return starpu_pthread_mutex_lock(mutex);
  641. }
  642. int starpu_pthread_mutex_unlock_sched(starpu_pthread_mutex_t *mutex)
  643. {
  644. return starpu_pthread_mutex_unlock(mutex);
  645. }
  646. int starpu_pthread_mutex_trylock_sched(starpu_pthread_mutex_t *mutex)
  647. {
  648. return starpu_pthread_mutex_trylock(mutex);
  649. }
  650. #ifdef STARPU_DEBUG
  651. void starpu_pthread_mutex_check_sched(starpu_pthread_mutex_t *mutex, char *file, int line)
  652. {
  653. int workerid = starpu_worker_get_id();
  654. 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);
  655. }
  656. #endif
  657. #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(HAVE_PTHREAD_SPIN_LOCK)
  658. #undef starpu_pthread_spin_init
  659. int starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared)
  660. {
  661. return _starpu_pthread_spin_init(lock, pshared);
  662. }
  663. #undef starpu_pthread_spin_destroy
  664. int starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  665. {
  666. return _starpu_pthread_spin_destroy(lock);
  667. }
  668. #undef starpu_pthread_spin_lock
  669. int starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
  670. {
  671. return _starpu_pthread_spin_lock(lock);
  672. }
  673. #endif
  674. #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK)
  675. #if !defined(STARPU_SIMGRID) && defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  676. int _starpu_pthread_spin_do_lock(starpu_pthread_spinlock_t *lock)
  677. {
  678. if (STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0)
  679. /* Got it on first try! */
  680. return 0;
  681. /* Busy, spin a bit. */
  682. unsigned i;
  683. for (i = 0; i < 128; i++)
  684. {
  685. /* Pause a bit before retrying */
  686. STARPU_UYIELD();
  687. /* And synchronize with other threads */
  688. STARPU_SYNCHRONIZE();
  689. if (!lock->taken)
  690. /* Holder released it, try again */
  691. if (STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0)
  692. /* Got it! */
  693. return 0;
  694. }
  695. /* We have spent enough time with spinning, let's block */
  696. /* This avoids typical 10ms pauses when the application thread tries to submit tasks. */
  697. while (1)
  698. {
  699. /* Tell releaser to wake us */
  700. unsigned prev = starpu_xchg(&lock->taken, 2);
  701. if (prev == 0)
  702. /* Ah, it just got released and we actually acquired
  703. * it!
  704. * Note: the sad thing is that we have just written 2,
  705. * so will spuriously try to wake a thread on unlock,
  706. * but we can not avoid it since we do not know whether
  707. * there are other threads sleeping or not.
  708. */
  709. return 0;
  710. /* Now start sleeping (unless it was released in between)
  711. * We are sure to get woken because either
  712. * - some thread has not released the lock yet, and lock->taken
  713. * is 2, so it will wake us.
  714. * - some other thread started blocking, and will set
  715. * lock->taken back to 2
  716. */
  717. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wait, 2, NULL, NULL, 0))
  718. if (errno == ENOSYS)
  719. _starpu_futex_wait = FUTEX_WAIT;
  720. }
  721. }
  722. #endif
  723. #undef starpu_pthread_spin_trylock
  724. int starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
  725. {
  726. return _starpu_pthread_spin_trylock(lock);
  727. }
  728. #undef starpu_pthread_spin_unlock
  729. int starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
  730. {
  731. return _starpu_pthread_spin_unlock(lock);
  732. }
  733. #if !defined(STARPU_SIMGRID) && defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  734. void _starpu_pthread_spin_do_unlock(starpu_pthread_spinlock_t *lock)
  735. {
  736. /*
  737. * Somebody to wake. Clear 'taken' and wake him.
  738. * Note that he may not be sleeping yet, but if he is not, we won't
  739. * since the value of 'taken' will have changed.
  740. */
  741. lock->taken = 0;
  742. STARPU_SYNCHRONIZE();
  743. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wake, 1, NULL, NULL, 0) == -1)
  744. switch (errno)
  745. {
  746. case ENOSYS:
  747. _starpu_futex_wake = FUTEX_WAKE;
  748. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wake, 1, NULL, NULL, 0) == -1)
  749. STARPU_ASSERT_MSG(0, "futex(wake) returned %d!", errno);
  750. break;
  751. case 0:
  752. break;
  753. default:
  754. STARPU_ASSERT_MSG(0, "futex returned %d!", errno);
  755. break;
  756. }
  757. }
  758. #endif
  759. #endif /* defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(STARPU_HAVE_PTHREAD_SPIN_LOCK) */
  760. #ifdef STARPU_SIMGRID
  761. int starpu_sem_destroy(starpu_sem_t *sem)
  762. {
  763. MSG_sem_destroy(*sem);
  764. return 0;
  765. }
  766. int starpu_sem_init(starpu_sem_t *sem, int pshared, unsigned value)
  767. {
  768. STARPU_ASSERT_MSG(pshared == 0, "pshared semaphores not supported under simgrid");
  769. *sem = MSG_sem_init(value);
  770. return 0;
  771. }
  772. int starpu_sem_post(starpu_sem_t *sem)
  773. {
  774. MSG_sem_release(*sem);
  775. return 0;
  776. }
  777. int starpu_sem_wait(starpu_sem_t *sem)
  778. {
  779. MSG_sem_acquire(*sem);
  780. return 0;
  781. }
  782. int starpu_sem_trywait(starpu_sem_t *sem)
  783. {
  784. if (MSG_sem_would_block(*sem))
  785. return EAGAIN;
  786. starpu_sem_wait(sem);
  787. return 0;
  788. }
  789. int starpu_sem_getvalue(starpu_sem_t *sem, int *sval)
  790. {
  791. #if SIMGRID_VERSION_MAJOR > 3 || (SIMGRID_VERSION_MAJOR == 3 && SIMGRID_VERSION_MINOR > 13)
  792. *sval = MSG_sem_get_capacity(*sem);
  793. return 0;
  794. #else
  795. (void) sem;
  796. (void) sval;
  797. STARPU_ABORT_MSG("sigmrid up to 3.13 did not have working MSG_sem_get_capacity");
  798. #endif
  799. }
  800. #elif !defined(_MSC_VER) || defined(BUILDING_STARPU) /* !STARPU_SIMGRID */
  801. int starpu_sem_wait(starpu_sem_t *sem)
  802. {
  803. int ret;
  804. while((ret = sem_wait(sem)) == -1 && errno == EINTR)
  805. ;
  806. return ret;
  807. }
  808. int starpu_sem_trywait(starpu_sem_t *sem)
  809. {
  810. int ret;
  811. while((ret = sem_trywait(sem)) == -1 && errno == EINTR)
  812. ;
  813. return ret;
  814. }
  815. #endif