Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | #ifndef _LINUX_RCUWAIT_H_ |
| 3 | #define _LINUX_RCUWAIT_H_ |
| 4 | |
| 5 | #include <linux/rcupdate.h> |
| 6 | |
| 7 | /* |
| 8 | * rcuwait provides a way of blocking and waking up a single |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 9 | * task in an rcu-safe manner. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 10 | * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 11 | * The only time @task is non-nil is when a user is blocked (or |
| 12 | * checking if it needs to) on a condition, and reset as soon as we |
| 13 | * know that the condition has succeeded and are awoken. |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 14 | */ |
| 15 | struct rcuwait { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 16 | struct task_struct __rcu *task; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 17 | }; |
| 18 | |
| 19 | #define __RCUWAIT_INITIALIZER(name) \ |
| 20 | { .task = NULL, } |
| 21 | |
| 22 | static inline void rcuwait_init(struct rcuwait *w) |
| 23 | { |
| 24 | w->task = NULL; |
| 25 | } |
| 26 | |
| 27 | extern void rcuwait_wake_up(struct rcuwait *w); |
| 28 | |
| 29 | /* |
| 30 | * The caller is responsible for locking around rcuwait_wait_event(), |
| 31 | * such that writes to @task are properly serialized. |
| 32 | */ |
| 33 | #define rcuwait_wait_event(w, condition) \ |
| 34 | ({ \ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | rcu_assign_pointer((w)->task, current); \ |
| 36 | for (;;) { \ |
| 37 | /* \ |
| 38 | * Implicit barrier (A) pairs with (B) in \ |
| 39 | * rcuwait_wake_up(). \ |
| 40 | */ \ |
| 41 | set_current_state(TASK_UNINTERRUPTIBLE); \ |
| 42 | if (condition) \ |
| 43 | break; \ |
| 44 | \ |
| 45 | schedule(); \ |
| 46 | } \ |
| 47 | \ |
| 48 | WRITE_ONCE((w)->task, NULL); \ |
| 49 | __set_current_state(TASK_RUNNING); \ |
| 50 | }) |
| 51 | |
| 52 | #endif /* _LINUX_RCUWAIT_H_ */ |