Files
react-native/packages/react-native/ReactCommon/oscompat/OSCompatPosix.cpp
T
Ruslan Lesiutin 994d7cc2f7 Define OSCompat module (#49302)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/49302

# Changelog: [Internal]

Added OS-agnostic module that will implement 2 basic capabilities:
- Getting current process id
- Getting current thread id

Reviewed By: javache

Differential Revision: D69316093

fbshipit-source-id: 114d235f1137eaf9c41d95df76f15532766d1bc8
2025-02-17 09:34:33 -08:00

64 lines
1.3 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#if defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) || \
defined(__ANDROID__)
#include "OSCompat.h"
#include <cassert>
#include <pthread.h>
#include <unistd.h>
#if defined(__MACH__)
#include <mach/mach.h>
#endif // defined(__MACH__)
#if defined(__linux__)
#include <sys/syscall.h>
#endif // defined(__linux__)
namespace facebook::react::oscompat {
uint64_t getCurrentProcessId() {
return getpid();
}
#if defined(__APPLE__) && defined(__MACH__)
uint64_t getCurrentThreadId() {
uint64_t tid = 0;
auto ret = pthread_threadid_np(nullptr, &tid);
assert(
ret == 0 &&
"Unexpected pthread_threadid_np failure while getting thread ID");
(void)ret;
return tid;
}
#elif defined(__ANDROID__)
uint64_t getCurrentThreadId() {
return gettid();
}
#elif defined(__linux__)
uint64_t getCurrentThreadId() {
return syscall(__NR_gettid);
}
#else
#error "getCurrentThreadID() is not supported on this platform"
#endif
} // namespace facebook::react::oscompat
#endif // defined(__linux__) || (defined(__APPLE__) && defined(__MACH__)) ||
// defined(__ANDROID__)