closure.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  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_create(struct closure *cl);
  291. void closure_debug_destroy(struct closure *cl);
  292. #else
  293. static inline void closure_debug_create(struct closure *cl) {}
  294. static inline void closure_debug_destroy(struct closure *cl) {}
  295. #endif
  296. static inline void closure_set_ip(struct closure *cl)
  297. {
  298. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  299. cl->ip = _THIS_IP_;
  300. #endif
  301. }
  302. static inline void closure_set_ret_ip(struct closure *cl)
  303. {
  304. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  305. cl->ip = _RET_IP_;
  306. #endif
  307. }
  308. static inline void closure_get(struct closure *cl)
  309. {
  310. #ifdef CONFIG_BCACHE_CLOSURES_DEBUG
  311. BUG_ON((atomic_inc_return(&cl->remaining) &
  312. CLOSURE_REMAINING_MASK) <= 1);
  313. #else
  314. atomic_inc(&cl->remaining);
  315. #endif
  316. }
  317. static inline void closure_set_stopped(struct closure *cl)
  318. {
  319. atomic_sub(CLOSURE_RUNNING, &cl->remaining);
  320. }
  321. static inline bool closure_is_stopped(struct closure *cl)
  322. {
  323. return !(atomic_read(&cl->remaining) & CLOSURE_RUNNING);
  324. }
  325. static inline bool closure_is_unlocked(struct closure *cl)
  326. {
  327. return atomic_read(&cl->remaining) == -1;
  328. }
  329. static inline void do_closure_init(struct closure *cl, struct closure *parent,
  330. bool running)
  331. {
  332. switch (cl->type) {
  333. case TYPE_closure_with_timer:
  334. case TYPE_closure_with_waitlist_and_timer:
  335. do_closure_timer_init(cl);
  336. default:
  337. break;
  338. }
  339. cl->parent = parent;
  340. if (parent)
  341. closure_get(parent);
  342. if (running) {
  343. closure_debug_create(cl);
  344. atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER);
  345. } else
  346. atomic_set(&cl->remaining, -1);
  347. closure_set_ip(cl);
  348. }
  349. /*
  350. * Hack to get at the embedded closure if there is one, by doing an unsafe cast:
  351. * the result of __closure_type() is thrown away, it's used merely for type
  352. * checking.
  353. */
  354. #define __to_internal_closure(cl) \
  355. ({ \
  356. BUILD_BUG_ON(__closure_type(*cl) > MAX_CLOSURE_TYPE); \
  357. (struct closure *) cl; \
  358. })
  359. #define closure_init_type(cl, parent, running) \
  360. do { \
  361. struct closure *_cl = __to_internal_closure(cl); \
  362. _cl->type = __closure_type(*(cl)); \
  363. do_closure_init(_cl, parent, running); \
  364. } while (0)
  365. /**
  366. * __closure_init() - Initialize a closure, skipping the memset()
  367. *
  368. * May be used instead of closure_init() when memory has already been zeroed.
  369. */
  370. #define __closure_init(cl, parent) \
  371. closure_init_type(cl, parent, true)
  372. /**
  373. * closure_init() - Initialize a closure, setting the refcount to 1
  374. * @cl: closure to initialize
  375. * @parent: parent of the new closure. cl will take a refcount on it for its
  376. * lifetime; may be NULL.
  377. */
  378. #define closure_init(cl, parent) \
  379. do { \
  380. memset((cl), 0, sizeof(*(cl))); \
  381. __closure_init(cl, parent); \
  382. } while (0)
  383. static inline void closure_init_stack(struct closure *cl)
  384. {
  385. memset(cl, 0, sizeof(struct closure));
  386. atomic_set(&cl->remaining, CLOSURE_REMAINING_INITIALIZER|
  387. CLOSURE_BLOCKING|CLOSURE_STACK);
  388. }
  389. /**
  390. * closure_init_unlocked() - Initialize a closure but leave it unlocked.
  391. * @cl: closure to initialize
  392. *
  393. * For when the closure will be used as a lock. The closure may not be used
  394. * until after a closure_lock() or closure_trylock().
  395. */
  396. #define closure_init_unlocked(cl) \
  397. do { \
  398. memset((cl), 0, sizeof(*(cl))); \
  399. closure_init_type(cl, NULL, false); \
  400. } while (0)
  401. /**
  402. * closure_lock() - lock and initialize a closure.
  403. * @cl: the closure to lock
  404. * @parent: the new parent for this closure
  405. *
  406. * The closure must be of one of the types that has a waitlist (otherwise we
  407. * wouldn't be able to sleep on contention).
  408. *
  409. * @parent has exactly the same meaning as in closure_init(); if non null, the
  410. * closure will take a reference on @parent which will be released when it is
  411. * unlocked.
  412. */
  413. #define closure_lock(cl, parent) \
  414. __closure_lock(__to_internal_closure(cl), parent, &(cl)->wait)
  415. /**
  416. * closure_delay() - delay some number of jiffies
  417. * @cl: the closure that will sleep
  418. * @delay: the delay in jiffies
  419. *
  420. * Takes a refcount on @cl which will be released after @delay jiffies; this may
  421. * be used to have a function run after a delay with continue_at(), or
  422. * closure_sync() may be used for a convoluted version of msleep().
  423. */
  424. #define closure_delay(cl, delay) \
  425. __closure_delay(__to_internal_closure(cl), delay, &(cl)->timer)
  426. #define closure_flush(cl) \
  427. __closure_flush(__to_internal_closure(cl), &(cl)->timer)
  428. #define closure_flush_sync(cl) \
  429. __closure_flush_sync(__to_internal_closure(cl), &(cl)->timer)
  430. static inline void __closure_end_sleep(struct closure *cl)
  431. {
  432. __set_current_state(TASK_RUNNING);
  433. if (atomic_read(&cl->remaining) & CLOSURE_SLEEPING)
  434. atomic_sub(CLOSURE_SLEEPING, &cl->remaining);
  435. }
  436. static inline void __closure_start_sleep(struct closure *cl)
  437. {
  438. closure_set_ip(cl);
  439. cl->task = current;
  440. set_current_state(TASK_UNINTERRUPTIBLE);
  441. if (!(atomic_read(&cl->remaining) & CLOSURE_SLEEPING))
  442. atomic_add(CLOSURE_SLEEPING, &cl->remaining);
  443. }
  444. /**
  445. * closure_blocking() - returns true if the closure is in blocking mode.
  446. *
  447. * If a closure is in blocking mode, closure_wait_event() will sleep until the
  448. * condition is true instead of waiting asynchronously.
  449. */
  450. static inline bool closure_blocking(struct closure *cl)
  451. {
  452. return atomic_read(&cl->remaining) & CLOSURE_BLOCKING;
  453. }
  454. /**
  455. * set_closure_blocking() - put a closure in blocking mode.
  456. *
  457. * If a closure is in blocking mode, closure_wait_event() will sleep until the
  458. * condition is true instead of waiting asynchronously.
  459. *
  460. * Not thread safe - can only be called by the thread running the closure.
  461. */
  462. static inline void set_closure_blocking(struct closure *cl)
  463. {
  464. if (!closure_blocking(cl))
  465. atomic_add(CLOSURE_BLOCKING, &cl->remaining);
  466. }
  467. /*
  468. * Not thread safe - can only be called by the thread running the closure.
  469. */
  470. static inline void clear_closure_blocking(struct closure *cl)
  471. {
  472. if (closure_blocking(cl))
  473. atomic_sub(CLOSURE_BLOCKING, &cl->remaining);
  474. }
  475. /**
  476. * closure_wake_up() - wake up all closures on a wait list.
  477. */
  478. static inline void closure_wake_up(struct closure_waitlist *list)
  479. {
  480. smp_mb();
  481. __closure_wake_up(list);
  482. }
  483. /*
  484. * Wait on an event, synchronously or asynchronously - analogous to wait_event()
  485. * but for closures.
  486. *
  487. * The loop is oddly structured so as to avoid a race; we must check the
  488. * condition again after we've added ourself to the waitlist. We know if we were
  489. * already on the waitlist because closure_wait() returns false; thus, we only
  490. * schedule or break if closure_wait() returns false. If it returns true, we
  491. * just loop again - rechecking the condition.
  492. *
  493. * The __closure_wake_up() is necessary because we may race with the event
  494. * becoming true; i.e. we see event false -> wait -> recheck condition, but the
  495. * thread that made the event true may have called closure_wake_up() before we
  496. * added ourself to the wait list.
  497. *
  498. * We have to call closure_sync() at the end instead of just
  499. * __closure_end_sleep() because a different thread might've called
  500. * closure_wake_up() before us and gotten preempted before they dropped the
  501. * refcount on our closure. If this was a stack allocated closure, that would be
  502. * bad.
  503. */
  504. #define __closure_wait_event(list, cl, condition, _block) \
  505. ({ \
  506. bool block = _block; \
  507. typeof(condition) ret; \
  508. \
  509. while (1) { \
  510. ret = (condition); \
  511. if (ret) { \
  512. __closure_wake_up(list); \
  513. if (block) \
  514. closure_sync(cl); \
  515. \
  516. break; \
  517. } \
  518. \
  519. if (block) \
  520. __closure_start_sleep(cl); \
  521. \
  522. if (!closure_wait(list, cl)) { \
  523. if (!block) \
  524. break; \
  525. \
  526. schedule(); \
  527. } \
  528. } \
  529. \
  530. ret; \
  531. })
  532. /**
  533. * closure_wait_event() - wait on a condition, synchronously or asynchronously.
  534. * @list: the wait list to wait on
  535. * @cl: the closure that is doing the waiting
  536. * @condition: a C expression for the event to wait for
  537. *
  538. * If the closure is in blocking mode, sleeps until the @condition evaluates to
  539. * true - exactly like wait_event().
  540. *
  541. * If the closure is not in blocking mode, waits asynchronously; if the
  542. * condition is currently false the @cl is put onto @list and returns. @list
  543. * owns a refcount on @cl; closure_sync() or continue_at() may be used later to
  544. * wait for another thread to wake up @list, which drops the refcount on @cl.
  545. *
  546. * Returns the value of @condition; @cl will be on @list iff @condition was
  547. * false.
  548. *
  549. * closure_wake_up(@list) must be called after changing any variable that could
  550. * cause @condition to become true.
  551. */
  552. #define closure_wait_event(list, cl, condition) \
  553. __closure_wait_event(list, cl, condition, closure_blocking(cl))
  554. #define closure_wait_event_async(list, cl, condition) \
  555. __closure_wait_event(list, cl, condition, false)
  556. #define closure_wait_event_sync(list, cl, condition) \
  557. __closure_wait_event(list, cl, condition, true)
  558. static inline void set_closure_fn(struct closure *cl, closure_fn *fn,
  559. struct workqueue_struct *wq)
  560. {
  561. BUG_ON(object_is_on_stack(cl));
  562. closure_set_ip(cl);
  563. cl->fn = fn;
  564. cl->wq = wq;
  565. /* between atomic_dec() in closure_put() */
  566. smp_mb__before_atomic_dec();
  567. }
  568. #define continue_at(_cl, _fn, _wq) \
  569. do { \
  570. set_closure_fn(_cl, _fn, _wq); \
  571. closure_sub(_cl, CLOSURE_RUNNING + 1); \
  572. return; \
  573. } while (0)
  574. #define closure_return(_cl) continue_at((_cl), NULL, NULL)
  575. #define continue_at_nobarrier(_cl, _fn, _wq) \
  576. do { \
  577. set_closure_fn(_cl, _fn, _wq); \
  578. closure_queue(cl); \
  579. return; \
  580. } while (0)
  581. #define closure_return_with_destructor(_cl, _destructor) \
  582. do { \
  583. set_closure_fn(_cl, _destructor, NULL); \
  584. closure_sub(_cl, CLOSURE_RUNNING - CLOSURE_DESTRUCTOR + 1); \
  585. return; \
  586. } while (0)
  587. static inline void closure_call(struct closure *cl, closure_fn fn,
  588. struct workqueue_struct *wq,
  589. struct closure *parent)
  590. {
  591. closure_init(cl, parent);
  592. continue_at_nobarrier(cl, fn, wq);
  593. }
  594. static inline void closure_trylock_call(struct closure *cl, closure_fn fn,
  595. struct workqueue_struct *wq,
  596. struct closure *parent)
  597. {
  598. if (closure_trylock(cl, parent))
  599. continue_at_nobarrier(cl, fn, wq);
  600. }
  601. #endif /* _LINUX_CLOSURE_H */