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 #ifndef LIBUSB_THREADS_WINDOWS_H
22 #define LIBUSB_THREADS_WINDOWS_H
23 
24 #define USBI_MUTEX_INITIALIZER	0L
25 #ifdef _WIN32_WCE
26 typedef LONG usbi_mutex_static_t;
27 #else
28 typedef volatile LONG usbi_mutex_static_t;
29 #endif
30 void usbi_mutex_static_lock(usbi_mutex_static_t *mutex);
usbi_mutex_static_unlock(usbi_mutex_static_t * mutex)31 static inline void usbi_mutex_static_unlock(usbi_mutex_static_t *mutex)
32 {
33 	InterlockedExchange(mutex, 0L);
34 }
35 
36 typedef CRITICAL_SECTION usbi_mutex_t;
usbi_mutex_init(usbi_mutex_t * mutex)37 static inline int usbi_mutex_init(usbi_mutex_t *mutex)
38 {
39 	InitializeCriticalSection(mutex);
40 	return 0;
41 }
usbi_mutex_lock(usbi_mutex_t * mutex)42 static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
43 {
44 	EnterCriticalSection(mutex);
45 }
usbi_mutex_unlock(usbi_mutex_t * mutex)46 static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
47 {
48 	LeaveCriticalSection(mutex);
49 }
usbi_mutex_trylock(usbi_mutex_t * mutex)50 static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
51 {
52 	return !TryEnterCriticalSection(mutex);
53 }
usbi_mutex_destroy(usbi_mutex_t * mutex)54 static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
55 {
56 	DeleteCriticalSection(mutex);
57 }
58 
59 // We *were* getting timespec from pthread.h:
60 #if (!defined(HAVE_STRUCT_TIMESPEC) && !defined(_TIMESPEC_DEFINED))
61 #define HAVE_STRUCT_TIMESPEC 1
62 #define _TIMESPEC_DEFINED 1
63 struct timespec {
64 	long tv_sec;
65 	long tv_nsec;
66 };
67 #endif /* HAVE_STRUCT_TIMESPEC | _TIMESPEC_DEFINED */
68 
69 // We *were* getting ETIMEDOUT from pthread.h:
70 #ifndef ETIMEDOUT
71 #define ETIMEDOUT	10060	/* This is the value in winsock.h. */
72 #endif
73 
74 typedef struct usbi_cond {
75 	// Every time a thread touches the CV, it winds up in one of these lists.
76 	//   It stays there until the CV is destroyed, even if the thread terminates.
77 	struct list_head waiters;
78 	struct list_head not_waiting;
79 } usbi_cond_t;
80 
81 void usbi_cond_init(usbi_cond_t *cond);
82 int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex);
83 int usbi_cond_timedwait(usbi_cond_t *cond,
84 	usbi_mutex_t *mutex, const struct timeval *tv);
85 void usbi_cond_broadcast(usbi_cond_t *cond);
86 void usbi_cond_destroy(usbi_cond_t *cond);
87 
88 typedef DWORD usbi_tls_key_t;
usbi_tls_key_create(usbi_tls_key_t * key)89 static inline void usbi_tls_key_create(usbi_tls_key_t *key)
90 {
91 	*key = TlsAlloc();
92 }
usbi_tls_key_get(usbi_tls_key_t key)93 static inline void *usbi_tls_key_get(usbi_tls_key_t key)
94 {
95 	return TlsGetValue(key);
96 }
usbi_tls_key_set(usbi_tls_key_t key,void * ptr)97 static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
98 {
99 	(void)TlsSetValue(key, ptr);
100 }
usbi_tls_key_delete(usbi_tls_key_t key)101 static inline void usbi_tls_key_delete(usbi_tls_key_t key)
102 {
103 	(void)TlsFree(key);
104 }
105 
usbi_get_tid(void)106 static inline int usbi_get_tid(void)
107 {
108 	return (int)GetCurrentThreadId();
109 }
110 
111 #endif /* LIBUSB_THREADS_WINDOWS_H */
112