thread.c 21 KB

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