thread.c 21 KB

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