lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #ifndef _LINUX_WAIT_SIMPLE_H |
| 2 | #define _LINUX_WAIT_SIMPLE_H |
| 3 | |
| 4 | #include <linux/spinlock.h> |
| 5 | #include <linux/list.h> |
| 6 | |
| 7 | #include <asm/current.h> |
| 8 | |
| 9 | struct swaiter { |
| 10 | struct task_struct *task; |
| 11 | struct list_head node; |
| 12 | }; |
| 13 | |
| 14 | #define DEFINE_SWAITER(name) \ |
| 15 | struct swaiter name = { \ |
| 16 | .task = current, \ |
| 17 | .node = LIST_HEAD_INIT((name).node), \ |
| 18 | } |
| 19 | |
| 20 | struct swait_head { |
| 21 | raw_spinlock_t lock; |
| 22 | struct list_head list; |
| 23 | }; |
| 24 | |
| 25 | #define SWAIT_HEAD_INITIALIZER(name) { \ |
| 26 | .lock = __RAW_SPIN_LOCK_UNLOCKED(name.lock), \ |
| 27 | .list = LIST_HEAD_INIT((name).list), \ |
| 28 | } |
| 29 | |
| 30 | #define DEFINE_SWAIT_HEAD(name) \ |
| 31 | struct swait_head name = SWAIT_HEAD_INITIALIZER(name) |
| 32 | |
| 33 | extern void __init_swait_head(struct swait_head *h, struct lock_class_key *key); |
| 34 | |
| 35 | #define init_swait_head(swh) \ |
| 36 | do { \ |
| 37 | static struct lock_class_key __key; \ |
| 38 | \ |
| 39 | __init_swait_head((swh), &__key); \ |
| 40 | } while (0) |
| 41 | |
| 42 | /* |
| 43 | * Waiter functions |
| 44 | */ |
| 45 | extern void swait_prepare_locked(struct swait_head *head, struct swaiter *w); |
| 46 | extern void swait_prepare(struct swait_head *head, struct swaiter *w, int state); |
| 47 | extern void swait_finish_locked(struct swait_head *head, struct swaiter *w); |
| 48 | extern void swait_finish(struct swait_head *head, struct swaiter *w); |
| 49 | |
| 50 | /* |
| 51 | * Wakeup functions |
| 52 | */ |
| 53 | extern unsigned int __swait_wake(struct swait_head *head, unsigned int state, unsigned int num); |
| 54 | extern unsigned int __swait_wake_locked(struct swait_head *head, unsigned int state, unsigned int num); |
| 55 | |
| 56 | #define swait_wake(head) __swait_wake(head, TASK_NORMAL, 1) |
| 57 | #define swait_wake_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 1) |
| 58 | #define swait_wake_all(head) __swait_wake(head, TASK_NORMAL, 0) |
| 59 | #define swait_wake_all_interruptible(head) __swait_wake(head, TASK_INTERRUPTIBLE, 0) |
| 60 | |
| 61 | /* |
| 62 | * Event API |
| 63 | */ |
| 64 | #define __swait_event(wq, condition) \ |
| 65 | do { \ |
| 66 | DEFINE_SWAITER(__wait); \ |
| 67 | \ |
| 68 | for (;;) { \ |
| 69 | swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ |
| 70 | if (condition) \ |
| 71 | break; \ |
| 72 | schedule(); \ |
| 73 | } \ |
| 74 | swait_finish(&wq, &__wait); \ |
| 75 | } while (0) |
| 76 | |
| 77 | /** |
| 78 | * swait_event - sleep until a condition gets true |
| 79 | * @wq: the waitqueue to wait on |
| 80 | * @condition: a C expression for the event to wait for |
| 81 | * |
| 82 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 83 | * @condition evaluates to true. The @condition is checked each time |
| 84 | * the waitqueue @wq is woken up. |
| 85 | * |
| 86 | * wake_up() has to be called after changing any variable that could |
| 87 | * change the result of the wait condition. |
| 88 | */ |
| 89 | #define swait_event(wq, condition) \ |
| 90 | do { \ |
| 91 | if (condition) \ |
| 92 | break; \ |
| 93 | __swait_event(wq, condition); \ |
| 94 | } while (0) |
| 95 | |
| 96 | #define __swait_event_interruptible(wq, condition, ret) \ |
| 97 | do { \ |
| 98 | DEFINE_SWAITER(__wait); \ |
| 99 | \ |
| 100 | for (;;) { \ |
| 101 | swait_prepare(&wq, &__wait, TASK_INTERRUPTIBLE); \ |
| 102 | if (condition) \ |
| 103 | break; \ |
| 104 | if (signal_pending(current)) { \ |
| 105 | ret = -ERESTARTSYS; \ |
| 106 | break; \ |
| 107 | } \ |
| 108 | schedule(); \ |
| 109 | } \ |
| 110 | swait_finish(&wq, &__wait); \ |
| 111 | } while (0) |
| 112 | |
| 113 | /** |
| 114 | * swait_event_interruptible - sleep until a condition gets true |
| 115 | * @wq: the waitqueue to wait on |
| 116 | * @condition: a C expression for the event to wait for |
| 117 | * |
| 118 | * The process is put to sleep (TASK_INTERRUPTIBLE) until the |
| 119 | * @condition evaluates to true. The @condition is checked each time |
| 120 | * the waitqueue @wq is woken up. |
| 121 | * |
| 122 | * wake_up() has to be called after changing any variable that could |
| 123 | * change the result of the wait condition. |
| 124 | */ |
| 125 | #define swait_event_interruptible(wq, condition) \ |
| 126 | ({ \ |
| 127 | int __ret = 0; \ |
| 128 | if (!(condition)) \ |
| 129 | __swait_event_interruptible(wq, condition, __ret); \ |
| 130 | __ret; \ |
| 131 | }) |
| 132 | |
| 133 | #define __swait_event_timeout(wq, condition, ret) \ |
| 134 | do { \ |
| 135 | DEFINE_SWAITER(__wait); \ |
| 136 | \ |
| 137 | for (;;) { \ |
| 138 | swait_prepare(&wq, &__wait, TASK_UNINTERRUPTIBLE); \ |
| 139 | if (condition) \ |
| 140 | break; \ |
| 141 | ret = schedule_timeout(ret); \ |
| 142 | if (!ret) \ |
| 143 | break; \ |
| 144 | } \ |
| 145 | swait_finish(&wq, &__wait); \ |
| 146 | } while (0) |
| 147 | |
| 148 | /** |
| 149 | * swait_event_timeout - sleep until a condition gets true or a timeout elapses |
| 150 | * @wq: the waitqueue to wait on |
| 151 | * @condition: a C expression for the event to wait for |
| 152 | * @timeout: timeout, in jiffies |
| 153 | * |
| 154 | * The process is put to sleep (TASK_UNINTERRUPTIBLE) until the |
| 155 | * @condition evaluates to true. The @condition is checked each time |
| 156 | * the waitqueue @wq is woken up. |
| 157 | * |
| 158 | * wake_up() has to be called after changing any variable that could |
| 159 | * change the result of the wait condition. |
| 160 | * |
| 161 | * The function returns 0 if the @timeout elapsed, and the remaining |
| 162 | * jiffies if the condition evaluated to true before the timeout elapsed. |
| 163 | */ |
| 164 | #define swait_event_timeout(wq, condition, timeout) \ |
| 165 | ({ \ |
| 166 | long __ret = timeout; \ |
| 167 | if (!(condition)) \ |
| 168 | __swait_event_timeout(wq, condition, __ret); \ |
| 169 | __ret; \ |
| 170 | }) |
| 171 | |
| 172 | #endif |