| 1 | #ifndef _TEST_THREAD_ID_H |
| 2 | #define _TEST_THREAD_ID_H |
| 3 | |
| 4 | /* |
| 5 | * thread-id.h |
| 6 | * |
| 7 | * Userspace RCU library - thread ID |
| 8 | * |
| 9 | * Copyright 2013 - Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca> |
| 10 | * |
| 11 | * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED |
| 12 | * OR IMPLIED. ANY USE IS AT YOUR OWN RISK. |
| 13 | * |
| 14 | * Permission is hereby granted to use or copy this program |
| 15 | * for any purpose, provided the above notices are retained on all copies. |
| 16 | * Permission to modify the code and to distribute modified code is granted, |
| 17 | * provided the above notices are retained, and a notice that the code was |
| 18 | * modified is included with the above copyright notice. |
| 19 | */ |
| 20 | |
| 21 | #ifdef __linux__ |
| 22 | # include <syscall.h> |
| 23 | |
| 24 | # if defined(_syscall0) |
| 25 | _syscall0(pid_t, gettid) |
| 26 | # elif defined(__NR_gettid) |
| 27 | static inline pid_t gettid(void) |
| 28 | { |
| 29 | return syscall(__NR_gettid); |
| 30 | } |
| 31 | # endif |
| 32 | |
| 33 | static inline |
| 34 | unsigned long urcu_get_thread_id(void) |
| 35 | { |
| 36 | return (unsigned long) gettid(); |
| 37 | } |
| 38 | #elif defined(__FreeBSD__) |
| 39 | # include <pthread_np.h> |
| 40 | |
| 41 | static inline |
| 42 | unsigned long urcu_get_thread_id(void) |
| 43 | { |
| 44 | return (unsigned long) pthread_getthreadid_np(); |
| 45 | } |
| 46 | #elif defined(__sun__) || defined(__APPLE__) |
| 47 | #include <pthread.h> |
| 48 | |
| 49 | static inline |
| 50 | unsigned long urcu_get_thread_id(void) |
| 51 | { |
| 52 | return (unsigned long) pthread_self(); |
| 53 | } |
| 54 | #elif defined(__CYGWIN__) |
| 55 | #include <pthread.h> |
| 56 | |
| 57 | extern unsigned long pthread_getsequence_np(pthread_t *); |
| 58 | |
| 59 | static inline |
| 60 | unsigned long urcu_get_thread_id(void) |
| 61 | { |
| 62 | pthread_t thr = pthread_self(); |
| 63 | return pthread_getsequence_np(&thr); |
| 64 | } |
| 65 | |
| 66 | #else |
| 67 | # warning "use pid as thread ID" |
| 68 | static inline |
| 69 | unsigned long urcu_get_thread_id(void) |
| 70 | { |
| 71 | return (unsigned long) getpid(); |
| 72 | } |
| 73 | #endif |
| 74 | |
| 75 | #endif /* _TEST_THREAD_ID_H */ |