|
Packit Service |
b0a153 |
/*
|
|
Packit Service |
b0a153 |
* libusb synchronization using POSIX Threads
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* Copyright © 2011 Vitali Lovich <vlovich@aliph.com>
|
|
Packit Service |
b0a153 |
* Copyright © 2011 Peter Stuge <peter@stuge.se>
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* This library is free software; you can redistribute it and/or
|
|
Packit Service |
b0a153 |
* modify it under the terms of the GNU Lesser General Public
|
|
Packit Service |
b0a153 |
* License as published by the Free Software Foundation; either
|
|
Packit Service |
b0a153 |
* version 2.1 of the License, or (at your option) any later version.
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* This library is distributed in the hope that it will be useful,
|
|
Packit Service |
b0a153 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Service |
b0a153 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Service |
b0a153 |
* Lesser General Public License for more details.
|
|
Packit Service |
b0a153 |
*
|
|
Packit Service |
b0a153 |
* You should have received a copy of the GNU Lesser General Public
|
|
Packit Service |
b0a153 |
* License along with this library; if not, write to the Free Software
|
|
Packit Service |
b0a153 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit Service |
b0a153 |
*/
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#include <config.h>
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#include <time.h>
|
|
Packit Service |
b0a153 |
#if defined(__linux__) || defined(__OpenBSD__)
|
|
Packit Service |
b0a153 |
# if defined(__OpenBSD__)
|
|
Packit Service |
b0a153 |
# define _BSD_SOURCE
|
|
Packit Service |
b0a153 |
# endif
|
|
Packit Service |
b0a153 |
# include <unistd.h>
|
|
Packit Service |
b0a153 |
# include <sys/syscall.h>
|
|
Packit Service |
b0a153 |
#elif defined(__APPLE__)
|
|
Packit Service |
b0a153 |
# include <pthread.h>
|
|
Packit Service |
b0a153 |
#elif defined(__CYGWIN__)
|
|
Packit Service |
b0a153 |
# include <windows.h>
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
#include "threads_posix.h"
|
|
Packit Service |
b0a153 |
#include "libusbi.h"
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
int usbi_cond_timedwait(pthread_cond_t *cond,
|
|
Packit Service |
b0a153 |
pthread_mutex_t *mutex, const struct timeval *tv)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
struct timespec timeout;
|
|
Packit Service |
b0a153 |
int r;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
r = usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &timeout);
|
|
Packit Service |
b0a153 |
if (r < 0)
|
|
Packit Service |
b0a153 |
return r;
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
timeout.tv_sec += tv->tv_sec;
|
|
Packit Service |
b0a153 |
timeout.tv_nsec += tv->tv_usec * 1000;
|
|
Packit Service |
b0a153 |
while (timeout.tv_nsec >= 1000000000L) {
|
|
Packit Service |
b0a153 |
timeout.tv_nsec -= 1000000000L;
|
|
Packit Service |
b0a153 |
timeout.tv_sec++;
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
return pthread_cond_timedwait(cond, mutex, &timeout);
|
|
Packit Service |
b0a153 |
}
|
|
Packit Service |
b0a153 |
|
|
Packit Service |
b0a153 |
int usbi_get_tid(void)
|
|
Packit Service |
b0a153 |
{
|
|
Packit Service |
b0a153 |
int ret;
|
|
Packit Service |
b0a153 |
#if defined(__ANDROID__)
|
|
Packit Service |
b0a153 |
ret = gettid();
|
|
Packit Service |
b0a153 |
#elif defined(__linux__)
|
|
Packit Service |
b0a153 |
ret = syscall(SYS_gettid);
|
|
Packit Service |
b0a153 |
#elif defined(__OpenBSD__)
|
|
Packit Service |
b0a153 |
/* The following only works with OpenBSD > 5.1 as it requires
|
|
Packit Service |
b0a153 |
real thread support. For 5.1 and earlier, -1 is returned. */
|
|
Packit Service |
b0a153 |
ret = syscall(SYS_getthrid);
|
|
Packit Service |
b0a153 |
#elif defined(__APPLE__)
|
|
Packit Service |
b0a153 |
ret = (int)pthread_mach_thread_np(pthread_self());
|
|
Packit Service |
b0a153 |
#elif defined(__CYGWIN__)
|
|
Packit Service |
b0a153 |
ret = GetCurrentThreadId();
|
|
Packit Service |
b0a153 |
#else
|
|
Packit Service |
b0a153 |
ret = -1;
|
|
Packit Service |
b0a153 |
#endif
|
|
Packit Service |
b0a153 |
/* TODO: NetBSD thread ID support */
|
|
Packit Service |
b0a153 |
return ret;
|
|
Packit Service |
b0a153 |
}
|