static void get_tname(ph_thread_t *me, char *buf, uint32_t size) { uint64_t tid; if (me) { ph_snprintf(buf, size, "%s/%d", me->name, me->tid); return; } #if defined(__linux__) tid = syscall(SYS_gettid); #elif defined(__MACH__) pthread_threadid_np(pthread_self(), &tid); #elif defined(__sun__) tid = _lwp_self(); #else tid = (uint64_t)(intptr_t)self; #endif #if defined(__linux__) || defined(__MACH__) if (pthread_getname_np(pthread_self(), buf, size) == 0) { int len = strlen(buf); if (len > 0) { ph_snprintf(buf + len, size - len, "/%" PRIu64, tid); return; } } #endif ph_snprintf(buf, size, "lwp/%" PRIu64, tid); }
unsigned long getCurrentThreadId() { pthread_t selfThread = pthread_self(); #ifdef __APPLE__ unsigned long long threadId; pthread_threadid_np(selfThread, &threadId); return threadId; #elif __linux return (long)selfThread; #endif }
/** * \brief Returns the ID of the calling thread * * It returns the ID of the calling thread. It works on Linux OS, Apple OS, * Windows. * * \return if successful the identifier of the thread is returned. Otherwise a * negative vlue is returned. * */ static inline long ff_getThreadID() { #if (defined(__GNUC__) && defined(__linux)) return gettid(); #elif defined(__APPLE__) && MAC_OS_X_HAS_AFFINITY uint64_t tid; pthread_threadid_np(NULL, &tid); return (long) tid; // > 10.6 only #elif (defined(_MSC_VER) || defined(__INTEL_COMPILER)) && defined(_WIN32) return GetCurrentThreadId(); #endif return -1; }
rtThreadId rtThreadGetCurrentId() { #ifdef __APPLE__ uint64_t threadId = 0; pthread_threadid_np(NULL, &threadId); return threadId; #elif WIN32 return GetCurrentThreadId(); #else return syscall(__NR_gettid); #endif }
unsigned long long getThreadID() { unsigned long long tid; #ifdef __APPLE__ pthread_threadid_np(NULL, &tid); #endif #ifdef __linux__ tid = syscall(SYS_gettid); #endif return tid; }
// Get numeric id of the current thread if possible on the // current platform. It is indended for logging purposes only. // Return: // Numeric id of the current thread, as best we can retrieve it. uint64_t GCToOSInterface::GetCurrentThreadIdForLogging() { #if defined(__linux__) return (uint64_t)syscall(SYS_gettid); #elif HAVE_PTHREAD_GETTHREADID_NP return (uint64_t)pthread_getthreadid_np(); #elif HAVE_PTHREAD_THREADID_NP unsigned long long tid; pthread_threadid_np(pthread_self(), &tid); return (uint64_t)tid; #else // Fallback in case we don't know how to get integer thread id on the current platform return (uint64_t)pthread_self(); #endif }
pid_t gettid(void) { #if defined(__APPLE__) uint64_t tid; pthread_threadid_np(NULL, &tid); return (pid_t)tid; #elif defined(__FreeBSD__) long tid; syscall(SYS_thr_self, &tid); return (pid_t)(tid); #elif defined(SYS_gettid) return (pid_t)syscall(SYS_gettid); #else return 0; #endif }
pid_t zh_gettid() { pid_t tid; #ifdef Darwin pthread_t ptid = pthread_self(); tid = pthread_threadid_np(ptid, &tid); #else #ifdef Linux #ifndef __NR_gettid #define __NR_gettid 224 #endif tid = syscall(__NR_gettid); #endif #endif return tid; }
pid_t gettid(void) { #ifdef __FreeBSD__ return (pid_t)pthread_getthreadid_np(); #elif defined(__APPLE__) #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) uint64_t curthreadid; pthread_threadid_np(NULL, &curthreadid); return (pid_t)curthreadid; #else /* __MAC_OS_X_VERSION_MIN_REQUIRED */ return (pid_t)pthread_self; #endif /* __MAC_OS_X_VERSION_MIN_REQUIRED */ #else /* __APPLE__*/ return (pid_t)syscall(SYS_gettid); #endif /* __FreeBSD__, __APPLE__*/ }
pid_t getTID() { #if defined(__APPLE__) || defined(__OSX__) // syscall is deprecated: first deprecated in macOS 10.12. // syscall is unsupported; // syscall pid_t tid = syscall(SYS_thread_selfid); uint64_t tid; pthread_threadid_np(NULL, &tid); #else #ifndef __NR_gettid #define __NR_gettid 224 #endif pid_t tid = syscall(__NR_gettid); #endif CHECK_NE((int)tid, -1); return tid; }
KernelThreadId kernelThreadId (void) { #if defined(linux_HOST_OS) pid_t tid = syscall(SYS_gettid); // no really, see man gettid return (KernelThreadId) tid; /* FreeBSD 9.0+ */ #elif defined(freebsd_HOST_OS) && (__FreeBSD_version >= 900031) return pthread_getthreadid_np(); // Check for OS X >= 10.6 (see #7356) #elif defined(darwin_HOST_OS) && !(defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && __MAC_OS_X_VERSION_MIN_REQUIRED < 1060) uint64_t ktid; pthread_threadid_np(NULL, &ktid); return ktid; #else // unsupported return 0; #endif }
uint64_t dylan_current_thread_id(void) { #if defined(OPEN_DYLAN_PLATFORM_LINUX) return syscall(SYS_gettid); #elif defined(OPEN_DYLAN_PLATFORM_DARWIN) uint64_t tid; pthread_threadid_np(pthread_self(), &tid); return tid; #elif defined(OPEN_DYLAN_PLATFORM_FREEBSD) return pthread_getthreadid_np(); #elif defined(OPEN_DYLAN_PLATFORM_NETBSD) return _lwp_self(); #elif defined(OPEN_DYLAN_PLATFORM_OPENBSD) return syscall(SYS_getthrid); #elif defined(OPEN_DYLAN_PLATFORM_WINDOWS) return GetCurrentThreadId(); #else #error dylan_current_thread_id is not yet implemented. #endif return 0; }
inline SIZE_T THREADSilentGetCurrentThreadId() { uint64_t tid; pthread_threadid_np(pthread_self(), &tid); return (SIZE_T)tid; }