closure.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #ifndef _LINUX_CLOSURE_H
  2. #define _LINUX_CLOSURE_H
  3. #include <linux/llist.h>
  4. #include <linux/sched.h>
  5. #include <linux/workqueue.h>
  6. /*
  7. * Closure is perhaps the most overused and abused term in computer science, but
  8. * since I've been unable to come up with anything better you're stuck with it
  9. * again.
  10. *
  11. * What are closures?
  12. *
  13. * They embed a refcount. The basic idea is they count "things that are in
  14. * progress" - in flight bios, some other thread that's doing something else -
  15. * anything you might want to wait on.
  16. *
  17. * The refcount may be manipulated with closure_get() and closure_put().
  18. * closure_put() is where many of the interesting things happen, when it causes
  19. * the refcount to go to 0.
  20. *
  21. * Closures can be used to wait on things both synchronously and asynchronously,
  22. * and synchronous and asynchronous use can be mixed without restriction. To
  23. * wait synchronously, use closure_sync() - you will sleep until your closure's
  24. * refcount hits 1.
  25. *
  26. * To wait asynchronously, use
  27. * continue_at(cl, next_function, workqueue);
  28. *
  29. * passing it, as you might expect, the function to run when nothing is pending
  30. * and the workqueue to run that function out of.
  31. *
  32. * continue_at() also, critically, is a macro that returns the calling function.
  33. * There's good reason for this.
  34. *
  35. * To use safely closures asynchronously, they must always have a refcount while
  36. * they are running owned by the thread that is running them. Otherwise, suppose
  37. * you submit some bios and wish to have a function run when they all complete:
  38. *
  39. * foo_endio(struct bio *bio, int error)
  40. * {
  41. * closure_put(cl);
  42. * }
  43. *
  44. * closure_init(cl);
  45. *
  46. * do_stuff();
  47. * closure_get(cl);
  48. * bio1->bi_endio = foo_endio;
  49. * bio_submit(bio1);
  50. *
  51. * do_more_stuff();
  52. * closure_get(cl);
  53. * bio2->bi_endio = foo_endio;
  54. * bio_submit(bio2);
  55. *
  56. * continue_at(cl, complete_some_read, system_wq);
  57. *
  58. * If closure's refcount started at 0, complete_some_read() could run before the
  59. * second bio was submitted - which is almost always not what you want! More
  60. * importantly, it wouldn't be possible to say whether the original thread or
  61. * complete_some_read()'s thread owned the closure - and whatever state it was
  62. * associated with!
  63. *
  64. * So, closure_init() initializes a closure's refcount to 1 - and when a
  65. * closure_fn is run, the refcount will be reset to 1 first.
  66. *
  67. * Then, the rule is - if you got the refcount with closure_get(), release it
  68. * with closure_put() (i.e, in a bio->bi_endio function). If you have a refcount
  69. * on a closure because you called closure_init() or you were run out of a
  70. * closure - _always_ use continue_at(). Doing so consistently will help
  71. * eliminate an entire class of particularly pernicious races.
  72. *
  73. * For a closure to wait on an arbitrary event, we need to introduce waitlists:
  74. *
  75. * struct closure_waitlist list;
  76. * closure_wait_event(list, cl, condition);
  77. * closure_wake_up(wait_list);
  78. *
  79. * These work analagously to wait_event() and wake_up() - except that instead of
  80. * operating on the current thread (for wait_event()) and lists of threads, they
  81. * operate on an explicit closure and lists of closures.
  82. *
  83. * Because it's a closure we can now wait either synchronously or
  84. * asynchronously. closure_wait_event() returns the current value of the
  85. * condition, and if it returned false continue_at() or closure_sync() can be
  86. * used to wait for it to become true.
  87. *
  88. * It's useful for waiting on things when you can't sleep in the context in
  89. * which you must check the condition (perhaps a spinlock held, or you might be
  90. * beneath generic_make_request() - in which case you can't sleep on IO).
  91. *
  92. * closure_wait_event() will wait either synchronously or asynchronously,
  93. * depending on whether the closure is in blocking mode or not. You can pick a
  94. * mode explicitly with closure_wait_event_sync() and
  95. * closure_wait_event_async(), which do just what you might expect.
  96. *
  97. * Lastly, you might have a wait list dedicated to a specific event, and have no
  98. * need for specifying the condition - you just want to wait until someone runs
  99. * closure_wake_up() on the appropriate wait list. In that case, just use
  100. * closure_wait(). It will return either true or false, depending on whether the
  101. * closure was already on a wait list or not - a closure can only be on one wait
  102. * list at a time.
  103. *
  104. * Parents:
  105. *
  106. * closure_init() takes two arguments - it takes the closure to initialize, and
  107. * a (possibly null) parent.
  108. *
  109. * If parent is non null, the new closure will have a refcount for its lifetime;
  110. * a closure is considered to be "finished" when its refcount hits 0 and the
  111. * function to run is null. Hence
  112. *
  113. * continue_at(cl, NULL, NULL);
  114. *
  115. * returns up the (spaghetti) stack of closures, precisely like normal return
  116. * returns up the C stack. continue_at() with non null fn is better thought of
  117. * as doing a tail call.
  118. *
  119. * All this implies that a closure should typically be embedded in a particular
  120. * struct (which its refcount will normally control the lifetime of), and that
  121. * struct can very much be thought of as a stack frame.
  122. *
  123. * Locking:
  124. *
  125. * Closures are based on work items but they can be thought of as more like
  126. * threads - in that like threads and unlike work items they have a well
  127. * defined lifetime; they are created (with closure_init()) and eventually
  128. * complete after a continue_at(cl, NULL, NULL).
  129. *
  130. * Suppose you've got some larger structure with a closure embedded in it that's
  131. * used for periodically doing garbage collection. You only want one garbage
  132. * collection happening at a time, so the natural thing to do is protect it with
  133. * a lock. However, it's difficult to use a lock protecting a closure correctly
  134. * because the unlock should come after the last continue_to() (additionally, if
  135. * you're using the closure asynchronously a mutex won't work since a mutex has
  136. * to be unlocked by the same process that locked it).
  137. *
  138. * So to make it less error prone and more efficient, we also have the ability
  139. * to use closures as locks:
  140. *
  141. * closure_init_unlocked();
  142. * closure_trylock();
  143. *
  144. * That's all we need for trylock() - the last closure_put() implicitly unlocks
  145. * it for you. But for closure_lock(), we also need a wait list:
  146. *
  147. * struct closure_with_waitlist frobnicator_cl;
  148. *
  149. * closure_init_unlocked(&frobnicator_cl);
  150. * closure_lock(&frobnicator_cl);
  151. *
  152. * A closure_with_waitlist embeds a closure and a wait list - much like struct
  153. * delayed_work embeds a work item and a timer_list. The important thing is, use
  154. * it exactly like you would a regular closure and closure_put() will magically
  155. * handle everything for you.
  156. *
  157. * We've got closures that embed timers, too. They're called, appropriately
  158. * enough:
  159. * struct closure_with_timer;
  160. *
  161. * This gives you access to closure_delay(). It takes a refcount for a specified
  162. * number of jiffies - you could then call closure_sync() (for a slightly
  163. * convoluted version of msleep()) or continue_at() - which gives you the same
  164. * effect as using a delayed work item, except you can reuse the work_struct
  165. * already embedded in struct closure.
  166. *
  167. * Lastly, there's struct closure_with_waitlist_and_timer. It does what you
  168. * probably expect, if you happen to need the features of both. (You don't
  169. * really want to know how all this is implemented, but if I've done my job
  170. * right you shouldn't have to care).
  171. */
  172. struct closure;
  173. typedef void (closure_fn) (struct closure *);
  174. struct closure_waitlist {
  175. struct llist_head list;
  176. };
  177. enum closure_type {
  178. TYPE_closure = 0,
  179. TYPE_closure_with_waitlist = 1,
  180. TYPE_closure_with_timer = 2,
  181. TYPE_closure_with_waitlist_and_timer = 3,
  182. MAX_CLOSURE_TYPE = 3,
  183. };
  184. enum closure_state {
  185. /*
  186. * CLOSURE_BLOCKING: Causes closure_wait_event() to block, instead of
  187. * waiting asynchronously
  188. *
  189. * CLOSURE_WAITING: Set iff the closure is on a waitlist. Must be set by
  190. * the thread that owns the closure, and cleared by the thread that's
  191. * waking up the closure.
  192. *
  193. * CLOSURE_SLEEPING: Must be set before a thread uses a closure to sleep
  194. * - indicates that cl->task is valid and closure_put() may wake it up.
  195. * Only set or cleared by the thread that owns the closure.
  196. *
  197. * CLOSURE_TIMER: Analagous to CLOSURE_WAITING, indicates that a closure
  198. * has an outstanding timer. Must be set by the thread that owns the
  199. * closure, and cleared by the timer function when the timer goes off.
  200. *
  201. * The rest are for debugging and don't affect behaviour:
  202. *
  203. * CLOSURE_RUNNING: Set when a closure is running (i.e. by
  204. * closure_init() and when closure_put() runs then next function), and
  205. * must be cleared before remaining hits 0. Primarily to help guard
  206. * against incorrect usage and accidentally transferring references.
  207. * continue_at() and closure_return() clear it for you, if you're doing
  208. * something unusual you can use closure_set_dead() which also helps
  209. * annotate where references are being transferred.
  210. *
  211. * CLOSURE_STACK: Sanity check - remaining should never hit 0 on a
  212. * closure with this flag set
  213. */
  214. CLOSURE_BITS_START = (1 << 19),
  215. CLOSURE_DESTRUCTOR = (1 << 19),
  216. CLOSURE_BLOCKING = (1 << 21),
  217. CLOSURE_WAITING = (1 << 23),
  218. CLOSURE_SLEEPING = (1 << 25),
  219. CLOSURE_TIMER = (1 << 27),
  220. CLOSURE_RUNNING = (1 << 29),
  221. CLOSURE_STACK = (1 << 31),
  222. };
  223. #define CLOSURE_GUARD_MASK \
  224. ((CLOSURE_DESTRUCTOR|CLOSURE_BLOCKING|CLOSURE_WAITING| \
  225. CLOSURE_SLEEPING|CLOSURE_TIMER|CLOSURE_RUNNING|CLOSURE_STACK) << 1)
  226. #define CLOSURE_REMAINING_MASK (CLOSURE_BITS_START - 1)
  227. #define CLOSURE_REMAINING_INITIALIZER (1|CLOSURE_RUNNING)
  228. struct closure {
  229. union {
  230. struct {
  231. struct workqueue_struct *wq;
  232. struct task_struct *task;
  233. struct llist_node list;
  234. closure_fn *fn;
  235. };
  236. struct work_struct work;
  237. };
  238. struct closure *parent;
  239. atomic_t remaining;
  240. enum closure_type type;
  241. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  242. #define CLOSURE_MAGIC_DEAD 0xc054dead
  243. #define CLOSURE_MAGIC_ALIVE 0xc054a11e
  244. unsigned magic;
  245. struct list_head all;
  246. unsigned long ip;
  247. unsigned long waiting_on;
  248. #endif
  249. };
  250. struct closure_with_waitlist {
  251. struct closure cl;
  252. struct closure_waitlist wait;
  253. };
  254. struct closure_with_timer {
  255. struct closure cl;
  256. struct timer_list timer;
  257. };
  258. struct closure_with_waitlist_and_timer {
  259. struct closure cl;
  260. struct closure_waitlist wait;
  261. struct timer_list timer;
  262. };
  263. extern unsigned invalid_closure_type(void);
  264. #define __CLOSURE_TYPE(cl, _t) \
  265. __builtin_types_compatible_p(typeof(cl), struct _t) \
  266. ? TYPE_ ## _t : \
  267. #define __closure_type(cl) \
  268. ( \
  269. __CLOSURE_TYPE(cl, closure) \
  270. __CLOSURE_TYPE(cl, closure_with_waitlist) \
  271. __CLOSURE_TYPE(cl, closure_with_timer) \
  272. __CLOSURE_TYPE(cl, closure_with_waitlist_and_timer) \
  273. invalid_closure_type() \
  274. )
  275. void closure_sub(struct closure *cl, int v);
  276. void closure_put(struct closure *cl);
  277. void closure_queue(struct closure *cl);
  278. void __closure_wake_up(struct closure_waitlist *list);
  279. bool closure_wait(struct closure_waitlist *list, struct closure *cl);
  280. void closure_sync(struct closure *cl);
  281. bool closure_trylock(struct closure *cl, struct closure *parent);
  282. void __closure_lock(struct closure *cl, struct closure *parent,
  283. struct closure_waitlist *wait_list);
  284. void do_closure_timer_init(struct closure *cl);
  285. bool __closure_delay(struct closure *cl, unsigned long delay,
  286. struct timer_list *timer);
  287. void __closure_flush(struct closure *cl, struct timer_list *timer);
  288. void __closure_flush_sync(struct closure *cl, struct timer_list *timer);
  289. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  290. void closure_debug_init(void);
  291. void closure_debug_create(struct closure *cl);
  292. void closure_debug_destroy(struct closure *cl);
  293. #else
  294. static inline void closure_debug_init(void) {}
  295. static inline void closure_debug_create(struct closure *cl) {}
  296. static inline void closure_debug_destroy(struct closure *cl) {}
  297. #endif
  298. static inline void closure_set_ip(struct closure *cl)
  299. {
  300. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  301. cl->ip = _THIS_IP_;
  302. #endif
  303. }
  304. static inline void closure_set_ret_ip(struct closure *cl)
  305. {
  306. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  307. cl->ip = _RET_IP_;
  308. #endif
  309. }
  310. static inline void closure_get(struct closure *cl)
  311. {
  312. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  313. BUG_ON((atomic_inc_return(&cl->remaining) &
  314. CLOSURE_REMAINING_MASK) <= 1);
  315. #else
  316. atomic_inc(&cl->remaining);
  317. #endif
  318. }
  319. static inline void closure_set_stopped(struct closure *cl)
  320. {
  321. atomic_sub(CLOSURE_RUNNING, &cl->remaining);
  322. }
  323. static inline bool closure_is_stopped(struct closure *cl)
  324. {
  325. return !(atomic_read(&cl->remaining) & CLOSURE_RUNNING);
  326. }
  327. static inline bool closure_is_unlocked(struct closure *cl)
  328. {
  329. return atomic_read(&cl->remaining) == -1;
  330. }
  331. static inline void do_closure_init(struct closure *cl, struct closure *parent,
  332. bool running)
  333. {
  334. switch (cl->type) {
  335. case TYPE_closure_with_timer:
  336. case TYPE_closure_with_waitlist_and_timer:
  337. do_closure_timer_init(cl);
  338. default:
  339. break;
  340. }
  341. cl->parent = parent;
  342. if (parent)
  343. closure_get(parent);
  344. if (running) {
  345. closure_debug_create(cl);
  346. atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER);
  347. } else
  348. atomic_set(&cl->remaining, -1);
  349. closure_set_ip(cl);
  350. }
  351. /*
  352. * Hack to get at the embedded closure if there is one, by doing an unsafe cast:
  353. * the result of __closure_type() is thrown away, it's used merely for type
  354. * checking.
  355. */
  356. #define __to_internal_closure(cl) \
  357. ({ \
  358. BUILD_BUG_ON(__closure_type(*cl) > MAX_CLOSURE_TYPE); \
  359. (struct closure *) cl; \
  360. })
  361. #define closure_init_type(cl, parent, running) \
  362. do { \
  363. struct closure *_cl = __to_internal_closure(cl); \
  364. _cl->type = __closure_type(*(cl)); \
  365. do_closure_init(_cl, parent, running); \
  366. } while (0)
  367. /**
  368. * __closure_init() - Initialize a closure, skipping the memset()
  369. *
  370. * May be used instead of closure_init() when memory has already been zeroed.
  371. */
  372. #define __closure_init(cl, parent) \
  373. closure_init_type(cl, parent, true)
  374. /**
  375. * closure_init() - Initialize a closure, setting the refcount to 1
  376. * @cl: closure to initialize
  377. * @parent: parent of the new closure. cl will take a refcount on it for its
  378. * lifetime; may be NULL.
  379. */
  380. #define closure_init(cl, parent) \
  381. do { \
  382. memset((cl), 0, sizeof(*(cl))); \
  383. __closure_init(cl, parent); \
  384. } while (0)
  385. static inline void closure_init_stack(struct closure *cl)
  386. {
  387. memset(cl, 0, sizeof(struct closure));
  388. atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|
  389. CLOSURE_BLOCKING|CLOSURE_STACK);
  390. }
  391. /**
  392. * closure_init_unlocked() - Initialize a closure but leave it unlocked.
  393. * @cl: closure to initialize
  394. *
  395. * For when the closure will be used as a lock. The closure may not be used
  396. * until after a closure_lock() or closure_trylock().
  397. */
  398. #define closure_init_unlocked(cl) \
  399. do { \
  400. memset((cl), 0, sizeof(*(cl))); \
  401. closure_init_type(cl, NULL, false); \
  402. } while (0)
  403. /**
  404. * closure_lock() - lock and initialize a closure.
  405. * @cl: the closure to lock
  406. * @parent: the new parent for this closure
  407. *
  408. * The closure must be of one of the types that has a waitlist (otherwise we
  409. * wouldn't be able to sleep on contention).
  410. *
  411. * @parent has exactly the same meaning as in closure_init(); if non null, the
  412. * closure will take a reference on @parent which will be released when it is
  413. * unlocked.
  414. */
  415. #define closure_lock(cl, parent) \
  416. __closure_lock(__to_internal_closure(cl), parent, &(cl)->wait)
  417. /**
  418. * closure_delay() - delay some number of jiffies
  419. * @cl: the closure that will sleep
  420. * @delay: the delay in jiffies
  421. *
  422. * Takes a refcount on @cl which will be released after @delay jiffies; this may
  423. * be used to have a function run after a delay with continue_at(), or
  424. * closure_sync() may be used for a convoluted version of msleep().
  425. */
  426. #define closure_delay(cl, delay) \
  427. __closure_delay(__to_internal_closure(cl), delay, &(cl)->timer)
  428. #define closure_flush(cl) \
  429. __closure_flush(__to_internal_closure(cl), &(cl)->timer)
  430. #define closure_flush_sync(cl) \
  431. __closure_flush_sync(__to_internal_closure(cl), &(cl)->timer)
  432. static inline void __closure_end_sleep(struct closure *cl)
  433. {
  434. __set_current_state(TASK_RUNNING);
  435. if (atomic_read(&cl->remaining) & CLOSURE_SLEEPING)
  436. atomic_sub(CLOSURE_SLEEPING, &cl->remaining);
  437. }
  438. static inline void __closure_start_sleep(struct closure *cl)
  439. {
  440. closure_set_ip(cl);
  441. cl->task = current;
  442. set_current_state(TASK_UNINTERRUPTIBLE);
  443. if (!(atomic_read(&cl->remaining) & CLOSURE_SLEEPING))
  444. atomic_add(CLOSURE_SLEEPING, &cl->remaining);
  445. }
  446. /**
  447. * closure_blocking() - returns true if the closure is in blocking mode.
  448. *
  449. * If a closure is in blocking mode, closure_wait_event() will sleep until the
  450. * condition is true instead of waiting asynchronously.
  451. */
  452. static inline bool closure_blocking(struct closure *cl)
  453. {
  454. return atomic_read(&cl->remaining) & CLOSURE_BLOCKING;
  455. }
  456. /**
  457. * set_closure_blocking() - put a closure in blocking mode.
  458. *
  459. * If a closure is in blocking mode, closure_wait_event() will sleep until the
  460. * condition is true instead of waiting asynchronously.
  461. *
  462. * Not thread safe - can only be called by the thread running the closure.
  463. */
  464. static inline void set_closure_blocking(struct closure *cl)
  465. {
  466. if (!closure_blocking(cl))
  467. atomic_add(CLOSURE_BLOCKING, &cl->remaining);
  468. }
  469. /*
  470. * Not thread safe - can only be called by the thread running the closure.
  471. */
  472. static inline void clear_closure_blocking(struct closure *cl)
  473. {
  474. if (closure_blocking(cl))
  475. atomic_sub(CLOSURE_BLOCKING, &cl->remaining);
  476. }
  477. /**
  478. * closure_wake_up() - wake up all closures on a wait list.
  479. */
  480. static inline void closure_wake_up(struct closure_waitlist *list)
  481. {
  482. smp_mb();
  483. __closure_wake_up(list);
  484. }
  485. /*
  486. * Wait on an event, synchronously or asynchronously - analogous to wait_event()
  487. * but for closures.
  488. *
  489. * The loop is oddly structured so as to avoid a race; we must check the
  490. * condition again after we've added ourself to the waitlist. We know if we were
  491. * already on the waitlist because closure_wait() returns false; thus, we only
  492. * schedule or break if closure_wait() returns false. If it returns true, we
  493. * just loop again - rechecking the condition.
  494. *
  495. * The __closure_wake_up() is necessary because we may race with the event
  496. * becoming true; i.e. we see event false -> wait -> recheck condition, but the
  497. * thread that made the event true may have called closure_wake_up() before we
  498. * added ourself to the wait list.
  499. *
  500. * We have to call closure_sync() at the end instead of just
  501. * __closure_end_sleep() because a different thread might've called
  502. * closure_wake_up() before us and gotten preempted before they dropped the
  503. * refcount on our closure. If this was a stack allocated closure, that would be
  504. * bad.
  505. */
  506. #define __closure_wait_event(list, cl, condition, _block) \
  507. ({ \
  508. bool block = _block; \
  509. typeof(condition) ret; \
  510. \
  511. while (1) { \
  512. ret = (condition); \
  513. if (ret) { \
  514. __closure_wake_up(list); \
  515. if (block) \
  516. closure_sync(cl); \
  517. \
  518. break; \
  519. } \
  520. \
  521. if (block) \
  522. __closure_start_sleep(cl); \
  523. \
  524. if (!closure_wait(list, cl)) { \
  525. if (!block) \
  526. break; \
  527. \
  528. schedule(); \
  529. } \
  530. } \
  531. \
  532. ret; \
  533. })
  534. /**
  535. * closure_wait_event() - wait on a condition, synchronously or asynchronously.
  536. * @list: the wait list to wait on
  537. * @cl: the closure that is doing the waiting
  538. * @condition: a C expression for the event to wait for
  539. *
  540. * If the closure is in blocking mode, sleeps until the @condition evaluates to
  541. * true - exactly like wait_event().
  542. *
  543. * If the closure is not in blocking mode, waits asynchronously; if the
  544. * condition is currently false the @cl is put onto @list and returns. @list
  545. * owns a refcount on @cl; closure_sync() or continue_at() may be used later to
  546. * wait for another thread to wake up @list, which drops the refcount on @cl.
  547. *
  548. * Returns the value of @condition; @cl will be on @list iff @condition was
  549. * false.
  550. *
  551. * closure_wake_up(@list) must be called after changing any variable that could
  552. * cause @condition to become true.
  553. */
  554. #define closure_wait_event(list, cl, condition) \
  555. __closure_wait_event(list, cl, condition, closure_blocking(cl))
  556. #define closure_wait_event_async(list, cl, condition) \
  557. __closure_wait_event(list, cl, condition, false)
  558. #define closure_wait_event_sync(list, cl, condition) \
  559. __closure_wait_event(list, cl, condition, true)
  560. static inline void set_closure_fn(struct closure *cl, closure_fn *fn,
  561. struct workqueue_struct *wq)
  562. {
  563. BUG_ON(object_is_on_stack(cl));
  564. closure_set_ip(cl);
  565. cl->fn = fn;
  566. cl->wq = wq;
  567. /* between atomic_dec() in closure_put() */
  568. smp_mb__before_atomic_dec();
  569. }
  570. #define continue_at(_cl, _fn, _wq) \
  571. do { \
  572. set_closure_fn(_cl, _fn, _wq); \
  573. closure_sub(_cl, CLOSURE_RUNNING + 1); \
  574. return; \
  575. } while (0)
  576. #define closure_return(_cl) continue_at((_cl), NULL, NULL)
  577. #define continue_at_nobarrier(_cl, _fn, _wq) \
  578. do { \
  579. set_closure_fn(_cl, _fn, _wq); \
  580. closure_queue(cl); \
  581. return; \
  582. } while (0)
  583. #define closure_return_with_destructor(_cl, _destructor) \
  584. do { \
  585. set_closure_fn(_cl, _destructor, NULL); \
  586. closure_sub(_cl, CLOSURE_RUNNING - CLOSURE_DESTRUCTOR + 1); \
  587. return; \
  588. } while (0)
  589. static inline void closure_call(struct closure *cl, closure_fn fn,
  590. struct workqueue_struct *wq,
  591. struct closure *parent)
  592. {
  593. closure_init(cl, parent);
  594. continue_at_nobarrier(cl, fn, wq);
  595. }
  596. static inline void closure_trylock_call(struct closure *cl, closure_fn fn,
  597. struct workqueue_struct *wq,
  598. struct closure *parent)
  599. {
  600. if (closure_trylock(cl, parent))
  601. continue_at_nobarrier(cl, fn, wq);
  602. }
  603. #endif /* _LINUX_CLOSURE_H */