aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/threading/notification_handle.h
blob: 9ee4010a43844ca0bb539dcd1d454dfcebdf45a4 (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
#pragma once

#include "public.h"

#include <util/system/platform.h>

#ifdef _win_
    #include <util/system/pipe.h>
#endif

namespace NYT::NThreading {

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

//! Provides a handle which can be used to wake up a polling thread.
/*!
 *  Internally implemented via |eventfd| API (for Linux) or pipes (for all other platforms).
 */
class TNotificationHandle
{
public:
    explicit TNotificationHandle(bool blocking = false);
    ~TNotificationHandle();

    //! Called from an arbitrary thread to wake up the polling thread.
    //! Multiple wakeups are coalesced.
    void Raise();

    //! Called from the polling thread to clear all outstanding notification.
    void Clear();

    //! Returns the pollable handle, which becomes readable when #Raise is invoked.
    int GetFD() const;

private:
#ifdef _linux_
    int EventFD_ = -1;
#elif defined(_win_)
    TPipeHandle Reader_;
    TPipeHandle Writer_;
#else
    int PipeFDs_[2] = {-1, -1};
#endif

};

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

} // namespace NYT::NThreading