2 * libusb synchronization using POSIX Threads
4 * Copyright © 2010 Peter Stuge <peter@stuge.se>
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
21 #ifndef LIBUSB_THREADS_POSIX_H
22 #define LIBUSB_THREADS_POSIX_H
25 #ifdef HAVE_SYS_TIME_H
29 #define USBI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
30 typedef pthread_mutex_t usbi_mutex_static_t;
31 static inline void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
33 (void)pthread_mutex_lock(mutex);
35 static inline void usbi_mutex_static_unlock(usbi_mutex_static_t *mutex)
37 (void)pthread_mutex_unlock(mutex);
40 typedef pthread_mutex_t usbi_mutex_t;
41 static inline int usbi_mutex_init(usbi_mutex_t *mutex)
43 return pthread_mutex_init(mutex, NULL);
45 static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
47 (void)pthread_mutex_lock(mutex);
49 static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
51 (void)pthread_mutex_unlock(mutex);
53 static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
55 return pthread_mutex_trylock(mutex);
57 static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
59 (void)pthread_mutex_destroy(mutex);
62 typedef pthread_cond_t usbi_cond_t;
63 static inline void usbi_cond_init(pthread_cond_t *cond)
65 (void)pthread_cond_init(cond, NULL);
67 static inline int usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
69 return pthread_cond_wait(cond, mutex);
71 int usbi_cond_timedwait(usbi_cond_t *cond,
72 usbi_mutex_t *mutex, const struct timeval *tv);
73 static inline void usbi_cond_broadcast(usbi_cond_t *cond)
75 (void)pthread_cond_broadcast(cond);
77 static inline void usbi_cond_destroy(usbi_cond_t *cond)
79 (void)pthread_cond_destroy(cond);
82 typedef pthread_key_t usbi_tls_key_t;
83 static inline void usbi_tls_key_create(usbi_tls_key_t *key)
85 (void)pthread_key_create(key, NULL);
87 static inline void *usbi_tls_key_get(usbi_tls_key_t key)
89 return pthread_getspecific(key);
91 static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
93 (void)pthread_setspecific(key, ptr);
95 static inline void usbi_tls_key_delete(usbi_tls_key_t key)
97 (void)pthread_key_delete(key);
100 int usbi_get_tid(void);
102 #endif /* LIBUSB_THREADS_POSIX_H */