thread.c 25 KB

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