summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/system/process_id.cpp
blob: 0be6dfd29139d3f144c9c8a9f71c7f9063db78b2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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