blob: 490e5cb10ff4b407cfe3fea0fa3a7d90f4ac170c [file] [log] [blame]
Jim Cownie5e8470a2013-09-27 10:38:44 +00001/*
2 * kmp_wrapper_getpid.h -- getpid() declaration.
Jim Cownie5e8470a2013-09-27 10:38:44 +00003 */
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 Peyton30419822017-05-12 18:01:32 +000021// 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 Cownie5e8470a2013-09-27 10:38:44 +000037
38#elif KMP_OS_WINDOWS
39
Jonathan Peyton30419822017-05-12 18:01:32 +000040// 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.
44typedef int pid_t;
45#define getpid _getpid
46#define __kmp_gettid() GetCurrentThreadId()
Jim Cownie5e8470a2013-09-27 10:38:44 +000047
48#else
49
Jonathan Peyton30419822017-05-12 18:01:32 +000050#error Unknown or unsupported OS.
Jim Cownie5e8470a2013-09-27 10:38:44 +000051
52#endif
53
Jonathan Peyton30419822017-05-12 18:01:32 +000054/* 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 Cownie5e8470a2013-09-27 10:38:44 +000058
Jonathan Peyton30419822017-05-12 18:01:32 +000059 printf( "%" KMP_UINT64_SPEC, (kmp_uint64) pid );
Jim Cownie5e8470a2013-09-27 10:38:44 +000060
Jonathan Peyton30419822017-05-12 18:01:32 +000061 or (at least) as
Jim Cownie5e8470a2013-09-27 10:38:44 +000062
Jonathan Peyton30419822017-05-12 18:01:32 +000063 printf( "%" KMP_UINT32_SPEC, (kmp_uint32) pid );
Jim Cownie5e8470a2013-09-27 10:38:44 +000064
Jonathan Peyton30419822017-05-12 18:01:32 +000065 (kmp_uint32, kmp_uint64, KMP_UINT64_SPEC, and KMP_UNIT32_SPEC are defined in
66 "kmp_os.h".) */
Jim Cownie5e8470a2013-09-27 10:38:44 +000067
68#endif // KMP_WRAPPER_GETPID_H
69
70// end of file //