1 /**************************************************************************
2 *
3 * Copyright 2020 Lag Free Games, LLC
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 #include "cnd_monotonic.h"
29 #include "util/os_time.h"
30 #include "util/timespec.h"
31
32 #include <assert.h>
33
34 #ifdef _WIN32
35 #include <windows.h>
36
37 static_assert(sizeof(struct u_cnd_monotonic) == sizeof(CONDITION_VARIABLE),
38 "The size of u_cnd_monotonic must equal to CONDITION_VARIABLE");
39 static_assert(sizeof(mtx_t) == sizeof(CRITICAL_SECTION),
40 "The size of mtx_t must equal to CRITICAL_SECTION");
41 #endif
42
43 int
u_cnd_monotonic_init(struct u_cnd_monotonic * cond)44 u_cnd_monotonic_init(struct u_cnd_monotonic *cond)
45 {
46 assert(cond != NULL);
47
48 #ifdef _WIN32
49 InitializeConditionVariable((PCONDITION_VARIABLE)cond);
50 return thrd_success;
51 #else
52 int ret = thrd_error;
53 pthread_condattr_t condattr;
54 if (pthread_condattr_init(&condattr) == 0) {
55 if (
56 // pthread_condattr_setclock is not supported on Apple platforms.
57 // Instead, they use a relative deadline. See u_cnd_monotonic_timedwait.
58 #ifndef __APPLE__
59 (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC) == 0) &&
60 #endif
61 (pthread_cond_init(&cond->cond, &condattr) == 0)) {
62 ret = thrd_success;
63 }
64
65 pthread_condattr_destroy(&condattr);
66 }
67
68 return ret;
69 #endif
70 }
71
72 void
u_cnd_monotonic_destroy(struct u_cnd_monotonic * cond)73 u_cnd_monotonic_destroy(struct u_cnd_monotonic *cond)
74 {
75 assert(cond != NULL);
76
77 #ifdef _WIN32
78 // Do nothing
79 #else
80 pthread_cond_destroy(&cond->cond);
81 #endif
82 }
83
84 int
u_cnd_monotonic_broadcast(struct u_cnd_monotonic * cond)85 u_cnd_monotonic_broadcast(struct u_cnd_monotonic *cond)
86 {
87 assert(cond != NULL);
88
89 #ifdef _WIN32
90 WakeAllConditionVariable((PCONDITION_VARIABLE)cond);
91 return thrd_success;
92 #else
93 return (pthread_cond_broadcast(&cond->cond) == 0) ? thrd_success : thrd_error;
94 #endif
95 }
96
97 int
u_cnd_monotonic_signal(struct u_cnd_monotonic * cond)98 u_cnd_monotonic_signal(struct u_cnd_monotonic *cond)
99 {
100 assert(cond != NULL);
101
102 #ifdef _WIN32
103 WakeConditionVariable((PCONDITION_VARIABLE)cond);
104 return thrd_success;
105 #else
106 return (pthread_cond_signal(&cond->cond) == 0) ? thrd_success : thrd_error;
107 #endif
108 }
109
110 int
u_cnd_monotonic_timedwait(struct u_cnd_monotonic * cond,mtx_t * mtx,const struct timespec * abs_time)111 u_cnd_monotonic_timedwait(struct u_cnd_monotonic *cond, mtx_t *mtx,
112 const struct timespec *abs_time)
113 {
114 assert(cond != NULL);
115 assert(mtx != NULL);
116 assert(abs_time != NULL);
117
118 #ifdef _WIN32
119 const uint64_t future = (abs_time->tv_sec * 1000) + (abs_time->tv_nsec / 1000000);
120 const uint64_t now = os_time_get_nano() / 1000000;
121 const DWORD timeout = (future > now) ? (DWORD)(future - now) : 0;
122 if (SleepConditionVariableCS((PCONDITION_VARIABLE)cond,
123 (PCRITICAL_SECTION)mtx, timeout))
124 return thrd_success;
125 return (GetLastError() == ERROR_TIMEOUT) ? thrd_timedout : thrd_error;
126 #else
127 #ifdef __APPLE__
128 // Convert to a relative wait as we can't use CLOCK_MONOTONIC deadlines on macOS.
129 struct timespec now_time;
130 timespec_get(&now_time, TIME_MONOTONIC);
131 struct timespec rel_time;
132 timespec_sub_saturate(&rel_time, abs_time, &now_time);
133 int rt = pthread_cond_timedwait_relative_np(&cond->cond, mtx, &rel_time);
134 #else
135 int rt = pthread_cond_timedwait(&cond->cond, mtx, abs_time);
136 #endif
137 if (rt == ETIMEDOUT)
138 return thrd_timedout;
139 return (rt == 0) ? thrd_success : thrd_error;
140 #endif
141 }
142
143 int
u_cnd_monotonic_wait(struct u_cnd_monotonic * cond,mtx_t * mtx)144 u_cnd_monotonic_wait(struct u_cnd_monotonic *cond, mtx_t *mtx)
145 {
146 assert(cond != NULL);
147 assert(mtx != NULL);
148
149 #ifdef _WIN32
150 SleepConditionVariableCS((PCONDITION_VARIABLE)cond,
151 (PCRITICAL_SECTION)mtx, INFINITE);
152 return thrd_success;
153 #else
154 return (pthread_cond_wait(&cond->cond, mtx) == 0) ? thrd_success : thrd_error;
155 #endif
156 }
157