| 1 | /* |
| 2 | * kmp_wrapper_getpid.h -- getpid() declaration. |
| 3 | */ |
| 4 | |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | // |
| 7 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 8 | // See https://llvm.org/LICENSE.txt for license information. |
| 9 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef KMP_WRAPPER_GETPID_H |
| 14 | #define KMP_WRAPPER_GETPID_H |
| 15 | |
| 16 | #if KMP_OS_UNIX |
| 17 | |
| 18 | // On Unix-like systems (Linux* OS and OS X*) getpid() is declared in standard |
| 19 | // headers. |
| 20 | #if !KMP_OS_AIX && !KMP_OS_HAIKU |
| 21 | #include <sys/syscall.h> |
| 22 | #endif |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | #if KMP_OS_DARWIN |
| 26 | // OS X |
| 27 | #define __kmp_gettid() pthread_mach_thread_np(pthread_self()) |
| 28 | #elif KMP_OS_FREEBSD || KMP_OS_DRAGONFLY |
| 29 | #include <pthread_np.h> |
| 30 | #define __kmp_gettid() pthread_getthreadid_np() |
| 31 | #elif KMP_OS_NETBSD |
| 32 | #include <lwp.h> |
| 33 | #define __kmp_gettid() _lwp_self() |
| 34 | #elif KMP_OS_OPENBSD |
| 35 | #define __kmp_gettid() getthrid() |
| 36 | #elif KMP_OS_AIX || KMP_OS_SOLARIS |
| 37 | #include <pthread.h> |
| 38 | #define __kmp_gettid() pthread_self() |
| 39 | #elif KMP_OS_HAIKU |
| 40 | #include <OS.h> |
| 41 | #define __kmp_gettid() find_thread(NULL) |
| 42 | #elif defined(SYS_gettid) |
| 43 | // Hopefully other Unix systems define SYS_gettid syscall for getting os thread |
| 44 | // id |
| 45 | #define __kmp_gettid() syscall(SYS_gettid) |
| 46 | #else |
| 47 | #warning No gettid found, use getpid instead |
| 48 | #define __kmp_gettid() getpid() |
| 49 | #endif |
| 50 | |
| 51 | #elif KMP_OS_WINDOWS |
| 52 | |
| 53 | // On Windows* OS _getpid() returns int (not pid_t) and is declared in |
| 54 | // "process.h". |
| 55 | #include <process.h> |
| 56 | // Let us simulate Unix. |
| 57 | #if KMP_MSVC_COMPAT |
| 58 | typedef int pid_t; |
| 59 | #endif |
| 60 | #define getpid _getpid |
| 61 | #define __kmp_gettid() GetCurrentThreadId() |
| 62 | |
| 63 | #else |
| 64 | |
| 65 | #error Unknown or unsupported OS. |
| 66 | |
| 67 | #endif |
| 68 | |
| 69 | /* TODO: All the libomp source code uses pid_t type for storing the result of |
| 70 | getpid(), it is good. But often it printed as "%d", that is not good, because |
| 71 | it ignores pid_t definition (may pid_t be longer that int?). It seems all pid |
| 72 | prints should be rewritten as: |
| 73 | |
| 74 | printf( "%" KMP_UINT64_SPEC, (kmp_uint64) pid ); |
| 75 | |
| 76 | or (at least) as |
| 77 | |
| 78 | printf( "%" KMP_UINT32_SPEC, (kmp_uint32) pid ); |
| 79 | |
| 80 | (kmp_uint32, kmp_uint64, KMP_UINT64_SPEC, and KMP_UNIT32_SPEC are defined in |
| 81 | "kmp_os.h".) */ |
| 82 | |
| 83 | #endif // KMP_WRAPPER_GETPID_H |
| 84 | |
| 85 | // end of file // |
| 86 | |