Merge pull request #154 from hjelmn/darwin_gettime_fix
[platform/upstream/libusb.git] / libusb / os / threads_posix.c
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
22 #include <config.h>
23
24 #include <time.h>
25 #if defined(__linux__) || defined(__OpenBSD__)
26 # if defined(__OpenBSD__)
27 #  define _BSD_SOURCE
28 # endif
29 # include <unistd.h>
30 # include <sys/syscall.h>
31 #elif defined(__APPLE__)
32 # include <mach/mach.h>
33 #elif defined(__CYGWIN__)
34 # include <windows.h>
35 #endif
36
37 #include "threads_posix.h"
38 #include "libusbi.h"
39
40 int usbi_mutex_init_recursive(pthread_mutex_t *mutex)
41 {
42         int err;
43         pthread_mutexattr_t attr;
44
45         err = pthread_mutexattr_init(&attr);
46         if (err != 0)
47                 return err;
48
49         /* mutexattr_settype requires _GNU_SOURCE or _XOPEN_SOURCE >= 500 on Linux */
50         err = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
51         if (err != 0)
52                 goto finish;
53
54         err = pthread_mutex_init(mutex, &attr);
55
56 finish:
57         pthread_mutexattr_destroy(&attr);
58
59         return err;
60 }
61
62 int usbi_cond_timedwait(pthread_cond_t *cond,
63         pthread_mutex_t *mutex, const struct timeval *tv)
64 {
65         struct timespec timeout;
66         int r;
67
68         r = usbi_backend->clock_gettime(USBI_CLOCK_REALTIME, &timeout);
69         if (r < 0)
70                 return r;
71
72         timeout.tv_sec += tv->tv_sec;
73         timeout.tv_nsec += tv->tv_usec * 1000;
74         while (timeout.tv_nsec >= 1000000000L) {
75                 timeout.tv_nsec -= 1000000000L;
76                 timeout.tv_sec++;
77         }
78
79         return pthread_cond_timedwait(cond, mutex, &timeout);
80 }
81
82 int usbi_get_tid(void)
83 {
84         int ret = -1;
85 #if defined(__ANDROID__)
86         ret = gettid();
87 #elif defined(__linux__)
88         ret = syscall(SYS_gettid);
89 #elif defined(__OpenBSD__)
90         /* The following only works with OpenBSD > 5.1 as it requires
91            real thread support. For 5.1 and earlier, -1 is returned. */
92         ret = syscall(SYS_getthrid);
93 #elif defined(__APPLE__)
94         ret = mach_thread_self();
95         mach_port_deallocate(mach_task_self(), ret);
96 #elif defined(__CYGWIN__)
97         ret = GetCurrentThreadId();
98 #endif
99 /* TODO: NetBSD thread ID support */
100         return ret;
101 }