2 * libusb 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
27 struct usbi_cond_perthread {
28 struct list_head list;
32 void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
34 while (InterlockedExchange(mutex, 1L) == 1L)
38 void usbi_cond_init(usbi_cond_t *cond)
40 list_init(&cond->waiters);
41 list_init(&cond->not_waiting);
44 static int usbi_cond_intwait(usbi_cond_t *cond,
45 usbi_mutex_t *mutex, DWORD timeout_ms)
47 struct usbi_cond_perthread *pos;
50 // Same assumption as usbi_cond_broadcast() holds
51 if (list_empty(&cond->not_waiting)) {
52 pos = malloc(sizeof(*pos));
54 return ENOMEM; // This errno is not POSIX-allowed.
55 pos->event = CreateEvent(NULL, FALSE, FALSE, NULL); // auto-reset.
56 if (pos->event == NULL) {
61 pos = list_first_entry(&cond->not_waiting, struct usbi_cond_perthread, list);
62 list_del(&pos->list); // remove from not_waiting list.
63 // Ensure the event is clear before waiting
64 WaitForSingleObject(pos->event, 0);
67 list_add(&pos->list, &cond->waiters);
69 LeaveCriticalSection(mutex);
70 r = WaitForSingleObject(pos->event, timeout_ms);
71 EnterCriticalSection(mutex);
74 list_add(&pos->list, &cond->not_waiting);
76 if (r == WAIT_OBJECT_0)
78 else if (r == WAIT_TIMEOUT)
84 // N.B.: usbi_cond_*wait() can also return ENOMEM, even though pthread_cond_*wait cannot!
85 int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
87 return usbi_cond_intwait(cond, mutex, INFINITE);
90 int usbi_cond_timedwait(usbi_cond_t *cond,
91 usbi_mutex_t *mutex, const struct timeval *tv)
95 millis = (DWORD)(tv->tv_sec * 1000) + (tv->tv_usec / 1000);
96 /* round up to next millisecond */
97 if (tv->tv_usec % 1000)
99 return usbi_cond_intwait(cond, mutex, millis);
102 void usbi_cond_broadcast(usbi_cond_t *cond)
104 // Assumes mutex is locked; this is not in keeping with POSIX spec, but
105 // libusb does this anyway, so we simplify by not adding more sync
106 // primitives to the CV definition!
107 struct usbi_cond_perthread *pos;
109 list_for_each_entry(pos, &cond->waiters, list, struct usbi_cond_perthread)
110 SetEvent(pos->event);
111 // The wait function will remove its respective item from the list.
114 void usbi_cond_destroy(usbi_cond_t *cond)
116 // This assumes no one is using this anymore. The check MAY NOT BE safe.
117 struct usbi_cond_perthread *pos, *next;
119 if (!list_empty(&cond->waiters))
120 return; // (!see above!)
121 list_for_each_entry_safe(pos, next, &cond->not_waiting, list, struct usbi_cond_perthread) {
122 CloseHandle(pos->event);
123 list_del(&pos->list);