thread.c 23 KB

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