thread.c 21 KB

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