aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/util/threadparkpad.cpp
blob: b939d6b61a6ec3982ff719329913eba88cfa0bf6 (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
147
148
149
150
151
152
#include "threadparkpad.h"
#include <util/system/winint.h>

#ifdef _linux_

#include "futex.h"

namespace NActors {
    class TThreadParkPad::TImpl {
        volatile bool Interrupted;
        int Futex;

    public:
        TImpl()
            : Interrupted(false)
            , Futex(0)
        {
        }
        ~TImpl() {
        }

        bool Park() noexcept {
            __atomic_fetch_sub(&Futex, 1, __ATOMIC_SEQ_CST);
            while (__atomic_load_n(&Futex, __ATOMIC_ACQUIRE) == -1)
                SysFutex(&Futex, FUTEX_WAIT_PRIVATE, -1, nullptr, nullptr, 0);
            return IsInterrupted();
        }

        void Unpark() noexcept {
            const int old = __atomic_fetch_add(&Futex, 1, __ATOMIC_SEQ_CST);
            if (old == -1)
                SysFutex(&Futex, FUTEX_WAKE_PRIVATE, -1, nullptr, nullptr, 0);
        }

        void Interrupt() noexcept {
            __atomic_store_n(&Interrupted, true, __ATOMIC_SEQ_CST);
            Unpark();
        }

        bool IsInterrupted() const noexcept {
            return __atomic_load_n(&Interrupted, __ATOMIC_ACQUIRE);
        }
    };

#elif defined _win32_
#include <library/cpp/deprecated/atomic/atomic.h>

#include <util/generic/bt_exception.h>
#include <util/generic/yexception.h>

namespace NActors {
    class TThreadParkPad::TImpl {
        TAtomic Interrupted;
        HANDLE EvHandle;

    public:
        TImpl()
            : Interrupted(false)
        {
            EvHandle = ::CreateEvent(0, false, false, 0);
            if (!EvHandle)
                ythrow TWithBackTrace<yexception>() << "::CreateEvent failed";
        }
        ~TImpl() {
            if (EvHandle)
                ::CloseHandle(EvHandle);
        }

        bool Park() noexcept {
            ::WaitForSingleObject(EvHandle, INFINITE);
            return AtomicGet(Interrupted);
        }

        void Unpark() noexcept {
            ::SetEvent(EvHandle);
        }

        void Interrupt() noexcept {
            AtomicSet(Interrupted, true);
            Unpark();
        }

        bool IsInterrupted() const noexcept {
            return AtomicGet(Interrupted);
        }
    };

#else

#include <library/cpp/deprecated/atomic/atomic.h>

#include <util/system/event.h>

namespace NActors {
    class TThreadParkPad::TImpl {
        TAtomic Interrupted;
        TSystemEvent Ev;

    public:
        TImpl()
            : Interrupted(false)
            , Ev(TSystemEvent::rAuto)
        {
        }
        ~TImpl() {
        }

        bool Park() noexcept {
            Ev.Wait();
            return AtomicGet(Interrupted);
        }

        void Unpark() noexcept {
            Ev.Signal();
        }

        void Interrupt() noexcept {
            AtomicSet(Interrupted, true);
            Unpark();
        }

        bool IsInterrupted() const noexcept {
            return AtomicGet(Interrupted);
        }
    };
#endif

    TThreadParkPad::TThreadParkPad()
        : Impl(new TThreadParkPad::TImpl())
    {
    }

    TThreadParkPad::~TThreadParkPad() {
    }

    bool TThreadParkPad::Park() noexcept {
        return Impl->Park();
    }

    void TThreadParkPad::Unpark() noexcept {
        Impl->Unpark();
    }

    void TThreadParkPad::Interrupt() noexcept {
        Impl->Interrupt();
    }

    bool TThreadParkPad::Interrupted() const noexcept {
        return Impl->IsInterrupted();
    }

}