summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/execution_stack.cpp
blob: 35b3281fd4a6ba873aaeffb4d4bcc5c6437b8da8 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include "execution_stack.h"

#if defined(_unix_)
#   include <sys/mman.h>
#   include <limits.h>
#   include <unistd.h>
#   if !defined(__x86_64__) && !defined(__arm64__) && !defined(__aarch64__)
#       error Unsupported platform
#   endif
#endif

#include <library/cpp/yt/memory/ref.h>
#include <library/cpp/yt/memory/ref_tracked.h>

#include <library/cpp/yt/misc/tls.h>

#include <library/cpp/yt/error/error.h>

#include <library/cpp/yt/system/exit.h>

namespace NYT::NThreading {

////////////////////////////////////////////////////////////////////////////////

TExecutionStackBase::TExecutionStackBase(size_t size)
    : Size_(RoundUpToPage(size))
{
    auto cookie = GetRefCountedTypeCookie<TExecutionStack>();
    TRefCountedTrackerFacade::AllocateInstance(cookie);
    TRefCountedTrackerFacade::AllocateSpace(cookie, Size_);
}

TExecutionStackBase::~TExecutionStackBase()
{
    auto cookie = GetRefCountedTypeCookie<TExecutionStack>();
    TRefCountedTrackerFacade::FreeInstance(cookie);
    TRefCountedTrackerFacade::FreeSpace(cookie, Size_);
}

void* TExecutionStackBase::GetStack() const
{
    return Stack_;
}

size_t TExecutionStackBase::GetSize() const
{
    return Size_;
}

////////////////////////////////////////////////////////////////////////////////

#if defined(_unix_)

TExecutionStack::TExecutionStack(size_t size)
    : TExecutionStackBase(size)
{
    size_t guardSize = GuardPageCount * GetPageSize();

    int flags =
#if defined(_darwin_)
        MAP_ANON | MAP_PRIVATE;
#else
        MAP_ANONYMOUS | MAP_PRIVATE;
#endif

    Base_ = reinterpret_cast<char*>(::mmap(
        0,
        guardSize * 2 + Size_,
        PROT_READ | PROT_WRITE,
        flags,
        -1,
        0));

    auto handleError = [&] {
        AbortProcessDramatically(
            EProcessExitCode::OutOfMemory,
            Format("Error creating execution stack (Size: %v): %v",
                Size_,
                TError::FromSystem()));
    };

    if (Base_ == MAP_FAILED) {
        handleError();
    }

    if (::mprotect(Base_, guardSize, PROT_NONE) == -1) {
        handleError();
    }

    if (::mprotect(Base_ + guardSize + Size_, guardSize, PROT_NONE) == -1) {
        handleError();
    }

    Stack_ = Base_ + guardSize;
    YT_VERIFY((reinterpret_cast<uintptr_t>(Stack_) & 15) == 0);
}

TExecutionStack::~TExecutionStack()
{
    const size_t guardSize = GuardPageCount * GetPageSize();
    ::munmap(Base_, guardSize * 2 + Size_);
}

#elif defined(_win_)

TExecutionStack::TExecutionStack(size_t size)
    : TExecutionStackBase(size)
    , Handle_(::CreateFiber(Size_, &FiberTrampoline, this))
{ }

TExecutionStack::~TExecutionStack()
{
    ::DeleteFiber(Handle_);
}

YT_DEFINE_THREAD_LOCAL(void*, FiberTrampolineOpaque);

void TExecutionStack::SetOpaque(void* opaque)
{
    FiberTrampolineOpaque() = opaque;
}

void* TExecutionStack::GetOpaque()
{
    return FiberTrampolineOpaque();
}

void TExecutionStack::SetTrampoline(void (*trampoline)(void*))
{
    YT_ASSERT(!Trampoline_);
    Trampoline_ = trampoline;
}

VOID CALLBACK TExecutionStack::FiberTrampoline(PVOID opaque)
{
    auto* stack = reinterpret_cast<TExecutionStack*>(opaque);
    stack->Trampoline_(FiberTrampolineOpaque());
}

#else
#   error Unsupported platform
#endif

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NThreading