• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 WINAPI_CHECK(expression)	ASSERT_NE(expression, 0)
25 
26 #define USBI_MUTEX_INITIALIZER	0L
27 typedef LONG usbi_mutex_static_t;
usbi_mutex_static_lock(usbi_mutex_static_t * mutex)28 static inline void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
29 {
30 	while (InterlockedExchange(mutex, 1L) == 1L)
31 		SleepEx(0, TRUE);
32 }
usbi_mutex_static_unlock(usbi_mutex_static_t * mutex)33 static inline void usbi_mutex_static_unlock(usbi_mutex_static_t *mutex)
34 {
35 	InterlockedExchange(mutex, 0L);
36 }
37 
38 typedef CRITICAL_SECTION usbi_mutex_t;
usbi_mutex_init(usbi_mutex_t * mutex)39 static inline void usbi_mutex_init(usbi_mutex_t *mutex)
40 {
41 	InitializeCriticalSection(mutex);
42 }
usbi_mutex_lock(usbi_mutex_t * mutex)43 static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
44 {
45 	EnterCriticalSection(mutex);
46 }
usbi_mutex_unlock(usbi_mutex_t * mutex)47 static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
48 {
49 	LeaveCriticalSection(mutex);
50 }
usbi_mutex_trylock(usbi_mutex_t * mutex)51 static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
52 {
53 	return TryEnterCriticalSection(mutex) != 0;
54 }
usbi_mutex_destroy(usbi_mutex_t * mutex)55 static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
56 {
57 	DeleteCriticalSection(mutex);
58 }
59 
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 typedef CONDITION_VARIABLE usbi_cond_t;
usbi_cond_init(usbi_cond_t * cond)70 static inline void usbi_cond_init(usbi_cond_t *cond)
71 {
72 	InitializeConditionVariable(cond);
73 }
usbi_cond_wait(usbi_cond_t * cond,usbi_mutex_t * mutex)74 static inline void usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
75 {
76 	WINAPI_CHECK(SleepConditionVariableCS(cond, mutex, INFINITE));
77 }
78 int usbi_cond_timedwait(usbi_cond_t *cond,
79 	usbi_mutex_t *mutex, const struct timeval *tv);
usbi_cond_broadcast(usbi_cond_t * cond)80 static inline void usbi_cond_broadcast(usbi_cond_t *cond)
81 {
82 	WakeAllConditionVariable(cond);
83 }
usbi_cond_destroy(usbi_cond_t * cond)84 static inline void usbi_cond_destroy(usbi_cond_t *cond)
85 {
86 	UNUSED(cond);
87 }
88 
89 typedef DWORD usbi_tls_key_t;
usbi_tls_key_create(usbi_tls_key_t * key)90 static inline void usbi_tls_key_create(usbi_tls_key_t *key)
91 {
92 	*key = TlsAlloc();
93 	assert(*key != TLS_OUT_OF_INDEXES);
94 }
usbi_tls_key_get(usbi_tls_key_t key)95 static inline void *usbi_tls_key_get(usbi_tls_key_t key)
96 {
97 	return TlsGetValue(key);
98 }
usbi_tls_key_set(usbi_tls_key_t key,void * ptr)99 static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
100 {
101 	WINAPI_CHECK(TlsSetValue(key, ptr));
102 }
usbi_tls_key_delete(usbi_tls_key_t key)103 static inline void usbi_tls_key_delete(usbi_tls_key_t key)
104 {
105 	WINAPI_CHECK(TlsFree(key));
106 }
107 
usbi_get_tid(void)108 static inline unsigned int usbi_get_tid(void)
109 {
110 	return (unsigned int)GetCurrentThreadId();
111 }
112 
113 #endif /* LIBUSB_THREADS_WINDOWS_H */
114