2 * libusbx 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
22 #if defined(__linux__) || defined(__OpenBSD__)
24 # include <sys/syscall.h>
25 #elif defined(__APPLE__)
26 # include <mach/mach.h>
27 #elif defined(__CYGWIN__)
32 # if _XOPEN_SOURCE < 500
34 # define _XOPEN_SOURCE 500
37 #define _XOPEN_SOURCE 500
38 #endif /* _XOPEN_SOURCE */
40 #include "threads_posix.h"
42 int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
45 pthread_mutexattr_t stack_attr;
48 err = pthread_mutexattr_init(&stack_attr);
53 err = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
57 err = pthread_mutex_init(mutex, attr);
60 if (attr == &stack_attr)
61 pthread_mutexattr_destroy(&stack_attr);
66 int usbi_get_tid(void)
69 #if defined(__linux__)
70 ret = syscall(SYS_gettid);
71 #elif defined(__OpenBSD__)
72 /* The following only works with OpenBSD > 5.1 as it requires
73 real thread support. For 5.1 and earlier, -1 is returned. */
74 ret = syscall(SYS_getthrid);
75 #elif defined(__APPLE__)
76 ret = mach_thread_self();
77 mach_port_deallocate(mach_task_self(), ret);
78 #elif defined(__CYGWIN__)
79 ret = GetCurrentThreadId();
81 /* TODO: NetBSD thread ID support */