thread.c 28 KB

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