diff options
Diffstat (limited to 'include/nvgpu/linux/cond.h')
-rw-r--r-- | include/nvgpu/linux/cond.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/include/nvgpu/linux/cond.h b/include/nvgpu/linux/cond.h new file mode 100644 index 0000000..b53ada3 --- /dev/null +++ b/include/nvgpu/linux/cond.h | |||
@@ -0,0 +1,81 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017-2019, NVIDIA CORPORATION. All rights reserved. | ||
3 | * | ||
4 | * This program is free software; you can redistribute it and/or modify it | ||
5 | * under the terms and conditions of the GNU General Public License, | ||
6 | * version 2, as published by the Free Software Foundation. | ||
7 | * | ||
8 | * This program is distributed in the hope it will be useful, but WITHOUT | ||
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | ||
11 | * more details. | ||
12 | * | ||
13 | * You should have received a copy of the GNU General Public License | ||
14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
15 | */ | ||
16 | |||
17 | #ifndef __NVGPU_COND_LINUX_H__ | ||
18 | #define __NVGPU_COND_LINUX_H__ | ||
19 | |||
20 | #include <linux/wait.h> | ||
21 | #include <linux/sched.h> | ||
22 | |||
23 | struct nvgpu_cond { | ||
24 | bool initialized; | ||
25 | wait_queue_head_t wq; | ||
26 | }; | ||
27 | |||
28 | /** | ||
29 | * NVGPU_COND_WAIT - Wait for a condition to be true | ||
30 | * | ||
31 | * @c - The condition variable to sleep on | ||
32 | * @condition - The condition that needs to be true | ||
33 | * @timeout_ms - Timeout in milliseconds, or 0 for infinite wait | ||
34 | * | ||
35 | * Wait for a condition to become true. Returns -ETIMEOUT if | ||
36 | * the wait timed out with condition false. | ||
37 | */ | ||
38 | #define NVGPU_COND_WAIT(c, condition, timeout_ms) \ | ||
39 | ({\ | ||
40 | int ret = 0; \ | ||
41 | long _timeout_ms = timeout_ms;\ | ||
42 | if (_timeout_ms > 0) { \ | ||
43 | long _ret = wait_event_timeout((c)->wq, condition, \ | ||
44 | msecs_to_jiffies(_timeout_ms)); \ | ||
45 | if (_ret == 0) \ | ||
46 | ret = -ETIMEDOUT; \ | ||
47 | } else { \ | ||
48 | wait_event((c)->wq, condition); \ | ||
49 | } \ | ||
50 | ret;\ | ||
51 | }) | ||
52 | |||
53 | /** | ||
54 | * NVGPU_COND_WAIT_INTERRUPTIBLE - Wait for a condition to be true | ||
55 | * | ||
56 | * @c - The condition variable to sleep on | ||
57 | * @condition - The condition that needs to be true | ||
58 | * @timeout_ms - Timeout in milliseconds, or 0 for infinite wait | ||
59 | * | ||
60 | * Wait for a condition to become true. Returns -ETIMEOUT if | ||
61 | * the wait timed out with condition false or -ERESTARTSYS on | ||
62 | * signal. | ||
63 | */ | ||
64 | #define NVGPU_COND_WAIT_INTERRUPTIBLE(c, condition, timeout_ms) \ | ||
65 | ({ \ | ||
66 | int ret = 0; \ | ||
67 | long _timeout_ms = timeout_ms;\ | ||
68 | if (_timeout_ms > 0) { \ | ||
69 | long _ret = wait_event_interruptible_timeout((c)->wq, condition, \ | ||
70 | msecs_to_jiffies(_timeout_ms)); \ | ||
71 | if (_ret == 0) \ | ||
72 | ret = -ETIMEDOUT; \ | ||
73 | else if (_ret == -ERESTARTSYS) \ | ||
74 | ret = -ERESTARTSYS; \ | ||
75 | } else { \ | ||
76 | ret = wait_event_interruptible((c)->wq, condition); \ | ||
77 | } \ | ||
78 | ret; \ | ||
79 | }) | ||
80 | |||
81 | #endif /* __NVGPU_LOCK_LINUX_H__ */ | ||