kmp_wrapper_getpid.h 2.0 KB

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