thread.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2015 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) */
  278. #endif /* STARPU_SIMGRID */
  279. #if (defined(STARPU_SIMGRID) && !defined(STARPU_SIMGRID_HAVE_XBT_BARRIER_INIT)) || !defined(STARPU_HAVE_PTHREAD_BARRIER)
  280. int starpu_pthread_barrier_init(starpu_pthread_barrier_t *restrict barrier, const starpu_pthread_barrierattr_t *restrict attr, unsigned count)
  281. {
  282. int ret = starpu_pthread_mutex_init(&barrier->mutex, NULL);
  283. if (!ret)
  284. ret = starpu_pthread_cond_init(&barrier->cond, NULL);
  285. if (!ret)
  286. ret = starpu_pthread_cond_init(&barrier->cond_destroy, NULL);
  287. barrier->count = count;
  288. barrier->done = 0;
  289. barrier->busy = 0;
  290. return ret;
  291. }
  292. int starpu_pthread_barrier_destroy(starpu_pthread_barrier_t *barrier)
  293. {
  294. starpu_pthread_mutex_lock(&barrier->mutex);
  295. while (barrier->busy) {
  296. starpu_pthread_cond_wait(&barrier->cond_destroy, &barrier->mutex);
  297. }
  298. starpu_pthread_mutex_unlock(&barrier->mutex);
  299. int ret = starpu_pthread_mutex_destroy(&barrier->mutex);
  300. if (!ret)
  301. ret = starpu_pthread_cond_destroy(&barrier->cond);
  302. if (!ret)
  303. ret = starpu_pthread_cond_destroy(&barrier->cond_destroy);
  304. return ret;
  305. }
  306. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  307. {
  308. int ret = 0;
  309. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  310. starpu_pthread_mutex_lock(&barrier->mutex);
  311. barrier->done++;
  312. if (barrier->done == barrier->count)
  313. {
  314. barrier->done = 0;
  315. starpu_pthread_cond_broadcast(&barrier->cond);
  316. ret = STARPU_PTHREAD_BARRIER_SERIAL_THREAD;
  317. }
  318. else
  319. {
  320. barrier->busy++;
  321. starpu_pthread_cond_wait(&barrier->cond, &barrier->mutex);
  322. barrier->busy--;
  323. starpu_pthread_cond_broadcast(&barrier->cond_destroy);
  324. }
  325. starpu_pthread_mutex_unlock(&barrier->mutex);
  326. _STARPU_TRACE_BARRIER_WAIT_END();
  327. return ret;
  328. }
  329. #endif /* defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_BARRIER) */
  330. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) /* !STARPU_SIMGRID */
  331. int starpu_pthread_mutex_lock(starpu_pthread_mutex_t *mutex)
  332. {
  333. _STARPU_TRACE_LOCKING_MUTEX();
  334. int p_ret = pthread_mutex_lock(mutex);
  335. int workerid = starpu_worker_get_id();
  336. if(workerid != -1 && _starpu_worker_mutex_is_sched_mutex(workerid, mutex))
  337. _starpu_worker_set_flag_sched_mutex_locked(workerid, 1);
  338. _STARPU_TRACE_MUTEX_LOCKED();
  339. return p_ret;
  340. }
  341. int starpu_pthread_mutex_unlock(starpu_pthread_mutex_t *mutex)
  342. {
  343. _STARPU_TRACE_UNLOCKING_MUTEX();
  344. int p_ret = pthread_mutex_unlock(mutex);
  345. int workerid = starpu_worker_get_id();
  346. if(workerid != -1 && _starpu_worker_mutex_is_sched_mutex(workerid, mutex))
  347. _starpu_worker_set_flag_sched_mutex_locked(workerid, 0);
  348. _STARPU_TRACE_MUTEX_UNLOCKED();
  349. return p_ret;
  350. }
  351. int starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex)
  352. {
  353. int ret;
  354. _STARPU_TRACE_TRYLOCK_MUTEX();
  355. ret = pthread_mutex_trylock(mutex);
  356. if (!ret)
  357. {
  358. int workerid = starpu_worker_get_id();
  359. if(workerid != -1 && _starpu_worker_mutex_is_sched_mutex(workerid, mutex))
  360. _starpu_worker_set_flag_sched_mutex_locked(workerid, 1);
  361. _STARPU_TRACE_MUTEX_LOCKED();
  362. }
  363. return ret;
  364. }
  365. int starpu_pthread_cond_wait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  366. {
  367. _STARPU_TRACE_COND_WAIT_BEGIN();
  368. int p_ret = pthread_cond_wait(cond, mutex);
  369. _STARPU_TRACE_COND_WAIT_END();
  370. return p_ret;
  371. }
  372. int starpu_pthread_rwlock_rdlock(starpu_pthread_rwlock_t *rwlock)
  373. {
  374. _STARPU_TRACE_RDLOCKING_RWLOCK();
  375. int p_ret = pthread_rwlock_rdlock(rwlock);
  376. _STARPU_TRACE_RWLOCK_RDLOCKED();
  377. return p_ret;
  378. }
  379. int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
  380. {
  381. _STARPU_TRACE_RDLOCKING_RWLOCK();
  382. int p_ret = pthread_rwlock_tryrdlock(rwlock);
  383. if (!p_ret)
  384. _STARPU_TRACE_RWLOCK_RDLOCKED();
  385. return p_ret;
  386. }
  387. int starpu_pthread_rwlock_wrlock(starpu_pthread_rwlock_t *rwlock)
  388. {
  389. _STARPU_TRACE_WRLOCKING_RWLOCK();
  390. int p_ret = pthread_rwlock_wrlock(rwlock);
  391. _STARPU_TRACE_RWLOCK_WRLOCKED();
  392. return p_ret;
  393. }
  394. int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
  395. {
  396. _STARPU_TRACE_WRLOCKING_RWLOCK();
  397. int p_ret = pthread_rwlock_trywrlock(rwlock);
  398. if (!p_ret)
  399. _STARPU_TRACE_RWLOCK_WRLOCKED();
  400. return p_ret;
  401. }
  402. int starpu_pthread_rwlock_unlock(starpu_pthread_rwlock_t *rwlock)
  403. {
  404. _STARPU_TRACE_UNLOCKING_RWLOCK();
  405. int p_ret = pthread_rwlock_unlock(rwlock);
  406. _STARPU_TRACE_RWLOCK_UNLOCKED();
  407. return p_ret;
  408. }
  409. #endif
  410. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) && defined(STARPU_HAVE_PTHREAD_BARRIER)
  411. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  412. {
  413. int ret;
  414. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  415. ret = pthread_barrier_wait(barrier);
  416. _STARPU_TRACE_BARRIER_WAIT_END();
  417. return ret;
  418. }
  419. #endif /* STARPU_SIMGRID, _MSC_VER, STARPU_HAVE_PTHREAD_BARRIER */
  420. #if defined(STARPU_SIMGRID) || (defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)) || !defined(HAVE_PTHREAD_SPIN_LOCK)
  421. int starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared STARPU_ATTRIBUTE_UNUSED)
  422. {
  423. lock->taken = 0;
  424. return 0;
  425. }
  426. int starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock STARPU_ATTRIBUTE_UNUSED)
  427. {
  428. /* we don't do anything */
  429. return 0;
  430. }
  431. int starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
  432. {
  433. #ifdef STARPU_SIMGRID
  434. while (1)
  435. {
  436. if (!lock->taken)
  437. {
  438. lock->taken = 1;
  439. return 0;
  440. }
  441. /* Give hand to another thread, hopefully the one which has the
  442. * spinlock and probably just has also a short-lived mutex. */
  443. MSG_process_sleep(0.000001);
  444. STARPU_UYIELD();
  445. }
  446. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  447. if (STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0)
  448. /* Got it on first try! */
  449. return 0;
  450. /* Busy, spin a bit. */
  451. unsigned i;
  452. for (i = 0; i < 128; i++)
  453. {
  454. /* Pause a bit before retrying */
  455. STARPU_UYIELD();
  456. /* And synchronize with other threads */
  457. STARPU_SYNCHRONIZE();
  458. if (!lock->taken)
  459. /* Holder released it, try again */
  460. if (STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1) == 0)
  461. /* Got it! */
  462. return 0;
  463. }
  464. /* We have spent enough time with spinning, let's block */
  465. while (1)
  466. {
  467. /* Tell releaser to wake us */
  468. unsigned prev = starpu_xchg(&lock->taken, 2);
  469. if (prev == 0)
  470. /* Ah, it just got released and we actually acquired
  471. * it!
  472. * Note: the sad thing is that we have just written 2,
  473. * so will spuriously try to wake a thread on unlock,
  474. * but we can not avoid it since we do not know whether
  475. * there are other threads sleeping or not.
  476. */
  477. return 0;
  478. /* Now start sleeping (unless it was released in between)
  479. * We are sure to get woken because either
  480. * - some thread has not released the lock yet, and lock->taken
  481. * is 2, so it will wake us.
  482. * - some other thread started blocking, and will set
  483. * lock->taken back to 2
  484. */
  485. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wait, 2, NULL, NULL, 0))
  486. if (errno == ENOSYS)
  487. _starpu_futex_wait = FUTEX_WAIT;
  488. }
  489. #else /* !SIMGRID && !LINUX */
  490. uint32_t prev;
  491. do
  492. {
  493. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  494. if (prev)
  495. STARPU_UYIELD();
  496. }
  497. while (prev);
  498. return 0;
  499. #endif
  500. }
  501. int starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
  502. {
  503. #ifdef STARPU_SIMGRID
  504. if (lock->taken)
  505. return EBUSY;
  506. lock->taken = 1;
  507. return 0;
  508. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  509. unsigned prev;
  510. prev = STARPU_VAL_COMPARE_AND_SWAP(&lock->taken, 0, 1);
  511. return (prev == 0)?0:EBUSY;
  512. #else /* !SIMGRID && !LINUX */
  513. uint32_t prev;
  514. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  515. return (prev == 0)?0:EBUSY;
  516. #endif
  517. }
  518. int starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
  519. {
  520. #ifdef STARPU_SIMGRID
  521. lock->taken = 0;
  522. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  523. STARPU_ASSERT(lock->taken != 0);
  524. unsigned next = STARPU_ATOMIC_ADD(&lock->taken, -1);
  525. if (next == 0)
  526. /* Nobody to wake, we are done */
  527. return 0;
  528. /*
  529. * Somebody to wake. Clear 'taken' and wake him.
  530. * Note that he may not be sleeping yet, but if he is not, we won't
  531. * since the value of 'taken' will have changed.
  532. */
  533. lock->taken = 0;
  534. STARPU_SYNCHRONIZE();
  535. if (syscall(SYS_futex, &lock->taken, _starpu_futex_wake, 1, NULL, NULL, 0))
  536. if (errno == ENOSYS)
  537. _starpu_futex_wake = FUTEX_WAKE;
  538. #else /* !SIMGRID && !LINUX */
  539. STARPU_RELEASE(&lock->taken);
  540. #endif
  541. return 0;
  542. }
  543. #endif /* defined(STARPU_SIMGRID) || !defined(HAVE_PTHREAD_SPIN_LOCK) */
  544. int _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock)
  545. {
  546. #ifdef STARPU_SIMGRID
  547. STARPU_ASSERT(lock->taken);
  548. return !lock->taken;
  549. #elif defined(STARPU_LINUX_SYS) && defined(STARPU_HAVE_XCHG)
  550. STARPU_ASSERT(lock->taken == 1 || lock->taken == 2);
  551. return lock->taken == 0;
  552. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  553. int ret = pthread_spin_trylock((pthread_spinlock_t *)lock);
  554. STARPU_ASSERT(ret != 0);
  555. return ret == 0;
  556. #else
  557. STARPU_ASSERT(lock->taken);
  558. return !lock->taken;
  559. #endif
  560. }