thread.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2010, 2012-2014 Université de Bordeaux 1
  4. * Copyright (C) 2010, 2011, 2012, 2013, 2014 Centre National de la Recherche Scientifique
  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. #endif
  23. #ifdef STARPU_SIMGRID
  24. extern int _starpu_simgrid_thread_start(int argc, char *argv[]);
  25. int starpu_pthread_create_on(char *name, starpu_pthread_t *thread, const starpu_pthread_attr_t *attr, void *(*start_routine) (void *), void *arg, int where)
  26. {
  27. struct _starpu_pthread_args *_args = malloc(sizeof(*_args));
  28. xbt_dynar_t _hosts;
  29. _args->f = start_routine;
  30. _args->arg = arg;
  31. _hosts = MSG_hosts_as_dynar();
  32. *thread = MSG_process_create(name, _starpu_simgrid_thread_start, _args,
  33. xbt_dynar_get_as(_hosts, (where), msg_host_t));
  34. xbt_dynar_free(&_hosts);
  35. return 0;
  36. }
  37. int starpu_pthread_create(starpu_pthread_t *thread, const starpu_pthread_attr_t *attr, void *(*start_routine) (void *), void *arg)
  38. {
  39. return starpu_pthread_create_on("", thread, attr, start_routine, arg, 0);
  40. }
  41. int starpu_pthread_join(starpu_pthread_t thread, void **retval)
  42. {
  43. #if 0 //def HAVE_MSG_PROCESS_JOIN
  44. MSG_process_join(thread, 100);
  45. #else
  46. MSG_process_sleep(1);
  47. #endif
  48. return 0;
  49. }
  50. int starpu_pthread_exit(void *retval)
  51. {
  52. MSG_process_kill(MSG_process_self());
  53. return 0;
  54. }
  55. int starpu_pthread_attr_init(starpu_pthread_attr_t *attr)
  56. {
  57. return 0;
  58. }
  59. int starpu_pthread_attr_destroy(starpu_pthread_attr_t *attr)
  60. {
  61. return 0;
  62. }
  63. int starpu_pthread_attr_setdetachstate(starpu_pthread_attr_t *attr, int detachstate)
  64. {
  65. return 0;
  66. }
  67. int starpu_pthread_mutex_init(starpu_pthread_mutex_t *mutex, const starpu_pthread_mutexattr_t *mutexattr)
  68. {
  69. *mutex = xbt_mutex_init();
  70. return 0;
  71. }
  72. int starpu_pthread_mutex_destroy(starpu_pthread_mutex_t *mutex)
  73. {
  74. if (*mutex)
  75. xbt_mutex_destroy(*mutex);
  76. return 0;
  77. }
  78. int starpu_pthread_mutex_lock(starpu_pthread_mutex_t *mutex)
  79. {
  80. _STARPU_TRACE_LOCKING_MUTEX();
  81. if (!*mutex) STARPU_PTHREAD_MUTEX_INIT(mutex, NULL);
  82. xbt_mutex_acquire(*mutex);
  83. _STARPU_TRACE_MUTEX_LOCKED();
  84. return 0;
  85. }
  86. int starpu_pthread_mutex_unlock(starpu_pthread_mutex_t *mutex)
  87. {
  88. _STARPU_TRACE_UNLOCKING_MUTEX();
  89. xbt_mutex_release(*mutex);
  90. _STARPU_TRACE_MUTEX_UNLOCKED();
  91. return 0;
  92. }
  93. int starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex)
  94. {
  95. _STARPU_TRACE_TRYLOCK_MUTEX();
  96. xbt_mutex_acquire(*mutex);
  97. _STARPU_TRACE_MUTEX_LOCKED();
  98. return 0;
  99. }
  100. int starpu_pthread_mutexattr_gettype(const starpu_pthread_mutexattr_t *attr, int *type)
  101. {
  102. return 0;
  103. }
  104. int starpu_pthread_mutexattr_settype(starpu_pthread_mutexattr_t *attr, int type)
  105. {
  106. return 0;
  107. }
  108. int starpu_pthread_mutexattr_destroy(starpu_pthread_mutexattr_t *attr)
  109. {
  110. return 0;
  111. }
  112. int starpu_pthread_mutexattr_init(starpu_pthread_mutexattr_t *attr)
  113. {
  114. return 0;
  115. }
  116. static int used_key[MAX_TSD];
  117. int starpu_pthread_key_create(starpu_pthread_key_t *key, void (*destr_function) (void *))
  118. {
  119. unsigned i;
  120. /* Note: no synchronization here, we are actually monothreaded anyway. */
  121. for (i = 0; i < MAX_TSD; i++)
  122. if (!used_key[i])
  123. {
  124. used_key[i] = 1;
  125. break;
  126. }
  127. STARPU_ASSERT(i < MAX_TSD);
  128. *key = i;
  129. return 0;
  130. }
  131. int starpu_pthread_key_delete(starpu_pthread_key_t key)
  132. {
  133. used_key[key] = 0;
  134. return 0;
  135. }
  136. int starpu_pthread_setspecific(starpu_pthread_key_t key, const void *pointer)
  137. {
  138. void **array = MSG_host_get_data(MSG_host_self());
  139. array[key] = pointer;
  140. return 0;
  141. }
  142. void* starpu_pthread_getspecific(starpu_pthread_key_t key)
  143. {
  144. void **array = MSG_host_get_data(MSG_host_self());
  145. return array[key];
  146. }
  147. int starpu_pthread_cond_init(starpu_pthread_cond_t *cond, starpu_pthread_condattr_t *cond_attr)
  148. {
  149. *cond = xbt_cond_init();
  150. return 0;
  151. }
  152. int starpu_pthread_cond_signal(starpu_pthread_cond_t *cond)
  153. {
  154. if (!*cond)
  155. STARPU_PTHREAD_COND_INIT(cond, NULL);
  156. xbt_cond_signal(*cond);
  157. return 0;
  158. }
  159. int starpu_pthread_cond_broadcast(starpu_pthread_cond_t *cond)
  160. {
  161. if (!*cond)
  162. STARPU_PTHREAD_COND_INIT(cond, NULL);
  163. xbt_cond_broadcast(*cond);
  164. return 0;
  165. }
  166. int starpu_pthread_cond_wait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  167. {
  168. _STARPU_TRACE_COND_WAIT_BEGIN();
  169. if (!*cond)
  170. STARPU_PTHREAD_COND_INIT(cond, NULL);
  171. xbt_cond_wait(*cond, *mutex);
  172. _STARPU_TRACE_COND_WAIT_END();
  173. return 0;
  174. }
  175. int starpu_pthread_cond_destroy(starpu_pthread_cond_t *cond)
  176. {
  177. if (*cond)
  178. xbt_cond_destroy(*cond);
  179. return 0;
  180. }
  181. int starpu_pthread_rwlock_init(starpu_pthread_rwlock_t *restrict rwlock, const starpu_pthread_rwlockattr_t *restrict attr)
  182. {
  183. return starpu_pthread_mutex_init(rwlock, NULL);
  184. }
  185. int starpu_pthread_rwlock_destroy(starpu_pthread_rwlock_t *rwlock)
  186. {
  187. return starpu_pthread_mutex_destroy(rwlock);
  188. }
  189. int starpu_pthread_rwlock_rdlock(starpu_pthread_rwlock_t *rwlock)
  190. {
  191. _STARPU_TRACE_RDLOCKING_RWLOCK();
  192. int p_ret = starpu_pthread_mutex_lock(rwlock);
  193. _STARPU_TRACE_RWLOCK_RDLOCKED();
  194. return p_ret;
  195. }
  196. int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
  197. {
  198. int p_ret = starpu_pthread_mutex_trylock(rwlock);
  199. if (!p_ret)
  200. _STARPU_TRACE_RWLOCK_RDLOCKED();
  201. return p_ret;
  202. }
  203. int starpu_pthread_rwlock_wrlock(starpu_pthread_rwlock_t *rwlock)
  204. {
  205. _STARPU_TRACE_WRLOCKING_RWLOCK();
  206. int p_ret = starpu_pthread_mutex_lock(rwlock);
  207. _STARPU_TRACE_RWLOCK_WRLOCKED();
  208. return p_ret;
  209. }
  210. int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
  211. {
  212. int p_ret = starpu_pthread_mutex_trylock(rwlock);
  213. if (!p_ret)
  214. _STARPU_TRACE_RWLOCK_RDLOCKED();
  215. return p_ret;
  216. }
  217. int starpu_pthread_rwlock_unlock(starpu_pthread_rwlock_t *rwlock)
  218. {
  219. _STARPU_TRACE_UNLOCKING_RWLOCK();
  220. int p_ret = starpu_pthread_mutex_unlock(rwlock);
  221. _STARPU_TRACE_RWLOCK_UNLOCKED();
  222. return p_ret;
  223. }
  224. #endif /* STARPU_SIMGRID */
  225. #if defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_BARRIER)
  226. int starpu_pthread_barrier_init(starpu_pthread_barrier_t *restrict barrier, const starpu_pthread_barrierattr_t *restrict attr, unsigned count)
  227. {
  228. int ret = starpu_pthread_mutex_init(&barrier->mutex, NULL);
  229. if (!ret)
  230. ret = starpu_pthread_cond_init(&barrier->cond, NULL);
  231. barrier->count = count;
  232. barrier->done = 0;
  233. return ret;
  234. }
  235. int starpu_pthread_barrier_destroy(starpu_pthread_barrier_t *barrier)
  236. {
  237. int ret = starpu_pthread_mutex_destroy(&barrier->mutex);
  238. if (!ret)
  239. ret = starpu_pthread_cond_destroy(&barrier->cond);
  240. return ret;
  241. }
  242. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  243. {
  244. int ret = 0;
  245. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  246. starpu_pthread_mutex_lock(&barrier->mutex);
  247. barrier->done++;
  248. if (barrier->done == barrier->count)
  249. {
  250. barrier->done = 0;
  251. starpu_pthread_cond_broadcast(&barrier->cond);
  252. ret = STARPU_PTHREAD_BARRIER_SERIAL_THREAD;
  253. } else {
  254. starpu_pthread_cond_wait(&barrier->cond, &barrier->mutex);
  255. }
  256. starpu_pthread_mutex_unlock(&barrier->mutex);
  257. _STARPU_TRACE_BARRIER_WAIT_END();
  258. return ret;
  259. }
  260. #endif /* defined(STARPU_SIMGRID) || !defined(STARPU_HAVE_PTHREAD_BARRIER) */
  261. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) /* !STARPU_SIMGRID */
  262. int starpu_pthread_mutex_lock(starpu_pthread_mutex_t *mutex)
  263. {
  264. _STARPU_TRACE_LOCKING_MUTEX();
  265. int p_ret = pthread_mutex_lock(mutex);
  266. int workerid = starpu_worker_get_id();
  267. if(workerid != -1 && _starpu_worker_mutex_is_sched_mutex(workerid, mutex))
  268. _starpu_worker_set_flag_sched_mutex_locked(workerid, 1);
  269. _STARPU_TRACE_MUTEX_LOCKED();
  270. return p_ret;
  271. }
  272. int starpu_pthread_mutex_unlock(starpu_pthread_mutex_t *mutex)
  273. {
  274. _STARPU_TRACE_UNLOCKING_MUTEX();
  275. int p_ret = pthread_mutex_unlock(mutex);
  276. int workerid = starpu_worker_get_id();
  277. if(workerid != -1 && _starpu_worker_mutex_is_sched_mutex(workerid, mutex))
  278. _starpu_worker_set_flag_sched_mutex_locked(workerid, 0);
  279. _STARPU_TRACE_MUTEX_UNLOCKED();
  280. return p_ret;
  281. }
  282. int starpu_pthread_mutex_trylock(starpu_pthread_mutex_t *mutex)
  283. {
  284. int ret;
  285. _STARPU_TRACE_TRYLOCK_MUTEX();
  286. ret = pthread_mutex_trylock(mutex);
  287. if (!ret)
  288. {
  289. int workerid = starpu_worker_get_id();
  290. if(workerid != -1 && _starpu_worker_mutex_is_sched_mutex(workerid, mutex))
  291. _starpu_worker_set_flag_sched_mutex_locked(workerid, 1);
  292. _STARPU_TRACE_MUTEX_LOCKED();
  293. }
  294. return ret;
  295. }
  296. int starpu_pthread_cond_wait(starpu_pthread_cond_t *cond, starpu_pthread_mutex_t *mutex)
  297. {
  298. _STARPU_TRACE_COND_WAIT_BEGIN();
  299. int p_ret = pthread_cond_wait(cond, mutex);
  300. _STARPU_TRACE_COND_WAIT_END();
  301. return p_ret;
  302. }
  303. int starpu_pthread_rwlock_rdlock(starpu_pthread_rwlock_t *rwlock)
  304. {
  305. _STARPU_TRACE_RDLOCKING_RWLOCK();
  306. int p_ret = pthread_rwlock_rdlock(rwlock);
  307. _STARPU_TRACE_RWLOCK_RDLOCKED();
  308. return p_ret;
  309. }
  310. int starpu_pthread_rwlock_tryrdlock(starpu_pthread_rwlock_t *rwlock)
  311. {
  312. _STARPU_TRACE_RDLOCKING_RWLOCK();
  313. int p_ret = pthread_rwlock_tryrdlock(rwlock);
  314. if (!p_ret)
  315. _STARPU_TRACE_RWLOCK_RDLOCKED();
  316. return p_ret;
  317. }
  318. int starpu_pthread_rwlock_wrlock(starpu_pthread_rwlock_t *rwlock)
  319. {
  320. _STARPU_TRACE_WRLOCKING_RWLOCK();
  321. int p_ret = pthread_rwlock_wrlock(rwlock);
  322. _STARPU_TRACE_RWLOCK_WRLOCKED();
  323. return p_ret;
  324. }
  325. int starpu_pthread_rwlock_trywrlock(starpu_pthread_rwlock_t *rwlock)
  326. {
  327. _STARPU_TRACE_WRLOCKING_RWLOCK();
  328. int p_ret = pthread_rwlock_trywrlock(rwlock);
  329. if (!p_ret)
  330. _STARPU_TRACE_RWLOCK_WRLOCKED();
  331. return p_ret;
  332. }
  333. int starpu_pthread_rwlock_unlock(starpu_pthread_rwlock_t *rwlock)
  334. {
  335. _STARPU_TRACE_UNLOCKING_RWLOCK();
  336. int p_ret = pthread_rwlock_unlock(rwlock);
  337. _STARPU_TRACE_RWLOCK_UNLOCKED();
  338. return p_ret;
  339. }
  340. #endif
  341. #if !defined(STARPU_SIMGRID) && !defined(_MSC_VER) && defined(STARPU_HAVE_PTHREAD_BARRIER)
  342. int starpu_pthread_barrier_wait(starpu_pthread_barrier_t *barrier)
  343. {
  344. int ret;
  345. _STARPU_TRACE_BARRIER_WAIT_BEGIN();
  346. ret = pthread_barrier_wait(barrier);
  347. _STARPU_TRACE_BARRIER_WAIT_END();
  348. return ret;
  349. }
  350. #endif /* STARPU_SIMGRID, _MSC_VER, STARPU_HAVE_PTHREAD_BARRIER */
  351. #if defined(STARPU_SIMGRID) || !defined(HAVE_PTHREAD_SPIN_LOCK)
  352. int starpu_pthread_spin_init(starpu_pthread_spinlock_t *lock, int pshared)
  353. {
  354. lock->taken = 0;
  355. return 0;
  356. }
  357. int starpu_pthread_spin_destroy(starpu_pthread_spinlock_t *lock)
  358. {
  359. /* we don't do anything */
  360. return 0;
  361. }
  362. int starpu_pthread_spin_lock(starpu_pthread_spinlock_t *lock)
  363. {
  364. #ifdef STARPU_SIMGRID
  365. while (1)
  366. {
  367. if (!lock->taken)
  368. {
  369. lock->taken = 1;
  370. return 0;
  371. }
  372. /* Give hand to another thread, hopefully the one which has the
  373. * spinlock and probably just has also a short-lived mutex. */
  374. MSG_process_sleep(0.000001);
  375. STARPU_UYIELD();
  376. }
  377. #else
  378. uint32_t prev;
  379. do
  380. {
  381. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  382. if (prev)
  383. STARPU_UYIELD();
  384. }
  385. while (prev);
  386. return 0;
  387. #endif
  388. }
  389. int starpu_pthread_spin_trylock(starpu_pthread_spinlock_t *lock)
  390. {
  391. #ifdef STARPU_SIMGRID
  392. if (lock->taken)
  393. return EBUSY;
  394. lock->taken = 1;
  395. return 0;
  396. #else
  397. uint32_t prev;
  398. prev = STARPU_TEST_AND_SET(&lock->taken, 1);
  399. return (prev == 0)?0:EBUSY;
  400. #endif
  401. }
  402. int starpu_pthread_spin_unlock(starpu_pthread_spinlock_t *lock)
  403. {
  404. #ifdef STARPU_SIMGRID
  405. lock->taken = 0;
  406. return 0;
  407. #else
  408. STARPU_RELEASE(&lock->taken);
  409. return 0;
  410. #endif
  411. }
  412. #endif /* defined(STARPU_SIMGRID) || !defined(HAVE_PTHREAD_SPIN_LOCK) */
  413. int _starpu_pthread_spin_checklocked(starpu_pthread_spinlock_t *lock)
  414. {
  415. #ifdef STARPU_SIMGRID
  416. STARPU_ASSERT(lock->taken);
  417. return !lock->taken;
  418. #elif defined(HAVE_PTHREAD_SPIN_LOCK)
  419. int ret = pthread_spin_trylock((pthread_spinlock_t *)lock);
  420. STARPU_ASSERT(ret != 0);
  421. return ret == 0;
  422. #else
  423. STARPU_ASSERT(lock->taken);
  424. return !lock->taken;
  425. #endif
  426. }