eadb9783389be783d36aa114ad58f4a9d867c393
[platform/upstream/libusb.git] / libusb / os / threads_posix.h
1 /*
2  * libusb synchronization using POSIX Threads
3  *
4  * Copyright © 2010 Peter Stuge <peter@stuge.se>
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_POSIX_H
22 #define LIBUSB_THREADS_POSIX_H
23
24 #include <pthread.h>
25
26 #define USBI_MUTEX_INITIALIZER  PTHREAD_MUTEX_INITIALIZER
27 typedef pthread_mutex_t usbi_mutex_static_t;
28 static inline void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
29 {
30         (void)pthread_mutex_lock(mutex);
31 }
32 static inline void usbi_mutex_static_unlock(usbi_mutex_static_t *mutex)
33 {
34         (void)pthread_mutex_unlock(mutex);
35 }
36
37 typedef pthread_mutex_t usbi_mutex_t;
38 static inline int usbi_mutex_init(usbi_mutex_t *mutex)
39 {
40         return pthread_mutex_init(mutex, NULL);
41 }
42 static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
43 {
44         (void)pthread_mutex_lock(mutex);
45 }
46 static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
47 {
48         (void)pthread_mutex_unlock(mutex);
49 }
50 static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
51 {
52         return pthread_mutex_trylock(mutex);
53 }
54 static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
55 {
56         (void)pthread_mutex_destroy(mutex);
57 }
58
59 typedef pthread_cond_t usbi_cond_t;
60 static inline void usbi_cond_init(pthread_cond_t *cond)
61 {
62         (void)pthread_cond_init(cond, NULL);
63 }
64 static inline void usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
65 {
66         (void)pthread_cond_wait(cond, mutex);
67 }
68 int usbi_cond_timedwait(usbi_cond_t *cond,
69         usbi_mutex_t *mutex, const struct timeval *tv);
70 static inline void usbi_cond_broadcast(usbi_cond_t *cond)
71 {
72         (void)pthread_cond_broadcast(cond);
73 }
74 static inline void usbi_cond_destroy(usbi_cond_t *cond)
75 {
76         (void)pthread_cond_destroy(cond);
77 }
78
79 typedef pthread_key_t usbi_tls_key_t;
80 static inline void usbi_tls_key_create(usbi_tls_key_t *key)
81 {
82         (void)pthread_key_create(key, NULL);
83 }
84 static inline void *usbi_tls_key_get(usbi_tls_key_t key)
85 {
86         return pthread_getspecific(key);
87 }
88 static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
89 {
90         (void)pthread_setspecific(key, ptr);
91 }
92 static inline void usbi_tls_key_delete(usbi_tls_key_t key)
93 {
94         (void)pthread_key_delete(key);
95 }
96
97 int usbi_get_tid(void);
98
99 #endif /* LIBUSB_THREADS_POSIX_H */