sleepq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. /*
  2. * Copyright (c) 2017-2018 Richard Braun.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. */
  18. #include <assert.h>
  19. #include <stdalign.h>
  20. #include <stdbool.h>
  21. #include <stddef.h>
  22. #include <stdint.h>
  23. #include <kern/hlist.h>
  24. #include <kern/init.h>
  25. #include <kern/kmem.h>
  26. #include <kern/list.h>
  27. #include <kern/macros.h>
  28. #include <kern/sleepq.h>
  29. #include <kern/spinlock.h>
  30. #include <kern/thread.h>
  31. #include <machine/cpu.h>
  32. struct sleepq_bucket
  33. {
  34. __cacheline_aligned struct spinlock lock;
  35. struct hlist sleepqs;
  36. };
  37. struct sleepq_waiter
  38. {
  39. struct list node;
  40. struct thread *thread;
  41. struct sleepq *sleepq;
  42. bool pending_wakeup;
  43. };
  44. #define SLEEPQ_HTABLE_SIZE 128
  45. #if !ISP2(SLEEPQ_HTABLE_SIZE) || !ISP2(SLEEPQ_COND_HTABLE_SIZE)
  46. #error "hash table size must be a power of two"
  47. #endif
  48. #define SLEEPQ_HTABLE_MASK (SLEEPQ_HTABLE_SIZE - 1)
  49. static struct sleepq_bucket sleepq_htable[SLEEPQ_HTABLE_SIZE];
  50. static struct kmem_cache sleepq_cache;
  51. static void
  52. sleepq_waiter_init (struct sleepq_waiter *waiter,
  53. struct thread *thread, struct sleepq *sleepq)
  54. {
  55. waiter->thread = thread;
  56. waiter->sleepq = sleepq;
  57. waiter->pending_wakeup = false;
  58. }
  59. static bool
  60. sleepq_waiter_pending_wakeup (const struct sleepq_waiter *waiter)
  61. {
  62. return (waiter->pending_wakeup);
  63. }
  64. static void
  65. sleepq_waiter_set_pending_wakeup (struct sleepq_waiter *waiter)
  66. {
  67. waiter->pending_wakeup = true;
  68. }
  69. static void
  70. sleepq_waiter_wakeup (struct sleepq_waiter *waiter, struct sleepq *sleepq)
  71. {
  72. if (!sleepq_waiter_pending_wakeup (waiter))
  73. return;
  74. waiter->sleepq = sleepq;
  75. thread_wakeup (waiter->thread);
  76. }
  77. static bool
  78. sleepq_init_state_valid (const struct sleepq *sleepq)
  79. {
  80. return (!sleepq->bucket &&
  81. sync_key_isclear (&sleepq->sync_key) &&
  82. list_empty (&sleepq->waiters) &&
  83. !sleepq->oldest_waiter &&
  84. !sleepq->next_free);
  85. }
  86. static void
  87. sleepq_use (struct sleepq *sleepq, const union sync_key *key)
  88. {
  89. assert (sync_key_isclear (&sleepq->sync_key));
  90. sleepq->sync_key = *key;
  91. }
  92. static void
  93. sleepq_unuse (struct sleepq *sleepq)
  94. {
  95. sync_key_clear (&sleepq->sync_key);
  96. }
  97. static bool
  98. sleepq_in_use (const struct sleepq *sleepq)
  99. {
  100. return (!sync_key_isclear (&sleepq->sync_key));
  101. }
  102. static bool
  103. sleepq_in_use_by (const struct sleepq *sleepq, const union sync_key *key)
  104. {
  105. return (sync_key_eq (&sleepq->sync_key, key));
  106. }
  107. static void
  108. sleepq_bucket_init (struct sleepq_bucket *bucket)
  109. {
  110. spinlock_init (&bucket->lock);
  111. hlist_init (&bucket->sleepqs);
  112. }
  113. static struct sleepq_bucket*
  114. sleepq_bucket_get (const union sync_key *key)
  115. {
  116. uintptr_t index = sync_key_hash (key) & SLEEPQ_HTABLE_MASK;
  117. assert (index < ARRAY_SIZE (sleepq_htable));
  118. return (&sleepq_htable[index]);
  119. }
  120. static void
  121. sleepq_bucket_add (struct sleepq_bucket *bucket, struct sleepq *sleepq)
  122. {
  123. assert (!sleepq->bucket);
  124. sleepq->bucket = bucket;
  125. hlist_insert_head (&bucket->sleepqs, &sleepq->node);
  126. }
  127. static void
  128. sleepq_bucket_remove (struct sleepq_bucket *bucket, struct sleepq *sleepq)
  129. {
  130. assert (sleepq->bucket == bucket);
  131. sleepq->bucket = NULL;
  132. hlist_remove (&sleepq->node);
  133. }
  134. static struct sleepq*
  135. sleepq_bucket_lookup (const struct sleepq_bucket *bucket,
  136. const union sync_key *key)
  137. {
  138. struct sleepq *sleepq;
  139. hlist_for_each_entry (&bucket->sleepqs, sleepq, node)
  140. if (sleepq_in_use_by (sleepq, key))
  141. {
  142. assert (sleepq->bucket == bucket);
  143. return (sleepq);
  144. }
  145. return (NULL);
  146. }
  147. static void
  148. sleepq_ctor (void *ptr)
  149. {
  150. struct sleepq *sleepq = ptr;
  151. sleepq->bucket = NULL;
  152. sync_key_clear (&sleepq->sync_key);
  153. list_init (&sleepq->waiters);
  154. sleepq->oldest_waiter = NULL;
  155. sleepq->next_free = NULL;
  156. sleepq->free_link = NULL;
  157. }
  158. static int __init
  159. sleepq_setup (void)
  160. {
  161. for (size_t i = 0; i < ARRAY_SIZE (sleepq_htable); i++)
  162. sleepq_bucket_init (&sleepq_htable[i]);
  163. kmem_cache_init (&sleepq_cache, "sleepq", sizeof (struct sleepq),
  164. alignof (struct sleepq), sleepq_ctor, 0);
  165. return (0);
  166. }
  167. INIT_OP_DEFINE (sleepq_setup,
  168. INIT_OP_DEP (kmem_setup, true));
  169. struct sleepq*
  170. sleepq_create (void)
  171. {
  172. struct sleepq *sleepq = kmem_cache_alloc (&sleepq_cache);
  173. assert (!sleepq || sleepq_init_state_valid (sleepq));
  174. return (sleepq);
  175. }
  176. void
  177. sleepq_destroy (struct sleepq *sleepq)
  178. {
  179. assert (sleepq_init_state_valid (sleepq));
  180. kmem_cache_free (&sleepq_cache, sleepq);
  181. }
  182. static struct sleepq*
  183. sleepq_acquire_common (const union sync_key *key, cpu_flags_t *flags)
  184. {
  185. assert (!sync_key_isclear (key));
  186. _Auto bucket = sleepq_bucket_get (key);
  187. if (flags)
  188. spinlock_lock_intr_save (&bucket->lock, flags);
  189. else
  190. spinlock_lock (&bucket->lock);
  191. _Auto sleepq = sleepq_bucket_lookup (bucket, key);
  192. if (! sleepq)
  193. {
  194. if (flags)
  195. spinlock_unlock_intr_restore (&bucket->lock, *flags);
  196. else
  197. spinlock_unlock (&bucket->lock);
  198. }
  199. return (sleepq);
  200. }
  201. static struct sleepq*
  202. sleepq_tryacquire_common (const union sync_key *key, cpu_flags_t *flags)
  203. {
  204. assert (!sync_key_isclear (key));
  205. _Auto bucket = sleepq_bucket_get (key);
  206. int error = flags ? spinlock_trylock_intr_save (&bucket->lock, flags) :
  207. spinlock_trylock (&bucket->lock);
  208. if (error)
  209. return (NULL);
  210. _Auto sleepq = sleepq_bucket_lookup (bucket, key);
  211. if (! sleepq)
  212. {
  213. if (flags)
  214. spinlock_unlock_intr_restore (&bucket->lock, *flags);
  215. else
  216. spinlock_unlock (&bucket->lock);
  217. }
  218. return (sleepq);
  219. }
  220. struct sleepq*
  221. sleepq_acquire_key (const union sync_key *key)
  222. {
  223. return (sleepq_acquire_common (key, NULL));
  224. }
  225. struct sleepq*
  226. sleepq_tryacquire_key (const union sync_key *key)
  227. {
  228. return (sleepq_tryacquire_common (key, NULL));
  229. }
  230. void
  231. sleepq_release (struct sleepq *sleepq)
  232. {
  233. spinlock_unlock (&sleepq->bucket->lock);
  234. }
  235. struct sleepq*
  236. sleepq_acquire_key_intr (const union sync_key *key, cpu_flags_t *flags)
  237. {
  238. return (sleepq_acquire_common (key, flags));
  239. }
  240. struct sleepq*
  241. sleepq_tryacquire_key_intr (const union sync_key *key, cpu_flags_t *flags)
  242. {
  243. return (sleepq_tryacquire_common (key, flags));
  244. }
  245. void
  246. sleepq_release_intr_restore (struct sleepq *sleepq, cpu_flags_t flags)
  247. {
  248. spinlock_unlock_intr_restore (&sleepq->bucket->lock, flags);
  249. }
  250. static void
  251. sleepq_push_free (struct sleepq *sleepq, struct sleepq *free_sleepq)
  252. {
  253. assert (!free_sleepq->next_free);
  254. free_sleepq->next_free = sleepq->next_free;
  255. if (!sleepq->next_free)
  256. sleepq->free_link = &free_sleepq->next_free;
  257. sleepq->next_free = free_sleepq;
  258. }
  259. static struct sleepq*
  260. sleepq_pop_free (struct sleepq *sleepq)
  261. {
  262. struct sleepq *free_sleepq = sleepq->next_free;
  263. if (! free_sleepq)
  264. return (NULL);
  265. sleepq->next_free = free_sleepq->next_free;
  266. free_sleepq->next_free = NULL;
  267. if (!sleepq->next_free)
  268. sleepq->free_link = NULL;
  269. return (free_sleepq);
  270. }
  271. static struct sleepq*
  272. sleepq_lend_common (const union sync_key *key, cpu_flags_t *flags)
  273. {
  274. assert (!sync_key_isclear (key));
  275. _Auto self = thread_self ();
  276. _Auto sleepq = thread_sleepq_lend (self);
  277. assert (sleepq_init_state_valid (sleepq));
  278. _Auto bucket = sleepq_bucket_get (key);
  279. self->wchan_addr = bucket;
  280. if (flags)
  281. spinlock_lock_intr_save (&bucket->lock, flags);
  282. else
  283. spinlock_lock (&bucket->lock);
  284. struct sleepq *prev = sleepq_bucket_lookup (bucket, key);
  285. if (! prev)
  286. {
  287. sleepq_use (sleepq, key);
  288. sleepq_bucket_add (bucket, sleepq);
  289. }
  290. else
  291. {
  292. sleepq_push_free (prev, sleepq);
  293. sleepq = prev;
  294. }
  295. self->wchan_addr = NULL;
  296. return (sleepq);
  297. }
  298. static void
  299. sleepq_return_common (struct sleepq *sleepq, cpu_flags_t *flags)
  300. {
  301. assert (sleepq_in_use (sleepq));
  302. _Auto bucket = sleepq->bucket;
  303. _Auto free_sleepq = sleepq_pop_free (sleepq);
  304. if (! free_sleepq)
  305. {
  306. sleepq_bucket_remove (bucket, sleepq);
  307. sleepq_unuse (sleepq);
  308. free_sleepq = sleepq;
  309. }
  310. if (flags)
  311. spinlock_unlock_intr_restore (&bucket->lock, *flags);
  312. else
  313. spinlock_unlock (&bucket->lock);
  314. assert (sleepq_init_state_valid (free_sleepq));
  315. thread_sleepq_return (free_sleepq);
  316. }
  317. struct sleepq*
  318. sleepq_lend_key (const union sync_key *key)
  319. {
  320. return (sleepq_lend_common (key, NULL));
  321. }
  322. void
  323. sleepq_return (struct sleepq *sleepq)
  324. {
  325. sleepq_return_common (sleepq, NULL);
  326. }
  327. struct sleepq*
  328. sleepq_lend_key_intr (const union sync_key *key, cpu_flags_t *flags)
  329. {
  330. return (sleepq_lend_common (key, flags));
  331. }
  332. void
  333. sleepq_return_intr_restore (struct sleepq *sleepq, cpu_flags_t flags)
  334. {
  335. sleepq_return_common (sleepq, &flags);
  336. }
  337. static void
  338. sleepq_shift_oldest_waiter (struct sleepq *sleepq)
  339. {
  340. assert (sleepq->oldest_waiter);
  341. struct list *node = list_next (&sleepq->oldest_waiter->node);
  342. sleepq->oldest_waiter = list_end (&sleepq->waiters, node) ?
  343. NULL : list_entry (node, struct sleepq_waiter, node);
  344. }
  345. static void
  346. sleepq_add_waiter (struct sleepq *sleepq, struct sleepq_waiter *waiter)
  347. {
  348. list_insert_tail (&sleepq->waiters, &waiter->node);
  349. if (!sleepq->oldest_waiter)
  350. sleepq->oldest_waiter = waiter;
  351. }
  352. static void
  353. sleepq_remove_waiter (struct sleepq *sleepq, struct sleepq_waiter *waiter)
  354. {
  355. if (sleepq->oldest_waiter == waiter)
  356. sleepq_shift_oldest_waiter (sleepq);
  357. list_remove (&waiter->node);
  358. }
  359. static struct sleepq_waiter*
  360. sleepq_get_last_waiter (struct sleepq *sleepq)
  361. {
  362. return (list_empty (&sleepq->waiters) ?
  363. NULL : list_first_entry (&sleepq->waiters, struct sleepq_waiter, node));
  364. }
  365. static int
  366. sleepq_wait_common (struct sleepq_waiter *waiter, const char *wchan,
  367. bool timed, uint64_t ticks)
  368. {
  369. _Auto sleepq = waiter->sleepq;
  370. struct spinlock *lock = &sleepq->bucket->lock;
  371. sleepq_add_waiter (sleepq, waiter);
  372. int error;
  373. do
  374. {
  375. if (! timed)
  376. {
  377. thread_sleep (lock, waiter, wchan);
  378. error = 0;
  379. }
  380. else
  381. {
  382. error = thread_timedsleep (lock, waiter, wchan, ticks);
  383. if (error)
  384. {
  385. if (sleepq_waiter_pending_wakeup (waiter))
  386. error = 0;
  387. else
  388. break;
  389. }
  390. }
  391. }
  392. while (!sleepq_waiter_pending_wakeup (waiter));
  393. struct sleepq *nsq = atomic_load_rlx (&waiter->sleepq);
  394. if (unlikely (nsq != sleepq))
  395. {
  396. spinlock_unlock (lock);
  397. sleepq = nsq;
  398. spinlock_lock (&sleepq->bucket->lock);
  399. }
  400. sleepq_remove_waiter (sleepq, waiter);
  401. /*
  402. * Chain wake-ups here to prevent broadacasting from walking a list
  403. * with preemption disabled. Note that this doesn't guard against
  404. * the thundering herd effect for condition variables.
  405. */
  406. struct sleepq_waiter *next = sleepq_get_last_waiter (sleepq);
  407. /*
  408. * Checking against the oldest waiter is enough as waiters are awoken
  409. * in strict FIFO order.
  410. */
  411. if (next && next != sleepq->oldest_waiter)
  412. {
  413. sleepq_waiter_set_pending_wakeup (next);
  414. sleepq_waiter_wakeup (next, sleepq);
  415. }
  416. return (error);
  417. }
  418. void
  419. sleepq_wait (struct sleepq *sleepq, const char *wchan)
  420. {
  421. struct sleepq_waiter waiter;
  422. sleepq_waiter_init (&waiter, thread_self (), sleepq);
  423. int error = sleepq_wait_common (&waiter, wchan, false, 0);
  424. assert (! error);
  425. }
  426. int
  427. sleepq_timedwait (struct sleepq *sleepq, const char *wchan, uint64_t ticks)
  428. {
  429. struct sleepq_waiter waiter;
  430. sleepq_waiter_init (&waiter, thread_self (), sleepq);
  431. return (sleepq_wait_common (&waiter, wchan, true, ticks));
  432. }
  433. int
  434. sleepq_wait_movable (struct sleepq **sleepq, const char *wchan,
  435. uint64_t *ticksp)
  436. {
  437. struct sleepq_waiter waiter;
  438. sleepq_waiter_init (&waiter, thread_self (), *sleepq);
  439. int error = ticksp ? sleepq_wait_common (&waiter, wchan, true, *ticksp) :
  440. sleepq_wait_common (&waiter, wchan, false, 0);
  441. *sleepq = waiter.sleepq;
  442. return (error);
  443. }
  444. void
  445. sleepq_signal (struct sleepq *sleepq)
  446. {
  447. struct sleepq_waiter *waiter = sleepq->oldest_waiter;
  448. if (! waiter)
  449. return;
  450. sleepq_shift_oldest_waiter (sleepq);
  451. sleepq_waiter_set_pending_wakeup (waiter);
  452. sleepq_waiter_wakeup (waiter, sleepq);
  453. }
  454. void
  455. sleepq_broadcast (struct sleepq *sleepq)
  456. {
  457. struct sleepq_waiter *waiter = sleepq->oldest_waiter;
  458. if (! waiter)
  459. return;
  460. sleepq->oldest_waiter = NULL;
  461. sleepq_waiter_set_pending_wakeup (waiter);
  462. sleepq_waiter_wakeup (waiter, sleepq);
  463. }
  464. static inline void
  465. sleepq_transfer (struct sleepq *dst, struct sleepq *src,
  466. struct sleepq_waiter *waiter)
  467. {
  468. // Remove oldest waiter from source queue.
  469. list_remove (&waiter->node);
  470. sleepq_add_waiter (dst, waiter);
  471. src->oldest_waiter = NULL;
  472. // Concatenate source and destination waiter queues.
  473. list_concat (&dst->waiters, &src->waiters);
  474. list_init (&src->waiters);
  475. // Transfer all the queues in the free list.
  476. if (src->free_link)
  477. {
  478. *src->free_link = dst->next_free;
  479. dst->next_free = src->next_free;
  480. src->next_free = NULL;
  481. src->free_link = NULL;
  482. }
  483. // Remove source queue from its bucket and clear its key.
  484. sleepq_bucket_remove (src->bucket, src);
  485. sleepq_unuse (src);
  486. sleepq_push_free (dst, src);
  487. }
  488. void
  489. sleepq_move (const union sync_key *dst_key, const union sync_key *src_key,
  490. bool wake_one, bool move_all)
  491. {
  492. assert (dst_key && !sync_key_isclear (dst_key));
  493. assert (src_key && !sync_key_isclear (src_key));
  494. if (sync_key_eq (dst_key, src_key))
  495. return;
  496. _Auto dbucket = sleepq_bucket_get (dst_key);
  497. _Auto sbucket = sleepq_bucket_get (src_key);
  498. // Lock the buckets in order to avoid any deadlocks.
  499. if (dbucket == sbucket)
  500. spinlock_lock (&dbucket->lock);
  501. else if (dbucket < sbucket)
  502. {
  503. spinlock_lock (&dbucket->lock);
  504. spinlock_lock (&sbucket->lock);
  505. }
  506. else
  507. {
  508. spinlock_lock (&sbucket->lock);
  509. spinlock_lock (&dbucket->lock);
  510. }
  511. _Auto dsq = sleepq_bucket_lookup (dbucket, dst_key);
  512. _Auto ssq = sleepq_bucket_lookup (sbucket, src_key);
  513. if (!dsq || !ssq || sleepq_empty (ssq))
  514. goto out;
  515. _Auto prev = ssq->oldest_waiter;
  516. if (move_all || list_singular (&ssq->waiters))
  517. // The source queue will be empty after this operation.
  518. sleepq_transfer (dsq, ssq, prev);
  519. else
  520. {
  521. sleepq_remove_waiter (ssq, prev);
  522. sleepq_add_waiter (dsq, prev);
  523. _Auto free_sleepq = sleepq_pop_free (ssq);
  524. if (free_sleepq)
  525. sleepq_push_free (dsq, free_sleepq);
  526. }
  527. if (wake_one)
  528. sleepq_signal (dsq);
  529. out:
  530. spinlock_unlock (&dbucket->lock);
  531. if (dbucket != sbucket)
  532. spinlock_unlock (&sbucket->lock);
  533. }