diff options
Diffstat (limited to 'drivers/gpu/nvgpu/include/nvgpu/linux/cond.h')
-rw-r--r-- | drivers/gpu/nvgpu/include/nvgpu/linux/cond.h | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/drivers/gpu/nvgpu/include/nvgpu/linux/cond.h b/drivers/gpu/nvgpu/include/nvgpu/linux/cond.h new file mode 100644 index 00000000..01ca5291 --- /dev/null +++ b/drivers/gpu/nvgpu/include/nvgpu/linux/cond.h | |||
@@ -0,0 +1,80 @@ | |||
1 | /* | ||
2 | * Copyright (c) 2017, 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 | |||
22 | struct nvgpu_cond { | ||
23 | bool initialized; | ||
24 | wait_queue_head_t wq; | ||
25 | }; | ||
26 | |||
27 | /** | ||
28 | * NVGPU_COND_WAIT - Wait for a condition to be true | ||
29 | * | ||
30 | * @c - The condition variable to sleep on | ||
31 | * @condition - The condition that needs to be true | ||
32 | * @timeout_ms - Timeout in milliseconds, or 0 for infinite wait | ||
33 | * | ||
34 | * Wait for a condition to become true. Returns -ETIMEOUT if | ||
35 | * the wait timed out with condition false. | ||
36 | */ | ||
37 | #define NVGPU_COND_WAIT(c, condition, timeout_ms) \ | ||
38 | ({\ | ||
39 | int ret = 0; \ | ||
40 | long _timeout_ms = timeout_ms;\ | ||
41 | if (_timeout_ms > 0) { \ | ||
42 | long _ret = wait_event_timeout((c)->wq, condition, \ | ||
43 | msecs_to_jiffies(_timeout_ms)); \ | ||
44 | if (_ret == 0) \ | ||
45 | ret = -ETIMEDOUT; \ | ||
46 | } else { \ | ||
47 | wait_event((c)->wq, condition); \ | ||
48 | } \ | ||
49 | ret;\ | ||
50 | }) | ||
51 | |||
52 | /** | ||
53 | * NVGPU_COND_WAIT_INTERRUPTIBLE - Wait for a condition to be true | ||
54 | * | ||
55 | * @c - The condition variable to sleep on | ||
56 | * @condition - The condition that needs to be true | ||
57 | * @timeout_ms - Timeout in milliseconds, or 0 for infinite wait | ||
58 | * | ||
59 | * Wait for a condition to become true. Returns -ETIMEOUT if | ||
60 | * the wait timed out with condition false or -ERESTARTSYS on | ||
61 | * signal. | ||
62 | */ | ||
63 | #define NVGPU_COND_WAIT_INTERRUPTIBLE(c, condition, timeout_ms) \ | ||
64 | ({ \ | ||
65 | int ret = 0; \ | ||
66 | long _timeout_ms = timeout_ms;\ | ||
67 | if (_timeout_ms > 0) { \ | ||
68 | long _ret = wait_event_interruptible_timeout((c)->wq, condition, \ | ||
69 | msecs_to_jiffies(_timeout_ms)); \ | ||
70 | if (_ret == 0) \ | ||
71 | ret = -ETIMEDOUT; \ | ||
72 | else if (_ret == -ERESTARTSYS) \ | ||
73 | ret = -ERESTARTSYS; \ | ||
74 | } else { \ | ||
75 | wait_event_interruptible((c)->wq, condition); \ | ||
76 | } \ | ||
77 | ret; \ | ||
78 | }) | ||
79 | |||
80 | #endif /* __NVGPU_LOCK_LINUX_H__ */ | ||