Home | History | Annotate | Download | only in os
      1 /*
      2  * libusbx synchronization using POSIX Threads
      3  *
      4  * Copyright  2011 Vitali Lovich <vlovich (at) aliph.com>
      5  * Copyright  2011 Peter Stuge <peter (at) 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 #if defined(__linux__) || defined(__OpenBSD__)
     23 # include <unistd.h>
     24 # include <sys/syscall.h>
     25 #elif defined(__APPLE__)
     26 # include <mach/mach.h>
     27 #elif defined(__CYGWIN__)
     28 # include <windows.h>
     29 #endif
     30 
     31 #include "threads_posix.h"
     32 
     33 int usbi_mutex_init_recursive(pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
     34 {
     35 	int err;
     36 	pthread_mutexattr_t stack_attr;
     37 	if (!attr) {
     38 		attr = &stack_attr;
     39 		err = pthread_mutexattr_init(&stack_attr);
     40 		if (err != 0)
     41 			return err;
     42 	}
     43 
     44 	/* mutexattr_settype requires _GNU_SOURCE or _XOPEN_SOURCE >= 500 on Linux */
     45 	err = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_RECURSIVE);
     46 	if (err != 0)
     47 		goto finish;
     48 
     49 	err = pthread_mutex_init(mutex, attr);
     50 
     51 finish:
     52 	if (attr == &stack_attr)
     53 		pthread_mutexattr_destroy(&stack_attr);
     54 
     55 	return err;
     56 }
     57 
     58 int usbi_get_tid(void)
     59 {
     60 	int ret = -1;
     61 #if defined(__ANDROID__)
     62 	ret = gettid();
     63 #elif defined(__linux__)
     64 	ret = syscall(SYS_gettid);
     65 #elif defined(__OpenBSD__)
     66 	/* The following only works with OpenBSD > 5.1 as it requires
     67 	   real thread support. For 5.1 and earlier, -1 is returned. */
     68 	ret = syscall(SYS_getthrid);
     69 #elif defined(__APPLE__)
     70 	ret = mach_thread_self();
     71 	mach_port_deallocate(mach_task_self(), ret);
     72 #elif defined(__CYGWIN__)
     73 	ret = GetCurrentThreadId();
     74 #endif
     75 /* TODO: NetBSD thread ID support */
     76 	return ret;
     77 }
     78