Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 1 | /* |
| 2 | * kmp_wrapper_getpid.h -- getpid() declaration. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 3 | */ |
| 4 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 5 | //===----------------------------------------------------------------------===// |
| 6 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 13 | #ifndef KMP_WRAPPER_GETPID_H |
| 14 | #define KMP_WRAPPER_GETPID_H |
| 15 | |
| 16 | #if KMP_OS_UNIX |
| 17 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 18 | // On Unix-like systems (Linux* OS and OS X*) getpid() is declared in standard |
| 19 | // headers. |
| 20 | #include <sys/syscall.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <unistd.h> |
| 23 | #if KMP_OS_DARWIN |
| 24 | // OS X |
| 25 | #define __kmp_gettid() syscall(SYS_thread_selfid) |
Dimitry Andric | dab9ed8 | 2019-03-25 18:37:14 +0000 | [diff] [blame] | 26 | #elif KMP_OS_FREEBSD |
| 27 | #include <pthread_np.h> |
| 28 | #define __kmp_gettid() pthread_getthreadid_np() |
Kamil Rytarowski | 98bdf1f | 2018-12-11 18:34:33 +0000 | [diff] [blame] | 29 | #elif KMP_OS_NETBSD |
| 30 | #include <lwp.h> |
| 31 | #define __kmp_gettid() _lwp_self() |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 32 | #elif defined(SYS_gettid) |
| 33 | // Hopefully other Unix systems define SYS_gettid syscall for getting os thread |
| 34 | // id |
| 35 | #define __kmp_gettid() syscall(SYS_gettid) |
| 36 | #else |
| 37 | #warning No gettid found, use getpid instead |
| 38 | #define __kmp_gettid() getpid() |
| 39 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 40 | |
| 41 | #elif KMP_OS_WINDOWS |
| 42 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 43 | // On Windows* OS _getpid() returns int (not pid_t) and is declared in |
| 44 | // "process.h". |
| 45 | #include <process.h> |
| 46 | // Let us simulate Unix. |
Andrey Churbanov | f700e9e | 2018-12-10 13:45:00 +0000 | [diff] [blame] | 47 | #if KMP_MSVC_COMPAT |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 48 | typedef int pid_t; |
Andrey Churbanov | f700e9e | 2018-12-10 13:45:00 +0000 | [diff] [blame] | 49 | #endif |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 50 | #define getpid _getpid |
| 51 | #define __kmp_gettid() GetCurrentThreadId() |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 52 | |
| 53 | #else |
| 54 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 55 | #error Unknown or unsupported OS. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 56 | |
| 57 | #endif |
| 58 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 59 | /* TODO: All the libomp source code uses pid_t type for storing the result of |
| 60 | getpid(), it is good. But often it printed as "%d", that is not good, because |
| 61 | it ignores pid_t definition (may pid_t be longer that int?). It seems all pid |
| 62 | prints should be rewritten as: |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 63 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 64 | printf( "%" KMP_UINT64_SPEC, (kmp_uint64) pid ); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 65 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 66 | or (at least) as |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 67 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 68 | printf( "%" KMP_UINT32_SPEC, (kmp_uint32) pid ); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 69 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 70 | (kmp_uint32, kmp_uint64, KMP_UINT64_SPEC, and KMP_UNIT32_SPEC are defined in |
| 71 | "kmp_os.h".) */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 72 | |
| 73 | #endif // KMP_WRAPPER_GETPID_H |
| 74 | |
| 75 | // end of file // |