1 /*
2  * libusb synchronization on Microsoft Windows
3  *
4  * Copyright © 2010 Michael Plante <michael.plante@gmail.com>
5  *
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.
10  *
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.
15  *
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
19  */
20 
21 #include <config.h>
22 
23 #include <errno.h>
24 
25 #include "libusbi.h"
26 
27 struct usbi_cond_perthread {
28 	struct list_head list;
29 	HANDLE event;
30 };
31 
usbi_mutex_static_lock(usbi_mutex_static_t * mutex)32 void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
33 {
34 	while (InterlockedExchange(mutex, 1L) == 1L)
35 		SleepEx(0, TRUE);
36 }
37 
usbi_cond_init(usbi_cond_t * cond)38 void usbi_cond_init(usbi_cond_t *cond)
39 {
40 	list_init(&cond->waiters);
41 	list_init(&cond->not_waiting);
42 }
43 
usbi_cond_intwait(usbi_cond_t * cond,usbi_mutex_t * mutex,DWORD timeout_ms)44 static int usbi_cond_intwait(usbi_cond_t *cond,
45 	usbi_mutex_t *mutex, DWORD timeout_ms)
46 {
47 	struct usbi_cond_perthread *pos;
48 	DWORD r;
49 
50 	// Same assumption as usbi_cond_broadcast() holds
51 	if (list_empty(&cond->not_waiting)) {
52 		pos = malloc(sizeof(*pos));
53 		if (pos == NULL)
54 			return ENOMEM; // This errno is not POSIX-allowed.
55 		pos->event = CreateEvent(NULL, FALSE, FALSE, NULL); // auto-reset.
56 		if (pos->event == NULL) {
57 			free(pos);
58 			return ENOMEM;
59 		}
60 	} else {
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);
65 	}
66 
67 	list_add(&pos->list, &cond->waiters);
68 
69 	LeaveCriticalSection(mutex);
70 	r = WaitForSingleObject(pos->event, timeout_ms);
71 	EnterCriticalSection(mutex);
72 
73 	list_del(&pos->list);
74 	list_add(&pos->list, &cond->not_waiting);
75 
76 	if (r == WAIT_OBJECT_0)
77 		return 0;
78 	else if (r == WAIT_TIMEOUT)
79 		return ETIMEDOUT;
80 	else
81 		return EINVAL;
82 }
83 
84 // N.B.: usbi_cond_*wait() can also return ENOMEM, even though pthread_cond_*wait cannot!
usbi_cond_wait(usbi_cond_t * cond,usbi_mutex_t * mutex)85 int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
86 {
87 	return usbi_cond_intwait(cond, mutex, INFINITE);
88 }
89 
usbi_cond_timedwait(usbi_cond_t * cond,usbi_mutex_t * mutex,const struct timeval * tv)90 int usbi_cond_timedwait(usbi_cond_t *cond,
91 	usbi_mutex_t *mutex, const struct timeval *tv)
92 {
93 	DWORD millis;
94 
95 	millis = (DWORD)(tv->tv_sec * 1000) + (tv->tv_usec / 1000);
96 	/* round up to next millisecond */
97 	if (tv->tv_usec % 1000)
98 		millis++;
99 	return usbi_cond_intwait(cond, mutex, millis);
100 }
101 
usbi_cond_broadcast(usbi_cond_t * cond)102 void usbi_cond_broadcast(usbi_cond_t *cond)
103 {
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;
108 
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.
112 }
113 
usbi_cond_destroy(usbi_cond_t * cond)114 void usbi_cond_destroy(usbi_cond_t *cond)
115 {
116 	// This assumes no one is using this anymore.  The check MAY NOT BE safe.
117 	struct usbi_cond_perthread *pos, *next;
118 
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);
124 		free(pos);
125 	}
126 }
127