1 /* SPDX-License-Identifier: GPL-2.0 */ 1 2 #ifndef _LINUX_SWAIT_H 3 #define _LINUX_SWAIT_H 4 5 #include <linux/list.h> 6 #include <linux/stddef.h> 7 #include <linux/spinlock.h> 8 #include <linux/wait.h> 9 #include <asm/current.h> 10 11 /* 12 * Simple waitqueues are semantically very dif 13 * (wait.h). The most important difference is 14 * for deterministic behaviour -- IOW it has s 15 * times. 16 * 17 * Mainly, this is accomplished by two things. 18 * from IRQ disabled, and dropping the lock up 19 * priority task a chance to run. 20 * 21 * Secondly, we had to drop a fair number of f 22 * code; notably: 23 * 24 * - mixing INTERRUPTIBLE and UNINTERRUPTIBLE 25 * all wakeups are TASK_NORMAL in order to 26 * sleeper state. 27 * 28 * - the !exclusive mode; because that leads 29 * exclusive. As such swake_up_one will onl 30 * 31 * - custom wake callback functions; because 32 * about random code. This also allows swai 33 * raw spinlock can be used for the swait q 34 * 35 * As a side effect of these; the data structu 36 * For all the above, note that simple wait qu 37 * very specific realtime constraints -- it is 38 * wait queues in most cases. 39 */ 40 41 struct task_struct; 42 43 struct swait_queue_head { 44 raw_spinlock_t lock; 45 struct list_head task_list; 46 }; 47 48 struct swait_queue { 49 struct task_struct *task; 50 struct list_head task_list; 51 }; 52 53 #define __SWAITQUEUE_INITIALIZER(name) { 54 .task = current, 55 .task_list = LIST_HEAD_INIT((name 56 } 57 58 #define DECLARE_SWAITQUEUE(name) 59 struct swait_queue name = __SWAITQUEUE 60 61 #define __SWAIT_QUEUE_HEAD_INITIALIZER(name) { 62 .lock = __RAW_SPIN_LOCK_UNLO 63 .task_list = LIST_HEAD_INIT((name 64 } 65 66 #define DECLARE_SWAIT_QUEUE_HEAD(name) 67 struct swait_queue_head name = __SWAIT 68 69 extern void __init_swait_queue_head(struct swa 70 struct loc 71 72 #define init_swait_queue_head(q) 73 do { 74 static struct lock_class_key _ 75 __init_swait_queue_head((q), # 76 } while (0) 77 78 #ifdef CONFIG_LOCKDEP 79 # define __SWAIT_QUEUE_HEAD_INIT_ONSTACK(name) 80 ({ init_swait_queue_head(&name); name; 81 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name 82 struct swait_queue_head name = __SWAIT 83 #else 84 # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name 85 DECLARE_SWAIT_QUEUE_HEAD(name) 86 #endif 87 88 /** 89 * swait_active -- locklessly test for waiters 90 * @wq: the waitqueue to test for waiters 91 * 92 * returns true if the wait list is not empty 93 * 94 * NOTE: this function is lockless and require 95 * lead to sporadic and non-obvious failure. 96 * 97 * NOTE2: this function has the same above imp 98 * 99 * Use either while holding swait_queue_head:: 100 * with an extra smp_mb() like: 101 * 102 * CPU0 - waker CPU1 - 103 * 104 * for (; 105 * @cond = true; prep 106 * smp_mb(); // s 107 * if (swait_active(wq_head)) if ( 108 * wake_up(wq_head); 109 * sche 110 * } 111 * finish 112 * 113 * Because without the explicit smp_mb() it's 114 * swait_active() load to get hoisted over the 115 * observe an empty wait list while the waiter 116 * This, in turn, can trigger missing wakeups. 117 * 118 * Also note that this 'optimization' trades a 119 * which (when the lock is uncontended) are of 120 */ 121 static inline int swait_active(struct swait_qu 122 { 123 return !list_empty(&wq->task_list); 124 } 125 126 /** 127 * swq_has_sleeper - check if there are any wa 128 * @wq: the waitqueue to test for waiters 129 * 130 * Returns true if @wq has waiting processes 131 * 132 * Please refer to the comment for swait_activ 133 */ 134 static inline bool swq_has_sleeper(struct swai 135 { 136 /* 137 * We need to be sure we are in sync w 138 * modifications to the wait queue (ta 139 * 140 * This memory barrier should be paire 141 * waiting side. 142 */ 143 smp_mb(); 144 return swait_active(wq); 145 } 146 147 extern void swake_up_one(struct swait_queue_he 148 extern void swake_up_all(struct swait_queue_he 149 extern void swake_up_locked(struct swait_queue 150 151 extern void prepare_to_swait_exclusive(struct 152 extern long prepare_to_swait_event(struct swai 153 154 extern void __finish_swait(struct swait_queue_ 155 extern void finish_swait(struct swait_queue_he 156 157 /* as per ___wait_event() but for swait, there 158 #define ___swait_event(wq, condition, state, r 159 ({ 160 __label__ __out; 161 struct swait_queue __wait; 162 long __ret = ret; 163 164 INIT_LIST_HEAD(&__wait.task_list); 165 for (;;) { 166 long __int = prepare_to_swait_ 167 168 if (condition) 169 break; 170 171 if (___wait_is_interruptible(s 172 __ret = __int; 173 goto __out; 174 } 175 176 cmd; 177 } 178 finish_swait(&wq, &__wait); 179 __out: __ret; 180 }) 181 182 #define __swait_event(wq, condition) 183 (void)___swait_event(wq, condition, TA 184 schedule()) 185 186 #define swait_event_exclusive(wq, condition) 187 do { 188 if (condition) 189 break; 190 __swait_event(wq, condition); 191 } while (0) 192 193 #define __swait_event_timeout(wq, condition, t 194 ___swait_event(wq, ___wait_cond_timeou 195 TASK_UNINTERRUPTIBLE, ti 196 __ret = schedule_timeout 197 198 #define swait_event_timeout_exclusive(wq, cond 199 ({ 200 long __ret = timeout; 201 if (!___wait_cond_timeout(condition)) 202 __ret = __swait_event_timeout( 203 __ret; 204 }) 205 206 #define __swait_event_interruptible(wq, condit 207 ___swait_event(wq, condition, TASK_INT 208 schedule()) 209 210 #define swait_event_interruptible_exclusive(wq 211 ({ 212 int __ret = 0; 213 if (!(condition)) 214 __ret = __swait_event_interrup 215 __ret; 216 }) 217 218 #define __swait_event_interruptible_timeout(wq 219 ___swait_event(wq, ___wait_cond_timeou 220 TASK_INTERRUPTIBLE, time 221 __ret = schedule_timeout 222 223 #define swait_event_interruptible_timeout_excl 224 ({ 225 long __ret = timeout; 226 if (!___wait_cond_timeout(condition)) 227 __ret = __swait_event_interrup 228 229 __ret; 230 }) 231 232 #define __swait_event_idle(wq, condition) 233 (void)___swait_event(wq, condition, TA 234 235 /** 236 * swait_event_idle_exclusive - wait without s 237 * @wq: the waitqueue to wait on 238 * @condition: a C expression for the event to 239 * 240 * The process is put to sleep (TASK_IDLE) unt 241 * true. The @condition is checked each time t 242 * 243 * This function is mostly used when a kthread 244 * condition and doesn't want to contribute to 245 * ignored. 246 */ 247 #define swait_event_idle_exclusive(wq, conditi 248 do { 249 if (condition) 250 break; 251 __swait_event_idle(wq, condition); 252 } while (0) 253 254 #define __swait_event_idle_timeout(wq, conditi 255 ___swait_event(wq, ___wait_cond_timeou 256 TASK_IDLE, timeout, 257 __ret = schedule_timeou 258 259 /** 260 * swait_event_idle_timeout_exclusive - wait u 261 * @wq: the waitqueue to wait on 262 * @condition: a C expression for the event to 263 * @timeout: timeout at which we'll give up in 264 * 265 * The process is put to sleep (TASK_IDLE) unt 266 * true. The @condition is checked each time t 267 * 268 * This function is mostly used when a kthread 269 * condition and doesn't want to contribute to 270 * ignored. 271 * 272 * Returns: 273 * 0 if the @condition evaluated to %false aft 274 * 1 if the @condition evaluated to %true afte 275 * or the remaining jiffies (at least 1) if th 276 * to %true before the @timeout elapsed. 277 */ 278 #define swait_event_idle_timeout_exclusive(wq, 279 ({ 280 long __ret = timeout; 281 if (!___wait_cond_timeout(condition)) 282 __ret = __swait_event_idle_tim 283 284 __ret; 285 }) 286 287 #endif /* _LINUX_SWAIT_H */ 288
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.