summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/system/process_id.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'library/cpp/yt/system/process_id.cpp')
-rw-r--r--library/cpp/yt/system/process_id.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/library/cpp/yt/system/process_id.cpp b/library/cpp/yt/system/process_id.cpp
new file mode 100644
index 00000000000..0be6dfd2913
--- /dev/null
+++ b/library/cpp/yt/system/process_id.cpp
@@ -0,0 +1,37 @@
+#include "process_id.h"
+
+#ifdef _unix_
+#include <library/cpp/yt/misc/static_initializer.h>
+
+#include <pthread.h>
+#endif
+
+namespace NYT {
+
+////////////////////////////////////////////////////////////////////////////////
+
+std::atomic<TProcessId> CachedProcessId = InvalidProcessId;
+
+namespace NDetail {
+
+TProcessId GetProcessIdImpl()
+{
+ return ::GetPID();
+}
+
+} // namespace NDetail
+
+#ifdef _unix_
+// The pid is stable for the lifetime of a process, so we cache it to avoid the
+// |getpid| syscall on each call. After a |fork|, however, the child runs with a
+// fresh pid, so the cache must be invalidated there.
+YT_STATIC_INITIALIZER(
+ ::pthread_atfork(
+ /*prepare*/ nullptr,
+ /*parent*/ nullptr,
+ /*child*/ [] { CachedProcessId.store(InvalidProcessId, std::memory_order::relaxed); }));
+#endif
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace NYT