2 * libusb synchronization using POSIX Threads
4 * Copyright © 2011 Vitali Lovich <vlovich@aliph.com>
5 * Copyright © 2011 Peter Stuge <peter@stuge.se>
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.
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.
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
25 #if defined(__linux__) || defined(__OpenBSD__)
26 # if defined(__OpenBSD__)
30 # include <sys/syscall.h>
31 #elif defined(__APPLE__)
32 # include <mach/mach.h>
33 #elif defined(__CYGWIN__)
37 #include "threads_posix.h"
40 int usbi_cond_timedwait(pthread_cond_t *cond,
41 pthread_mutex_t *mutex, const struct timeval *tv)
43 struct timespec timeout;
46 r = usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &timeout);
50 timeout.tv_sec += tv->tv_sec;
51 timeout.tv_nsec += tv->tv_usec * 1000;
52 while (timeout.tv_nsec >= 1000000000L) {
53 timeout.tv_nsec -= 1000000000L;
57 return pthread_cond_timedwait(cond, mutex, &timeout);
60 int usbi_get_tid(void)
63 #if defined(__ANDROID__)
65 #elif defined(__linux__)
66 ret = syscall(SYS_gettid);
67 #elif defined(__OpenBSD__)
68 /* The following only works with OpenBSD > 5.1 as it requires
69 real thread support. For 5.1 and earlier, -1 is returned. */
70 ret = syscall(SYS_getthrid);
71 #elif defined(__APPLE__)
72 ret = mach_thread_self();
73 mach_port_deallocate(mach_task_self(), ret);
74 #elif defined(__CYGWIN__)
75 ret = GetCurrentThreadId();
79 /* TODO: NetBSD thread ID support */