X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=libusb%2Fos%2Fthreads_posix.c;h=92bb11d7a03c8e393b4d32bdd0d96c672491b53c;hb=aaff15d48d1b8555aabf012b06bf39bf8aa4768a;hp=dc182b708dda20243e8ded0961ec5e05e9542c46;hpb=a544e5972bf2b0ac9e006439576f681a8439d311;p=platform%2Fupstream%2Flibusb.git diff --git a/libusb/os/threads_posix.c b/libusb/os/threads_posix.c index dc182b7..92bb11d 100644 --- a/libusb/os/threads_posix.c +++ b/libusb/os/threads_posix.c @@ -1,5 +1,5 @@ /* - * libusbx synchronization using POSIX Threads + * libusb synchronization using POSIX Threads * * Copyright © 2011 Vitali Lovich * Copyright © 2011 Peter Stuge @@ -19,64 +19,54 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ -#if defined(__linux__) || defined(__OpenBSD__) +#include "libusbi.h" + +#if defined(__ANDROID__) # include +#elif defined(__linux__) || defined(__OpenBSD__) +# if defined(__OpenBSD__) +# define _BSD_SOURCE +# endif # include -#elif defined(__APPLE__) -# include -#elif defined(__CYGWIN__) -# include #endif -#ifdef _XOPEN_SOURCE -# if _XOPEN_SOURCE < 500 -# undef _XOPEN_SOURCE -# define _XOPEN_SOURCE 500 -# endif -#else -#define _XOPEN_SOURCE 500 -#endif /* _XOPEN_SOURCE */ - -#include "threads_posix.h" - -int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) +int usbi_cond_timedwait(pthread_cond_t *cond, + pthread_mutex_t *mutex, const struct timeval *tv) { - int err; - pthread_mutexattr_t stack_attr; - if (!attr) { - attr = &stack_attr; - err = pthread_mutexattr_init(&stack_attr); - if (err != 0) - return err; - } + struct timespec timeout; + int r; - err = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE); - if (err != 0) - goto finish; + r = usbi_backend.clock_gettime(USBI_CLOCK_REALTIME, &timeout); + if (r < 0) + return r; - err = pthread_mutex_init(mutex, attr); - -finish: - if (attr == &stack_attr) - pthread_mutexattr_destroy(&stack_attr); + 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 err; + return pthread_cond_timedwait(cond, mutex, &timeout); } int usbi_get_tid(void) { - int ret = -1; -#if defined(__linux__) + int ret; +#if defined(__ANDROID__) + ret = gettid(); +#elif defined(__linux__) ret = syscall(SYS_gettid); #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. */ ret = syscall(SYS_getthrid); #elif defined(__APPLE__) - ret = mach_thread_self(); - mach_port_deallocate(mach_task_self(), ret); + ret = (int)pthread_mach_thread_np(pthread_self()); #elif defined(__CYGWIN__) ret = GetCurrentThreadId(); +#else + ret = -1; #endif /* TODO: NetBSD thread ID support */ return ret;