Debian Codegraph @

libusb/os/threads_posix.h

raw
1 /*
2 * libusb synchronization using POSIX Threads
3 *
4 * Copyright © 2010 Peter Stuge <[email protected]>
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 PTHREAD_CHECK(expression) ASSERT_EQ(expression, 0)
27
28 #define USBI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
29 typedef pthread_mutex_t usbi_mutex_static_t;
30 static inline void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
31 {
32 PTHREAD_CHECK(pthread_mutex_lock(mutex));
33 }
34 static inline void usbi_mutex_static_unlock(usbi_mutex_static_t *mutex)
35 {
36 PTHREAD_CHECK(pthread_mutex_unlock(mutex));
37 }
38
39 typedef pthread_mutex_t usbi_mutex_t;
40 static inline void usbi_mutex_init(usbi_mutex_t *mutex)
41 {
42 PTHREAD_CHECK(pthread_mutex_init(mutex, NULL));
43 }
44 static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
45 {
46 PTHREAD_CHECK(pthread_mutex_lock(mutex));
47 }
48 static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
49 {
50 PTHREAD_CHECK(pthread_mutex_unlock(mutex));
51 }
52 static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
53 {
54 int mutexIsLocked = pthread_mutex_trylock(mutex) == 0;
55 return mutexIsLocked;
56 }
57 static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
58 {
59 PTHREAD_CHECK(pthread_mutex_destroy(mutex));
60 }
61
62 #define USBI_COND_INITIALIZER PTHREAD_COND_INITIALIZER
63 typedef pthread_cond_t usbi_cond_t;
64 void usbi_cond_init(usbi_cond_t *cond);
65 static inline void usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
66 {
67 PTHREAD_CHECK(pthread_cond_wait(cond, mutex));
68 }
69 int usbi_cond_timedwait(usbi_cond_t *cond,
70 usbi_mutex_t *mutex, const struct timeval *tv);
71 static inline void usbi_cond_signal(usbi_cond_t *cond)
72 {
73 PTHREAD_CHECK(pthread_cond_signal(cond));
74 }
75 static inline void usbi_cond_broadcast(usbi_cond_t *cond)
76 {
77 PTHREAD_CHECK(pthread_cond_broadcast(cond));
78 }
79 static inline void usbi_cond_destroy(usbi_cond_t *cond)
80 {
81 PTHREAD_CHECK(pthread_cond_destroy(cond));
82 }
83
84 typedef pthread_key_t usbi_tls_key_t;
85 static inline void usbi_tls_key_create(usbi_tls_key_t *key)
86 {
87 PTHREAD_CHECK(pthread_key_create(key, NULL));
88 }
89 static inline void *usbi_tls_key_get(usbi_tls_key_t key)
90 {
91 return pthread_getspecific(key);
92 }
93 static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
94 {
95 PTHREAD_CHECK(pthread_setspecific(key, ptr));
96 }
97 static inline void usbi_tls_key_delete(usbi_tls_key_t key)
98 {
99 PTHREAD_CHECK(pthread_key_delete(key));
100 }
101
102 unsigned long usbi_get_tid(void);
103
104 #endif /* LIBUSB_THREADS_POSIX_H */