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 | |
| 5 | |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | // |
| 8 | // The LLVM Compiler Infrastructure |
| 9 | // |
| 10 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 11 | // Source Licenses. See LICENSE.txt for details. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | |
| 16 | #ifndef KMP_WRAPPER_GETPID_H |
| 17 | #define KMP_WRAPPER_GETPID_H |
| 18 | |
| 19 | #if KMP_OS_UNIX |
| 20 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 21 | // On Unix-like systems (Linux* OS and OS X*) getpid() is declared in standard |
| 22 | // headers. |
| 23 | #include <sys/syscall.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <unistd.h> |
| 26 | #if KMP_OS_DARWIN |
| 27 | // OS X |
| 28 | #define __kmp_gettid() syscall(SYS_thread_selfid) |
| 29 | #elif defined(SYS_gettid) |
| 30 | // Hopefully other Unix systems define SYS_gettid syscall for getting os thread |
| 31 | // id |
| 32 | #define __kmp_gettid() syscall(SYS_gettid) |
| 33 | #else |
| 34 | #warning No gettid found, use getpid instead |
| 35 | #define __kmp_gettid() getpid() |
| 36 | #endif |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 37 | |
| 38 | #elif KMP_OS_WINDOWS |
| 39 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 40 | // On Windows* OS _getpid() returns int (not pid_t) and is declared in |
| 41 | // "process.h". |
| 42 | #include <process.h> |
| 43 | // Let us simulate Unix. |
| 44 | typedef int pid_t; |
| 45 | #define getpid _getpid |
| 46 | #define __kmp_gettid() GetCurrentThreadId() |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 47 | |
| 48 | #else |
| 49 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 50 | #error Unknown or unsupported OS. |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 51 | |
| 52 | #endif |
| 53 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 54 | /* TODO: All the libomp source code uses pid_t type for storing the result of |
| 55 | getpid(), it is good. But often it printed as "%d", that is not good, because |
| 56 | it ignores pid_t definition (may pid_t be longer that int?). It seems all pid |
| 57 | prints should be rewritten as: |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 58 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 59 | printf( "%" KMP_UINT64_SPEC, (kmp_uint64) pid ); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 60 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 61 | or (at least) as |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 62 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 63 | printf( "%" KMP_UINT32_SPEC, (kmp_uint32) pid ); |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 64 | |
Jonathan Peyton | 3041982 | 2017-05-12 18:01:32 +0000 | [diff] [blame] | 65 | (kmp_uint32, kmp_uint64, KMP_UINT64_SPEC, and KMP_UNIT32_SPEC are defined in |
| 66 | "kmp_os.h".) */ |
Jim Cownie | 5e8470a | 2013-09-27 10:38:44 +0000 | [diff] [blame] | 67 | |
| 68 | #endif // KMP_WRAPPER_GETPID_H |
| 69 | |
| 70 | // end of file // |