summaryrefslogtreecommitdiff
path: root/libusb/os/threads_posix.c
blob: 763cd84e99833e2dd1948f9b64a513f8d3e0fec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
 * libusb synchronization using POSIX Threads
 *
 * Copyright © 2011 Vitali Lovich <vlovich@aliph.com>
 * Copyright © 2011 Peter Stuge <peter@stuge.se>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "libusbi.h"

#if defined(__ANDROID__)
# include <unistd.h>
#elif defined(__HAIKU__)
# include <os/kernel/OS.h>
#elif defined(__linux__)
# include <sys/syscall.h>
# include <unistd.h>
#elif defined(__NetBSD__)
# include <lwp.h>
#elif defined(__OpenBSD__)
# define _BSD_SOURCE
# include <sys/syscall.h>
# include <unistd.h>
#elif defined(__sun__)
# include <sys/lwp.h>
#endif

int usbi_cond_timedwait(pthread_cond_t *cond,
	pthread_mutex_t *mutex, const struct timeval *tv)
{
	struct timespec timeout;
	int r;

	r = usbi_clock_gettime(USBI_CLOCK_REALTIME, &timeout);
	if (r < 0)
		return r;

	timeout.tv_sec += tv->tv_sec;
	timeout.tv_nsec += tv->tv_usec * 1000;
	while (timeout.tv_nsec >= 1000000000L) {
		timeout.tv_nsec -= 1000000000L;
		timeout.tv_sec++;
	}

	return pthread_cond_timedwait(cond, mutex, &timeout);
}

int usbi_get_tid(void)
{
#ifdef HAVE_CC_THREAD_LOCAL
	static _Thread_local int tid;

	if (tid)
		return tid;
#else
	int tid;
#endif

#if defined(__ANDROID__)
	tid = gettid();
#elif defined(__APPLE__)
#ifdef HAVE_PTHREAD_THREADID_NP
	uint64_t thread_id;

	if (pthread_threadid_np(NULL, &thread_id) == 0)
		tid = (int)thread_id;
	else
		tid = -1;
#else
	tid = (int)pthread_mach_thread_np(pthread_self());
#endif
#elif defined(__HAIKU__)
	tid = get_pthread_thread_id(pthread_self());
#elif defined(__linux__)
	tid = (int)syscall(SYS_gettid);
#elif defined(__NetBSD__)
	tid = _lwp_self();
#elif defined(__OpenBSD__)
	/* The following only works with OpenBSD > 5.1 as it requires
	 * real thread support. For 5.1 and earlier, -1 is returned. */
	tid = syscall(SYS_getthrid);
#elif defined(__sun__)
	tid = _lwp_self();
#elif defined(_WIN32)
	tid = (int)GetCurrentThreadId();
#else
	tid = -1;
#endif

	if (tid == -1) {
		/* If we don't have a thread ID, at least return a unique
		 * value that can be used to distinguish individual
		 * threads. */
		tid = (int)(intptr_t)pthread_self();
	}

	return tid;
}