12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181 |
- /* SPDX-License-Identifier: GPL-2.0 */
- #ifndef _LINUX_WAIT_H
- #define _LINUX_WAIT_H
- /*
- * Linux wait queue related types and methods
- */
- #include <linux/list.h>
- #include <linux/stddef.h>
- #include <linux/spinlock.h>
- #include <asm/current.h>
- #include <uapi/linux/wait.h>
- typedef struct wait_queue_entry wait_queue_entry_t;
- typedef int (*wait_queue_func_t)(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
- int default_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int flags, void *key);
- /* wait_queue_entry::flags */
- #define WQ_FLAG_EXCLUSIVE 0x01
- #define WQ_FLAG_WOKEN 0x02
- #define WQ_FLAG_BOOKMARK 0x04
- #define WQ_FLAG_CUSTOM 0x08
- #define WQ_FLAG_DONE 0x10
- /*
- * A single wait-queue entry structure:
- */
- struct wait_queue_entry {
- unsigned int flags;
- void *private;
- wait_queue_func_t func;
- struct list_head entry;
- };
- struct wait_queue_head {
- spinlock_t lock;
- struct list_head head;
- };
- typedef struct wait_queue_head wait_queue_head_t;
- struct task_struct;
- /*
- * Macros for declaration and initialisaton of the datatypes
- */
- #define __WAITQUEUE_INITIALIZER(name, tsk) { \
- .private = tsk, \
- .func = default_wake_function, \
- .entry = { NULL, NULL } }
- #define DECLARE_WAITQUEUE(name, tsk) \
- struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)
- #define __WAIT_QUEUE_HEAD_INITIALIZER(name) { \
- .lock = __SPIN_LOCK_UNLOCKED(name.lock), \
- .head = { &(name).head, &(name).head } }
- #define DECLARE_WAIT_QUEUE_HEAD(name) \
- struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
- extern void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *);
- #define init_waitqueue_head(wq_head) \
- do { \
- static struct lock_class_key __key; \
- \
- __init_waitqueue_head((wq_head), #wq_head, &__key); \
- } while (0)
- #ifdef CONFIG_LOCKDEP
- # define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name) \
- ({ init_waitqueue_head(&name); name; })
- # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) \
- struct wait_queue_head name = __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
- #else
- # define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name) DECLARE_WAIT_QUEUE_HEAD(name)
- #endif
- static inline void init_waitqueue_entry(struct wait_queue_entry *wq_entry, struct task_struct *p)
- {
- wq_entry->flags = 0;
- wq_entry->private = p;
- wq_entry->func = default_wake_function;
- }
- static inline void
- init_waitqueue_func_entry(struct wait_queue_entry *wq_entry, wait_queue_func_t func)
- {
- wq_entry->flags = 0;
- wq_entry->private = NULL;
- wq_entry->func = func;
- }
- /**
- * waitqueue_active -- locklessly test for waiters on the queue
- * @wq_head: the waitqueue to test for waiters
- *
- * returns true if the wait list is not empty
- *
- * NOTE: this function is lockless and requires care, incorrect usage _will_
- * lead to sporadic and non-obvious failure.
- *
- * Use either while holding wait_queue_head::lock or when used for wakeups
- * with an extra smp_mb() like::
- *
- * CPU0 - waker CPU1 - waiter
- *
- * for (;;) {
- * @cond = true; prepare_to_wait(&wq_head, &wait, state);
- * smp_mb(); // smp_mb() from set_current_state()
- * if (waitqueue_active(wq_head)) if (@cond)
- * wake_up(wq_head); break;
- * schedule();
- * }
- * finish_wait(&wq_head, &wait);
- *
- * Because without the explicit smp_mb() it's possible for the
- * waitqueue_active() load to get hoisted over the @cond store such that we'll
- * observe an empty wait list while the waiter might not observe @cond.
- *
- * Also note that this 'optimization' trades a spin_lock() for an smp_mb(),
- * which (when the lock is uncontended) are of roughly equal cost.
- */
- static inline int waitqueue_active(struct wait_queue_head *wq_head)
- {
- return !list_empty(&wq_head->head);
- }
- /**
- * wq_has_single_sleeper - check if there is only one sleeper
- * @wq_head: wait queue head
- *
- * Returns true of wq_head has only one sleeper on the list.
- *
- * Please refer to the comment for waitqueue_active.
- */
- static inline bool wq_has_single_sleeper(struct wait_queue_head *wq_head)
- {
- return list_is_singular(&wq_head->head);
- }
- /**
- * wq_has_sleeper - check if there are any waiting processes
- * @wq_head: wait queue head
- *
- * Returns true if wq_head has waiting processes
- *
- * Please refer to the comment for waitqueue_active.
- */
- static inline bool wq_has_sleeper(struct wait_queue_head *wq_head)
- {
- /*
- * We need to be sure we are in sync with the
- * add_wait_queue modifications to the wait queue.
- *
- * This memory barrier should be paired with one on the
- * waiting side.
- */
- smp_mb();
- return waitqueue_active(wq_head);
- }
- extern void add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
- extern void add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
- extern void remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
- static inline void __add_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
- {
- list_add(&wq_entry->entry, &wq_head->head);
- }
- /*
- * Used for wake-one threads:
- */
- static inline void
- __add_wait_queue_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
- {
- wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
- __add_wait_queue(wq_head, wq_entry);
- }
- static inline void __add_wait_queue_entry_tail(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
- {
- list_add_tail(&wq_entry->entry, &wq_head->head);
- }
- static inline void
- __add_wait_queue_entry_tail_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
- {
- wq_entry->flags |= WQ_FLAG_EXCLUSIVE;
- __add_wait_queue_entry_tail(wq_head, wq_entry);
- }
- static inline void
- __remove_wait_queue(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry)
- {
- list_del(&wq_entry->entry);
- }
- void __wake_up(struct wait_queue_head *wq_head, unsigned int mode, int nr, void *key);
- void __wake_up_locked_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
- void __wake_up_locked_key_bookmark(struct wait_queue_head *wq_head,
- unsigned int mode, void *key, wait_queue_entry_t *bookmark);
- void __wake_up_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
- void __wake_up_locked_sync_key(struct wait_queue_head *wq_head, unsigned int mode, void *key);
- void __wake_up_locked(struct wait_queue_head *wq_head, unsigned int mode, int nr);
- void __wake_up_sync(struct wait_queue_head *wq_head, unsigned int mode);
- void __wake_up_pollfree(struct wait_queue_head *wq_head);
- #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
- #define wake_up_nr(x, nr) __wake_up(x, TASK_NORMAL, nr, NULL)
- #define wake_up_all(x) __wake_up(x, TASK_NORMAL, 0, NULL)
- #define wake_up_locked(x) __wake_up_locked((x), TASK_NORMAL, 1)
- #define wake_up_all_locked(x) __wake_up_locked((x), TASK_NORMAL, 0)
- #define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
- #define wake_up_interruptible_nr(x, nr) __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
- #define wake_up_interruptible_all(x) __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
- #define wake_up_interruptible_sync(x) __wake_up_sync((x), TASK_INTERRUPTIBLE)
- /*
- * Wakeup macros to be used to report events to the targets.
- */
- #define poll_to_key(m) ((void *)(__force uintptr_t)(__poll_t)(m))
- #define key_to_poll(m) ((__force __poll_t)(uintptr_t)(void *)(m))
- #define wake_up_poll(x, m) \
- __wake_up(x, TASK_NORMAL, 1, poll_to_key(m))
- #define wake_up_locked_poll(x, m) \
- __wake_up_locked_key((x), TASK_NORMAL, poll_to_key(m))
- #define wake_up_interruptible_poll(x, m) \
- __wake_up(x, TASK_INTERRUPTIBLE, 1, poll_to_key(m))
- #define wake_up_interruptible_sync_poll(x, m) \
- __wake_up_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
- #define wake_up_interruptible_sync_poll_locked(x, m) \
- __wake_up_locked_sync_key((x), TASK_INTERRUPTIBLE, poll_to_key(m))
- /**
- * wake_up_pollfree - signal that a polled waitqueue is going away
- * @wq_head: the wait queue head
- *
- * In the very rare cases where a ->poll() implementation uses a waitqueue whose
- * lifetime is tied to a task rather than to the 'struct file' being polled,
- * this function must be called before the waitqueue is freed so that
- * non-blocking polls (e.g. epoll) are notified that the queue is going away.
- *
- * The caller must also RCU-delay the freeing of the wait_queue_head, e.g. via
- * an explicit synchronize_rcu() or call_rcu(), or via SLAB_TYPESAFE_BY_RCU.
- */
- static inline void wake_up_pollfree(struct wait_queue_head *wq_head)
- {
- /*
- * For performance reasons, we don't always take the queue lock here.
- * Therefore, we might race with someone removing the last entry from
- * the queue, and proceed while they still hold the queue lock.
- * However, rcu_read_lock() is required to be held in such cases, so we
- * can safely proceed with an RCU-delayed free.
- */
- if (waitqueue_active(wq_head))
- __wake_up_pollfree(wq_head);
- }
- #define ___wait_cond_timeout(condition) \
- ({ \
- bool __cond = (condition); \
- if (__cond && !__ret) \
- __ret = 1; \
- __cond || !__ret; \
- })
- #define ___wait_is_interruptible(state) \
- (!__builtin_constant_p(state) || \
- state == TASK_INTERRUPTIBLE || state == TASK_KILLABLE) \
- extern void init_wait_entry(struct wait_queue_entry *wq_entry, int flags);
- /*
- * The below macro ___wait_event() has an explicit shadow of the __ret
- * variable when used from the wait_event_*() macros.
- *
- * This is so that both can use the ___wait_cond_timeout() construct
- * to wrap the condition.
- *
- * The type inconsistency of the wait_event_*() __ret variable is also
- * on purpose; we use long where we can return timeout values and int
- * otherwise.
- */
- #define ___wait_event(wq_head, condition, state, exclusive, ret, cmd) \
- ({ \
- __label__ __out; \
- struct wait_queue_entry __wq_entry; \
- long __ret = ret; /* explicit shadow */ \
- \
- init_wait_entry(&__wq_entry, exclusive ? WQ_FLAG_EXCLUSIVE : 0); \
- for (;;) { \
- long __int = prepare_to_wait_event(&wq_head, &__wq_entry, state);\
- \
- if (condition) \
- break; \
- \
- if (___wait_is_interruptible(state) && __int) { \
- __ret = __int; \
- goto __out; \
- } \
- \
- cmd; \
- } \
- finish_wait(&wq_head, &__wq_entry); \
- __out: __ret; \
- })
- #define __wait_event(wq_head, condition) \
- (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
- schedule())
- /**
- * wait_event - sleep until a condition gets true
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
- * @condition evaluates to true. The @condition is checked each time
- * the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- */
- #define wait_event(wq_head, condition) \
- do { \
- might_sleep(); \
- if (condition) \
- break; \
- __wait_event(wq_head, condition); \
- } while (0)
- #define __io_wait_event(wq_head, condition) \
- (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
- io_schedule())
- /*
- * io_wait_event() -- like wait_event() but with io_schedule()
- */
- #define io_wait_event(wq_head, condition) \
- do { \
- might_sleep(); \
- if (condition) \
- break; \
- __io_wait_event(wq_head, condition); \
- } while (0)
- #define __wait_event_freezable(wq_head, condition) \
- ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
- freezable_schedule())
- /**
- * wait_event_freezable - sleep (or freeze) until a condition gets true
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE -- so as not to contribute
- * to system load) until the @condition evaluates to true. The
- * @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- */
- #define wait_event_freezable(wq_head, condition) \
- ({ \
- int __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_freezable(wq_head, condition); \
- __ret; \
- })
- #define __wait_event_timeout(wq_head, condition, timeout) \
- ___wait_event(wq_head, ___wait_cond_timeout(condition), \
- TASK_UNINTERRUPTIBLE, 0, timeout, \
- __ret = schedule_timeout(__ret))
- /**
- * wait_event_timeout - sleep until a condition gets true or a timeout elapses
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @timeout: timeout, in jiffies
- *
- * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
- * @condition evaluates to true. The @condition is checked each time
- * the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * Returns:
- * 0 if the @condition evaluated to %false after the @timeout elapsed,
- * 1 if the @condition evaluated to %true after the @timeout elapsed,
- * or the remaining jiffies (at least 1) if the @condition evaluated
- * to %true before the @timeout elapsed.
- */
- #define wait_event_timeout(wq_head, condition, timeout) \
- ({ \
- long __ret = timeout; \
- might_sleep(); \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_timeout(wq_head, condition, timeout); \
- __ret; \
- })
- #define __wait_event_freezable_timeout(wq_head, condition, timeout) \
- ___wait_event(wq_head, ___wait_cond_timeout(condition), \
- TASK_INTERRUPTIBLE, 0, timeout, \
- __ret = freezable_schedule_timeout(__ret))
- /*
- * like wait_event_timeout() -- except it uses TASK_INTERRUPTIBLE to avoid
- * increasing load and is freezable.
- */
- #define wait_event_freezable_timeout(wq_head, condition, timeout) \
- ({ \
- long __ret = timeout; \
- might_sleep(); \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_freezable_timeout(wq_head, condition, timeout); \
- __ret; \
- })
- #define __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
- (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 1, 0, \
- cmd1; schedule(); cmd2)
- /*
- * Just like wait_event_cmd(), except it sets exclusive flag
- */
- #define wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2) \
- do { \
- if (condition) \
- break; \
- __wait_event_exclusive_cmd(wq_head, condition, cmd1, cmd2); \
- } while (0)
- #define __wait_event_cmd(wq_head, condition, cmd1, cmd2) \
- (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
- cmd1; schedule(); cmd2)
- /**
- * wait_event_cmd - sleep until a condition gets true
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @cmd1: the command will be executed before sleep
- * @cmd2: the command will be executed after sleep
- *
- * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
- * @condition evaluates to true. The @condition is checked each time
- * the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- */
- #define wait_event_cmd(wq_head, condition, cmd1, cmd2) \
- do { \
- if (condition) \
- break; \
- __wait_event_cmd(wq_head, condition, cmd1, cmd2); \
- } while (0)
- #define __wait_event_interruptible(wq_head, condition) \
- ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
- schedule())
- /**
- * wait_event_interruptible - sleep until a condition gets true
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function will return -ERESTARTSYS if it was interrupted by a
- * signal and 0 if @condition evaluated to true.
- */
- #define wait_event_interruptible(wq_head, condition) \
- ({ \
- int __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_interruptible(wq_head, condition); \
- __ret; \
- })
- #define __wait_event_interruptible_timeout(wq_head, condition, timeout) \
- ___wait_event(wq_head, ___wait_cond_timeout(condition), \
- TASK_INTERRUPTIBLE, 0, timeout, \
- __ret = schedule_timeout(__ret))
- /**
- * wait_event_interruptible_timeout - sleep until a condition gets true or a timeout elapses
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @timeout: timeout, in jiffies
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * Returns:
- * 0 if the @condition evaluated to %false after the @timeout elapsed,
- * 1 if the @condition evaluated to %true after the @timeout elapsed,
- * the remaining jiffies (at least 1) if the @condition evaluated
- * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
- * interrupted by a signal.
- */
- #define wait_event_interruptible_timeout(wq_head, condition, timeout) \
- ({ \
- long __ret = timeout; \
- might_sleep(); \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_interruptible_timeout(wq_head, \
- condition, timeout); \
- __ret; \
- })
- #define __wait_event_hrtimeout(wq_head, condition, timeout, state) \
- ({ \
- int __ret = 0; \
- struct hrtimer_sleeper __t; \
- \
- hrtimer_init_sleeper_on_stack(&__t, CLOCK_MONOTONIC, \
- HRTIMER_MODE_REL); \
- if ((timeout) != KTIME_MAX) \
- hrtimer_start_range_ns(&__t.timer, timeout, \
- current->timer_slack_ns, \
- HRTIMER_MODE_REL); \
- \
- __ret = ___wait_event(wq_head, condition, state, 0, 0, \
- if (!__t.task) { \
- __ret = -ETIME; \
- break; \
- } \
- schedule()); \
- \
- hrtimer_cancel(&__t.timer); \
- destroy_hrtimer_on_stack(&__t.timer); \
- __ret; \
- })
- /**
- * wait_event_hrtimeout - sleep until a condition gets true or a timeout elapses
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @timeout: timeout, as a ktime_t
- *
- * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function returns 0 if @condition became true, or -ETIME if the timeout
- * elapsed.
- */
- #define wait_event_hrtimeout(wq_head, condition, timeout) \
- ({ \
- int __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_hrtimeout(wq_head, condition, timeout, \
- TASK_UNINTERRUPTIBLE); \
- __ret; \
- })
- /**
- * wait_event_interruptible_hrtimeout - sleep until a condition gets true or a timeout elapses
- * @wq: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @timeout: timeout, as a ktime_t
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function returns 0 if @condition became true, -ERESTARTSYS if it was
- * interrupted by a signal, or -ETIME if the timeout elapsed.
- */
- #define wait_event_interruptible_hrtimeout(wq, condition, timeout) \
- ({ \
- long __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_hrtimeout(wq, condition, timeout, \
- TASK_INTERRUPTIBLE); \
- __ret; \
- })
- #define __wait_event_interruptible_exclusive(wq, condition) \
- ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
- schedule())
- #define wait_event_interruptible_exclusive(wq, condition) \
- ({ \
- int __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_interruptible_exclusive(wq, condition); \
- __ret; \
- })
- #define __wait_event_killable_exclusive(wq, condition) \
- ___wait_event(wq, condition, TASK_KILLABLE, 1, 0, \
- schedule())
- #define wait_event_killable_exclusive(wq, condition) \
- ({ \
- int __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_killable_exclusive(wq, condition); \
- __ret; \
- })
- #define __wait_event_freezable_exclusive(wq, condition) \
- ___wait_event(wq, condition, TASK_INTERRUPTIBLE, 1, 0, \
- freezable_schedule())
- #define wait_event_freezable_exclusive(wq, condition) \
- ({ \
- int __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_freezable_exclusive(wq, condition); \
- __ret; \
- })
- /**
- * wait_event_idle - wait for a condition without contributing to system load
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_IDLE) until the
- * @condition evaluates to true.
- * The @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- */
- #define wait_event_idle(wq_head, condition) \
- do { \
- might_sleep(); \
- if (!(condition)) \
- ___wait_event(wq_head, condition, TASK_IDLE, 0, 0, schedule()); \
- } while (0)
- /**
- * wait_event_idle_exclusive - wait for a condition with contributing to system load
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_IDLE) until the
- * @condition evaluates to true.
- * The @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
- * set thus if other processes wait on the same list, when this
- * process is woken further processes are not considered.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- */
- #define wait_event_idle_exclusive(wq_head, condition) \
- do { \
- might_sleep(); \
- if (!(condition)) \
- ___wait_event(wq_head, condition, TASK_IDLE, 1, 0, schedule()); \
- } while (0)
- #define __wait_event_idle_timeout(wq_head, condition, timeout) \
- ___wait_event(wq_head, ___wait_cond_timeout(condition), \
- TASK_IDLE, 0, timeout, \
- __ret = schedule_timeout(__ret))
- /**
- * wait_event_idle_timeout - sleep without load until a condition becomes true or a timeout elapses
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @timeout: timeout, in jiffies
- *
- * The process is put to sleep (TASK_IDLE) until the
- * @condition evaluates to true. The @condition is checked each time
- * the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * Returns:
- * 0 if the @condition evaluated to %false after the @timeout elapsed,
- * 1 if the @condition evaluated to %true after the @timeout elapsed,
- * or the remaining jiffies (at least 1) if the @condition evaluated
- * to %true before the @timeout elapsed.
- */
- #define wait_event_idle_timeout(wq_head, condition, timeout) \
- ({ \
- long __ret = timeout; \
- might_sleep(); \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_idle_timeout(wq_head, condition, timeout); \
- __ret; \
- })
- #define __wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
- ___wait_event(wq_head, ___wait_cond_timeout(condition), \
- TASK_IDLE, 1, timeout, \
- __ret = schedule_timeout(__ret))
- /**
- * wait_event_idle_exclusive_timeout - sleep without load until a condition becomes true or a timeout elapses
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @timeout: timeout, in jiffies
- *
- * The process is put to sleep (TASK_IDLE) until the
- * @condition evaluates to true. The @condition is checked each time
- * the waitqueue @wq_head is woken up.
- *
- * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
- * set thus if other processes wait on the same list, when this
- * process is woken further processes are not considered.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * Returns:
- * 0 if the @condition evaluated to %false after the @timeout elapsed,
- * 1 if the @condition evaluated to %true after the @timeout elapsed,
- * or the remaining jiffies (at least 1) if the @condition evaluated
- * to %true before the @timeout elapsed.
- */
- #define wait_event_idle_exclusive_timeout(wq_head, condition, timeout) \
- ({ \
- long __ret = timeout; \
- might_sleep(); \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_idle_exclusive_timeout(wq_head, condition, timeout);\
- __ret; \
- })
- extern int do_wait_intr(wait_queue_head_t *, wait_queue_entry_t *);
- extern int do_wait_intr_irq(wait_queue_head_t *, wait_queue_entry_t *);
- #define __wait_event_interruptible_locked(wq, condition, exclusive, fn) \
- ({ \
- int __ret; \
- DEFINE_WAIT(__wait); \
- if (exclusive) \
- __wait.flags |= WQ_FLAG_EXCLUSIVE; \
- do { \
- __ret = fn(&(wq), &__wait); \
- if (__ret) \
- break; \
- } while (!(condition)); \
- __remove_wait_queue(&(wq), &__wait); \
- __set_current_state(TASK_RUNNING); \
- __ret; \
- })
- /**
- * wait_event_interruptible_locked - sleep until a condition gets true
- * @wq: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq is woken up.
- *
- * It must be called with wq.lock being held. This spinlock is
- * unlocked while sleeping but @condition testing is done while lock
- * is held and when this macro exits the lock is held.
- *
- * The lock is locked/unlocked using spin_lock()/spin_unlock()
- * functions which must match the way they are locked/unlocked outside
- * of this macro.
- *
- * wake_up_locked() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function will return -ERESTARTSYS if it was interrupted by a
- * signal and 0 if @condition evaluated to true.
- */
- #define wait_event_interruptible_locked(wq, condition) \
- ((condition) \
- ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr))
- /**
- * wait_event_interruptible_locked_irq - sleep until a condition gets true
- * @wq: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq is woken up.
- *
- * It must be called with wq.lock being held. This spinlock is
- * unlocked while sleeping but @condition testing is done while lock
- * is held and when this macro exits the lock is held.
- *
- * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
- * functions which must match the way they are locked/unlocked outside
- * of this macro.
- *
- * wake_up_locked() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function will return -ERESTARTSYS if it was interrupted by a
- * signal and 0 if @condition evaluated to true.
- */
- #define wait_event_interruptible_locked_irq(wq, condition) \
- ((condition) \
- ? 0 : __wait_event_interruptible_locked(wq, condition, 0, do_wait_intr_irq))
- /**
- * wait_event_interruptible_exclusive_locked - sleep exclusively until a condition gets true
- * @wq: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq is woken up.
- *
- * It must be called with wq.lock being held. This spinlock is
- * unlocked while sleeping but @condition testing is done while lock
- * is held and when this macro exits the lock is held.
- *
- * The lock is locked/unlocked using spin_lock()/spin_unlock()
- * functions which must match the way they are locked/unlocked outside
- * of this macro.
- *
- * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
- * set thus when other process waits process on the list if this
- * process is awaken further processes are not considered.
- *
- * wake_up_locked() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function will return -ERESTARTSYS if it was interrupted by a
- * signal and 0 if @condition evaluated to true.
- */
- #define wait_event_interruptible_exclusive_locked(wq, condition) \
- ((condition) \
- ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr))
- /**
- * wait_event_interruptible_exclusive_locked_irq - sleep until a condition gets true
- * @wq: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq is woken up.
- *
- * It must be called with wq.lock being held. This spinlock is
- * unlocked while sleeping but @condition testing is done while lock
- * is held and when this macro exits the lock is held.
- *
- * The lock is locked/unlocked using spin_lock_irq()/spin_unlock_irq()
- * functions which must match the way they are locked/unlocked outside
- * of this macro.
- *
- * The process is put on the wait queue with an WQ_FLAG_EXCLUSIVE flag
- * set thus when other process waits process on the list if this
- * process is awaken further processes are not considered.
- *
- * wake_up_locked() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function will return -ERESTARTSYS if it was interrupted by a
- * signal and 0 if @condition evaluated to true.
- */
- #define wait_event_interruptible_exclusive_locked_irq(wq, condition) \
- ((condition) \
- ? 0 : __wait_event_interruptible_locked(wq, condition, 1, do_wait_intr_irq))
- #define __wait_event_killable(wq, condition) \
- ___wait_event(wq, condition, TASK_KILLABLE, 0, 0, schedule())
- /**
- * wait_event_killable - sleep until a condition gets true
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- *
- * The process is put to sleep (TASK_KILLABLE) until the
- * @condition evaluates to true or a signal is received.
- * The @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * The function will return -ERESTARTSYS if it was interrupted by a
- * signal and 0 if @condition evaluated to true.
- */
- #define wait_event_killable(wq_head, condition) \
- ({ \
- int __ret = 0; \
- might_sleep(); \
- if (!(condition)) \
- __ret = __wait_event_killable(wq_head, condition); \
- __ret; \
- })
- #define __wait_event_killable_timeout(wq_head, condition, timeout) \
- ___wait_event(wq_head, ___wait_cond_timeout(condition), \
- TASK_KILLABLE, 0, timeout, \
- __ret = schedule_timeout(__ret))
- /**
- * wait_event_killable_timeout - sleep until a condition gets true or a timeout elapses
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @timeout: timeout, in jiffies
- *
- * The process is put to sleep (TASK_KILLABLE) until the
- * @condition evaluates to true or a kill signal is received.
- * The @condition is checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * Returns:
- * 0 if the @condition evaluated to %false after the @timeout elapsed,
- * 1 if the @condition evaluated to %true after the @timeout elapsed,
- * the remaining jiffies (at least 1) if the @condition evaluated
- * to %true before the @timeout elapsed, or -%ERESTARTSYS if it was
- * interrupted by a kill signal.
- *
- * Only kill signals interrupt this process.
- */
- #define wait_event_killable_timeout(wq_head, condition, timeout) \
- ({ \
- long __ret = timeout; \
- might_sleep(); \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_killable_timeout(wq_head, \
- condition, timeout); \
- __ret; \
- })
- #define __wait_event_lock_irq(wq_head, condition, lock, cmd) \
- (void)___wait_event(wq_head, condition, TASK_UNINTERRUPTIBLE, 0, 0, \
- spin_unlock_irq(&lock); \
- cmd; \
- schedule(); \
- spin_lock_irq(&lock))
- /**
- * wait_event_lock_irq_cmd - sleep until a condition gets true. The
- * condition is checked under the lock. This
- * is expected to be called with the lock
- * taken.
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @lock: a locked spinlock_t, which will be released before cmd
- * and schedule() and reacquired afterwards.
- * @cmd: a command which is invoked outside the critical section before
- * sleep
- *
- * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
- * @condition evaluates to true. The @condition is checked each time
- * the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * This is supposed to be called while holding the lock. The lock is
- * dropped before invoking the cmd and going to sleep and is reacquired
- * afterwards.
- */
- #define wait_event_lock_irq_cmd(wq_head, condition, lock, cmd) \
- do { \
- if (condition) \
- break; \
- __wait_event_lock_irq(wq_head, condition, lock, cmd); \
- } while (0)
- /**
- * wait_event_lock_irq - sleep until a condition gets true. The
- * condition is checked under the lock. This
- * is expected to be called with the lock
- * taken.
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @lock: a locked spinlock_t, which will be released before schedule()
- * and reacquired afterwards.
- *
- * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the
- * @condition evaluates to true. The @condition is checked each time
- * the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * This is supposed to be called while holding the lock. The lock is
- * dropped before going to sleep and is reacquired afterwards.
- */
- #define wait_event_lock_irq(wq_head, condition, lock) \
- do { \
- if (condition) \
- break; \
- __wait_event_lock_irq(wq_head, condition, lock, ); \
- } while (0)
- #define __wait_event_interruptible_lock_irq(wq_head, condition, lock, cmd) \
- ___wait_event(wq_head, condition, TASK_INTERRUPTIBLE, 0, 0, \
- spin_unlock_irq(&lock); \
- cmd; \
- schedule(); \
- spin_lock_irq(&lock))
- /**
- * wait_event_interruptible_lock_irq_cmd - sleep until a condition gets true.
- * The condition is checked under the lock. This is expected to
- * be called with the lock taken.
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @lock: a locked spinlock_t, which will be released before cmd and
- * schedule() and reacquired afterwards.
- * @cmd: a command which is invoked outside the critical section before
- * sleep
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or a signal is received. The @condition is
- * checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * This is supposed to be called while holding the lock. The lock is
- * dropped before invoking the cmd and going to sleep and is reacquired
- * afterwards.
- *
- * The macro will return -ERESTARTSYS if it was interrupted by a signal
- * and 0 if @condition evaluated to true.
- */
- #define wait_event_interruptible_lock_irq_cmd(wq_head, condition, lock, cmd) \
- ({ \
- int __ret = 0; \
- if (!(condition)) \
- __ret = __wait_event_interruptible_lock_irq(wq_head, \
- condition, lock, cmd); \
- __ret; \
- })
- /**
- * wait_event_interruptible_lock_irq - sleep until a condition gets true.
- * The condition is checked under the lock. This is expected
- * to be called with the lock taken.
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @lock: a locked spinlock_t, which will be released before schedule()
- * and reacquired afterwards.
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or signal is received. The @condition is
- * checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * This is supposed to be called while holding the lock. The lock is
- * dropped before going to sleep and is reacquired afterwards.
- *
- * The macro will return -ERESTARTSYS if it was interrupted by a signal
- * and 0 if @condition evaluated to true.
- */
- #define wait_event_interruptible_lock_irq(wq_head, condition, lock) \
- ({ \
- int __ret = 0; \
- if (!(condition)) \
- __ret = __wait_event_interruptible_lock_irq(wq_head, \
- condition, lock,); \
- __ret; \
- })
- #define __wait_event_lock_irq_timeout(wq_head, condition, lock, timeout, state) \
- ___wait_event(wq_head, ___wait_cond_timeout(condition), \
- state, 0, timeout, \
- spin_unlock_irq(&lock); \
- __ret = schedule_timeout(__ret); \
- spin_lock_irq(&lock));
- /**
- * wait_event_interruptible_lock_irq_timeout - sleep until a condition gets
- * true or a timeout elapses. The condition is checked under
- * the lock. This is expected to be called with the lock taken.
- * @wq_head: the waitqueue to wait on
- * @condition: a C expression for the event to wait for
- * @lock: a locked spinlock_t, which will be released before schedule()
- * and reacquired afterwards.
- * @timeout: timeout, in jiffies
- *
- * The process is put to sleep (TASK_INTERRUPTIBLE) until the
- * @condition evaluates to true or signal is received. The @condition is
- * checked each time the waitqueue @wq_head is woken up.
- *
- * wake_up() has to be called after changing any variable that could
- * change the result of the wait condition.
- *
- * This is supposed to be called while holding the lock. The lock is
- * dropped before going to sleep and is reacquired afterwards.
- *
- * The function returns 0 if the @timeout elapsed, -ERESTARTSYS if it
- * was interrupted by a signal, and the remaining jiffies otherwise
- * if the condition evaluated to true before the timeout elapsed.
- */
- #define wait_event_interruptible_lock_irq_timeout(wq_head, condition, lock, \
- timeout) \
- ({ \
- long __ret = timeout; \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_lock_irq_timeout( \
- wq_head, condition, lock, timeout, \
- TASK_INTERRUPTIBLE); \
- __ret; \
- })
- #define wait_event_lock_irq_timeout(wq_head, condition, lock, timeout) \
- ({ \
- long __ret = timeout; \
- if (!___wait_cond_timeout(condition)) \
- __ret = __wait_event_lock_irq_timeout( \
- wq_head, condition, lock, timeout, \
- TASK_UNINTERRUPTIBLE); \
- __ret; \
- })
- /*
- * Waitqueues which are removed from the waitqueue_head at wakeup time
- */
- void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
- bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
- long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state);
- void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry);
- long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout);
- int woken_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
- int autoremove_wake_function(struct wait_queue_entry *wq_entry, unsigned mode, int sync, void *key);
- #define DEFINE_WAIT_FUNC(name, function) \
- struct wait_queue_entry name = { \
- .private = current, \
- .func = function, \
- .entry = LIST_HEAD_INIT((name).entry), \
- }
- #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
- #define init_wait(wait) \
- do { \
- (wait)->private = current; \
- (wait)->func = autoremove_wake_function; \
- INIT_LIST_HEAD(&(wait)->entry); \
- (wait)->flags = 0; \
- } while (0)
- bool try_invoke_on_locked_down_task(struct task_struct *p, bool (*func)(struct task_struct *t, void *arg), void *arg);
- #endif /* _LINUX_WAIT_H */
|