libusb-1.0
threads_windows.h
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 #include <errno.h>
25 
26 #define USBI_MUTEX_INITIALIZER 0L
27 typedef LONG usbi_mutex_static_t;
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 }
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;
39 static inline int usbi_mutex_init(usbi_mutex_t *mutex)
40 {
41  InitializeCriticalSection(mutex);
42  return 0;
43 }
44 static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
45 {
46  EnterCriticalSection(mutex);
47 }
48 static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
49 {
50  LeaveCriticalSection(mutex);
51 }
52 static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
53 {
54  return !TryEnterCriticalSection(mutex);
55 }
56 static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
57 {
58  DeleteCriticalSection(mutex);
59 }
60 
61 // We *were* getting timespec from pthread.h:
62 #if !defined(HAVE_STRUCT_TIMESPEC) && !defined(_TIMESPEC_DEFINED)
63 #define HAVE_STRUCT_TIMESPEC 1
64 #define _TIMESPEC_DEFINED 1
65 struct timespec {
66  long tv_sec;
67  long tv_nsec;
68 };
69 #endif /* HAVE_STRUCT_TIMESPEC || _TIMESPEC_DEFINED */
70 
71 // We *were* getting ETIMEDOUT from pthread.h:
72 #ifndef ETIMEDOUT
73 #define ETIMEDOUT 10060 /* This is the value in winsock.h. */
74 #endif
75 
76 typedef CONDITION_VARIABLE usbi_cond_t;
77 static inline void usbi_cond_init(usbi_cond_t *cond)
78 {
79  InitializeConditionVariable(cond);
80 }
81 static inline void usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
82 {
83  (void)SleepConditionVariableCS(cond, mutex, INFINITE);
84 }
85 int usbi_cond_timedwait(usbi_cond_t *cond,
86  usbi_mutex_t *mutex, const struct timeval *tv);
87 static inline void usbi_cond_broadcast(usbi_cond_t *cond)
88 {
89  WakeAllConditionVariable(cond);
90 }
91 static inline void usbi_cond_destroy(usbi_cond_t *cond)
92 {
93  UNUSED(cond);
94 }
95 
96 typedef DWORD usbi_tls_key_t;
97 static inline void usbi_tls_key_create(usbi_tls_key_t *key)
98 {
99  *key = TlsAlloc();
100 }
101 static inline void *usbi_tls_key_get(usbi_tls_key_t key)
102 {
103  return TlsGetValue(key);
104 }
105 static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
106 {
107  (void)TlsSetValue(key, ptr);
108 }
109 static inline void usbi_tls_key_delete(usbi_tls_key_t key)
110 {
111  (void)TlsFree(key);
112 }
113 
114 static inline int usbi_get_tid(void)
115 {
116  return (int)GetCurrentThreadId();
117 }
118 
119 #endif /* LIBUSB_THREADS_WINDOWS_H */
typedef void(LIBUSB_CALL *libusb_transfer_cb_fn)(struct libusb_transfer *transfer)
Definition: threads_windows.h:65