summaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/execution_stack.h
blob: fb719af77a1c0f407b7e1df468985171dcb3666c (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
#pragma once

#include "public.h"

#include <util/system/platform.h>

#if defined(_win_)
#include <windows.h>
#endif

namespace NYT::NThreading {

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

class TExecutionStackBase
{
public:
    TExecutionStackBase(const TExecutionStackBase& other) = delete;
    TExecutionStackBase& operator=(const TExecutionStackBase& other) = delete;

    virtual ~TExecutionStackBase();

    void* GetStack() const;
    size_t GetSize() const;

protected:
    const size_t Size_;
    void* Stack_ = nullptr;

    explicit TExecutionStackBase(size_t size);
};

#if defined(_unix_)

//! Mapped memory with a few extra guard pages.
class TExecutionStack
    : public TExecutionStackBase
{
public:
    explicit TExecutionStack(size_t size);
    ~TExecutionStack();

private:
    char* Base_ = nullptr;

    static const int GuardPageCount = 256;
};

#elif defined(_win_)

//! Stack plus Windows fiber holder.
class TExecutionStack
    : public TExecutionStackBase
{
public:
    explicit TExecutionStack(size_t size);
    ~TExecutionStack();

    static void SetOpaque(void* opaque);
    static void* GetOpaque();

    void SetTrampoline(void (*callee)(void*));

private:
    friend class TExecutionContext;

    void* const Handle_;
    void (*Trampoline_)(void*) = nullptr;

    static VOID CALLBACK FiberTrampoline(PVOID opaque);

    friend TExecutionContext CreateExecutionContext(
        TExecutionStack* stack,
        void (*trampoline)(void*));
};

#else
#   error Unsupported platform
#endif

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

} // namespace NYT::NThreading