threads_posix.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * libusb synchronization using POSIX Threads
  3. *
  4. * Copyright © 2011 Vitali Lovich <vlovich@aliph.com>
  5. * Copyright © 2011 Peter Stuge <peter@stuge.se>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "libusbi.h"
  22. #include <errno.h>
  23. #if defined(__ANDROID__)
  24. # include <unistd.h>
  25. #elif defined(__HAIKU__)
  26. # include <os/kernel/OS.h>
  27. #elif defined(__linux__)
  28. # include <sys/syscall.h>
  29. # include <unistd.h>
  30. #elif defined(__NetBSD__)
  31. # include <lwp.h>
  32. #elif defined(__OpenBSD__)
  33. # define _BSD_SOURCE
  34. # include <sys/syscall.h>
  35. # include <unistd.h>
  36. #elif defined(__sun__)
  37. # include <sys/lwp.h>
  38. #endif
  39. void usbi_cond_init(pthread_cond_t *cond)
  40. {
  41. #ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK
  42. pthread_condattr_t condattr;
  43. PTHREAD_CHECK(pthread_condattr_init(&condattr));
  44. PTHREAD_CHECK(pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC));
  45. PTHREAD_CHECK(pthread_cond_init(cond, &condattr));
  46. PTHREAD_CHECK(pthread_condattr_destroy(&condattr));
  47. #else
  48. PTHREAD_CHECK(pthread_cond_init(cond, NULL));
  49. #endif
  50. }
  51. int usbi_cond_timedwait(pthread_cond_t *cond,
  52. pthread_mutex_t *mutex, const struct timeval *tv)
  53. {
  54. struct timespec timeout;
  55. int r;
  56. #ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK
  57. usbi_get_monotonic_time(&timeout);
  58. #else
  59. usbi_get_real_time(&timeout);
  60. #endif
  61. timeout.tv_sec += tv->tv_sec;
  62. timeout.tv_nsec += tv->tv_usec * 1000L;
  63. if (timeout.tv_nsec >= NSEC_PER_SEC) {
  64. timeout.tv_nsec -= NSEC_PER_SEC;
  65. timeout.tv_sec++;
  66. }
  67. r = pthread_cond_timedwait(cond, mutex, &timeout);
  68. if (r == 0)
  69. return 0;
  70. else if (r == ETIMEDOUT)
  71. return LIBUSB_ERROR_TIMEOUT;
  72. else
  73. return LIBUSB_ERROR_OTHER;
  74. }
  75. unsigned int usbi_get_tid(void)
  76. {
  77. static _Thread_local unsigned int tl_tid;
  78. int tid;
  79. if (tl_tid)
  80. return tl_tid;
  81. #if defined(__ANDROID__)
  82. tid = gettid();
  83. #elif defined(__APPLE__)
  84. #ifdef HAVE_PTHREAD_THREADID_NP
  85. uint64_t thread_id;
  86. if (pthread_threadid_np(NULL, &thread_id) == 0)
  87. tid = (int)thread_id;
  88. else
  89. tid = -1;
  90. #else
  91. tid = (int)pthread_mach_thread_np(pthread_self());
  92. #endif
  93. #elif defined(__HAIKU__)
  94. tid = get_pthread_thread_id(pthread_self());
  95. #elif defined(__linux__)
  96. tid = (int)syscall(SYS_gettid);
  97. #elif defined(__NetBSD__)
  98. tid = _lwp_self();
  99. #elif defined(__OpenBSD__)
  100. /* The following only works with OpenBSD > 5.1 as it requires
  101. * real thread support. For 5.1 and earlier, -1 is returned. */
  102. tid = syscall(SYS_getthrid);
  103. #elif defined(__sun__)
  104. tid = _lwp_self();
  105. #else
  106. tid = -1;
  107. #endif
  108. if (tid == -1) {
  109. /* If we don't have a thread ID, at least return a unique
  110. * value that can be used to distinguish individual
  111. * threads. */
  112. tid = (int)(intptr_t)pthread_self();
  113. }
  114. return tl_tid = (unsigned int)tid;
  115. }