threads_posix.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. #ifndef LIBUSB_THREADS_POSIX_H
  21. #define LIBUSB_THREADS_POSIX_H
  22. #include <pthread.h>
  23. #define PTHREAD_CHECK(expression) ASSERT_EQ(expression, 0)
  24. #define USBI_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
  25. typedef pthread_mutex_t usbi_mutex_static_t;
  26. static inline void usbi_mutex_static_lock(usbi_mutex_static_t *mutex)
  27. {
  28. PTHREAD_CHECK(pthread_mutex_lock(mutex));
  29. }
  30. static inline void usbi_mutex_static_unlock(usbi_mutex_static_t *mutex)
  31. {
  32. PTHREAD_CHECK(pthread_mutex_unlock(mutex));
  33. }
  34. typedef pthread_mutex_t usbi_mutex_t;
  35. static inline void usbi_mutex_init(usbi_mutex_t *mutex)
  36. {
  37. PTHREAD_CHECK(pthread_mutex_init(mutex, NULL));
  38. }
  39. static inline void usbi_mutex_lock(usbi_mutex_t *mutex)
  40. {
  41. PTHREAD_CHECK(pthread_mutex_lock(mutex));
  42. }
  43. static inline void usbi_mutex_unlock(usbi_mutex_t *mutex)
  44. {
  45. PTHREAD_CHECK(pthread_mutex_unlock(mutex));
  46. }
  47. static inline int usbi_mutex_trylock(usbi_mutex_t *mutex)
  48. {
  49. return pthread_mutex_trylock(mutex) == 0;
  50. }
  51. static inline void usbi_mutex_destroy(usbi_mutex_t *mutex)
  52. {
  53. PTHREAD_CHECK(pthread_mutex_destroy(mutex));
  54. }
  55. typedef pthread_cond_t usbi_cond_t;
  56. void usbi_cond_init(pthread_cond_t *cond);
  57. static inline void usbi_cond_wait(usbi_cond_t *cond, usbi_mutex_t *mutex)
  58. {
  59. PTHREAD_CHECK(pthread_cond_wait(cond, mutex));
  60. }
  61. int usbi_cond_timedwait(usbi_cond_t *cond,
  62. usbi_mutex_t *mutex, const struct timeval *tv);
  63. static inline void usbi_cond_broadcast(usbi_cond_t *cond)
  64. {
  65. PTHREAD_CHECK(pthread_cond_broadcast(cond));
  66. }
  67. static inline void usbi_cond_destroy(usbi_cond_t *cond)
  68. {
  69. PTHREAD_CHECK(pthread_cond_destroy(cond));
  70. }
  71. typedef pthread_key_t usbi_tls_key_t;
  72. static inline void usbi_tls_key_create(usbi_tls_key_t *key)
  73. {
  74. PTHREAD_CHECK(pthread_key_create(key, NULL));
  75. }
  76. static inline void *usbi_tls_key_get(usbi_tls_key_t key)
  77. {
  78. return pthread_getspecific(key);
  79. }
  80. static inline void usbi_tls_key_set(usbi_tls_key_t key, void *ptr)
  81. {
  82. PTHREAD_CHECK(pthread_setspecific(key, ptr));
  83. }
  84. static inline void usbi_tls_key_delete(usbi_tls_key_t key)
  85. {
  86. PTHREAD_CHECK(pthread_key_delete(key));
  87. }
  88. unsigned int usbi_get_tid(void);
  89. #endif /* LIBUSB_THREADS_POSIX_H */