2 * libusbx synchronization on Microsoft Windows
4 * Copyright © 2010 Michael Plante <michael.plante@gmail.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 int usbi_mutex_init(usbi_mutex_t *mutex,
30 const usbi_mutexattr_t *attr) {
32 if(! mutex) return ((errno=EINVAL));
33 *mutex = CreateMutex(NULL, FALSE, NULL);
34 if(!*mutex) return ((errno=ENOMEM));
37 int usbi_mutex_destroy(usbi_mutex_t *mutex) {
38 // It is not clear if CloseHandle failure is due to failure to unlock.
39 // If so, this should be errno=EBUSY.
40 if(!mutex || !CloseHandle(*mutex)) return ((errno=EINVAL));
44 int usbi_mutex_trylock(usbi_mutex_t *mutex) {
46 if(!mutex) return ((errno=EINVAL));
47 result = WaitForSingleObject(*mutex, 0);
48 if(result == WAIT_OBJECT_0 || result == WAIT_ABANDONED)
49 return 0; // acquired (ToDo: check that abandoned is ok)
50 if(result == WAIT_TIMEOUT)
51 return ((errno=EBUSY));
52 return ((errno=EINVAL)); // don't know how this would happen
53 // so don't know proper errno
55 int usbi_mutex_lock(usbi_mutex_t *mutex) {
57 if(!mutex) return ((errno=EINVAL));
58 result = WaitForSingleObject(*mutex, INFINITE);
59 if(result == WAIT_OBJECT_0 || result == WAIT_ABANDONED)
60 return 0; // acquired (ToDo: check that abandoned is ok)
61 return ((errno=EINVAL)); // don't know how this would happen
62 // so don't know proper errno
64 int usbi_mutex_unlock(usbi_mutex_t *mutex) {
65 if(!mutex) return ((errno=EINVAL));
66 if(!ReleaseMutex(*mutex)) return ((errno=EPERM ));
70 int usbi_mutex_static_lock(usbi_mutex_static_t *mutex) {
71 if(!mutex) return ((errno=EINVAL));
72 while (InterlockedExchange((LONG *)mutex, 1) == 1) {
77 int usbi_mutex_static_unlock(usbi_mutex_static_t *mutex) {
78 if(!mutex) return ((errno=EINVAL));
85 int usbi_cond_init(usbi_cond_t *cond,
86 const usbi_condattr_t *attr) {
88 if(!cond) return ((errno=EINVAL));
89 list_init(&cond->waiters );
90 list_init(&cond->not_waiting);
93 int usbi_cond_destroy(usbi_cond_t *cond) {
94 // This assumes no one is using this anymore. The check MAY NOT BE safe.
95 struct usbi_cond_perthread *pos, *next_pos = NULL;
96 if(!cond) return ((errno=EINVAL));
97 if(!list_empty(&cond->waiters)) return ((errno=EBUSY )); // (!see above!)
98 list_for_each_entry_safe(pos, next_pos, &cond->not_waiting, list, struct usbi_cond_perthread) {
99 CloseHandle(pos->event);
100 list_del(&pos->list);
107 int usbi_cond_broadcast(usbi_cond_t *cond) {
108 // Assumes mutex is locked; this is not in keeping with POSIX spec, but
109 // libusb does this anyway, so we simplify by not adding more sync
110 // primitives to the CV definition!
112 struct usbi_cond_perthread *pos;
113 if(!cond) return ((errno=EINVAL));
114 list_for_each_entry(pos, &cond->waiters, list, struct usbi_cond_perthread) {
115 if(!SetEvent(pos->event))
118 // The wait function will remove its respective item from the list.
119 return fail ? ((errno=EINVAL)) : 0;
121 int usbi_cond_signal(usbi_cond_t *cond) {
122 // Assumes mutex is locked; this is not in keeping with POSIX spec, but
123 // libusb does this anyway, so we simplify by not adding more sync
124 // primitives to the CV definition!
125 struct usbi_cond_perthread *pos;
126 if(!cond) return ((errno=EINVAL));
127 if(list_empty(&cond->waiters)) return 0; // no one to wakeup.
128 pos = list_entry(&cond->waiters.next, struct usbi_cond_perthread, list);
129 // The wait function will remove its respective item from the list.
130 return SetEvent(pos->event) ? 0 : ((errno=EINVAL));
132 static int __inline usbi_cond_intwait(usbi_cond_t *cond,
135 struct usbi_cond_perthread *pos;
137 DWORD r2,tid = GetCurrentThreadId();
138 if(!cond || !mutex) return ((errno=EINVAL));
139 list_for_each_entry(pos, &cond->not_waiting, list, struct usbi_cond_perthread) {
140 if(tid == pos->tid) {
146 pos = (struct usbi_cond_perthread*) calloc(1, sizeof(struct usbi_cond_perthread));
147 if(!pos) return ((errno=ENOMEM)); // This errno is not POSIX-allowed.
149 pos->event = CreateEvent(NULL, FALSE, FALSE, NULL); // auto-reset.
152 return ((errno=ENOMEM));
154 list_add(&pos->list, &cond->not_waiting);
157 list_del(&pos->list); // remove from not_waiting list.
158 list_add(&pos->list, &cond->waiters);
160 r = usbi_mutex_unlock(mutex);
162 r2 = WaitForSingleObject(pos->event, timeout_ms);
163 r = usbi_mutex_lock(mutex);
166 list_del(&pos->list);
167 list_add(&pos->list, &cond->not_waiting);
169 if(r2 == WAIT_TIMEOUT) return ((errno=ETIMEDOUT));
173 // N.B.: usbi_cond_*wait() can also return ENOMEM, even though pthread_cond_*wait cannot!
174 int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex) {
175 return usbi_cond_intwait(cond, mutex, INFINITE);
177 int usbi_cond_timedwait(usbi_cond_t *cond,
179 const struct timespec *abstime) {
181 ULARGE_INTEGER rtime;
182 struct timeval targ_time, cur_time, delta_time;
183 struct timespec cur_time_ns;
185 extern const uint64_t epoch_time;
187 GetSystemTimeAsFileTime(&filetime);
188 rtime.LowPart = filetime.dwLowDateTime;
189 rtime.HighPart = filetime.dwHighDateTime;
190 rtime.QuadPart -= epoch_time;
191 cur_time_ns.tv_sec = (long)(rtime.QuadPart / 10000000);
192 cur_time_ns.tv_nsec = (long)((rtime.QuadPart % 10000000)*100);
193 TIMESPEC_TO_TIMEVAL(&cur_time, &cur_time_ns);
195 TIMESPEC_TO_TIMEVAL(&targ_time, abstime);
196 timersub(&targ_time, &cur_time, &delta_time);
197 if(delta_time.tv_sec < 0) // abstime already passed?
200 millis = delta_time.tv_usec/1000;
201 millis += delta_time.tv_sec *1000;
202 if (delta_time.tv_usec % 1000) // round up to next millisecond
206 return usbi_cond_intwait(cond, mutex, millis);
209 int usbi_get_tid(void) {
210 return GetCurrentThreadId();