Line data Source code
1 : // Copyright (c) 2020-2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef BITCOIN_UTIL_WPIPE_H 6 : #define BITCOIN_UTIL_WPIPE_H 7 : 8 : #include <array> 9 : #include <assert.h> 10 : #include <atomic> 11 : 12 : #ifndef WIN32 13 : #define USE_WAKEUP_PIPE 14 : #endif 15 : 16 : class EdgeTriggeredEvents; 17 : 18 : /** 19 : * A manager for abstracting logic surrounding wakeup pipes. Supported only on 20 : * platforms with a POSIX API. Disabled on Windows. 21 : */ 22 : class WakeupPipe 23 : { 24 : private: 25 : /* Iterate through m_pipe and ::close() them */ 26 : void Close(); 27 : 28 : public: 29 : explicit WakeupPipe(EdgeTriggeredEvents* edge_trig_events); 30 : ~WakeupPipe(); 31 : 32 0 : bool IsValid() const { return m_valid; }; 33 : 34 : /* Drain pipe of all contents */ 35 : void Drain() const; 36 : /* Write a byte to the pipe */ 37 : void Write(); 38 : 39 : /* Used to wrap calls around m_need_wakeup toggling */ 40 : template <typename Callable> 41 0 : void Toggle(Callable&& func) 42 : { 43 0 : assert(m_valid); 44 : 45 0 : m_need_wakeup = true; 46 0 : func(); 47 0 : m_need_wakeup = false; 48 0 : } 49 : 50 : public: 51 : /* File descriptors for read and write data channels */ 52 : std::array<int, 2> m_pipe{{ -1, -1 }}; 53 : /* Flag used to determine if Write() needs to be called. Used occasionally */ 54 : std::atomic<bool> m_need_wakeup{false}; 55 : 56 : private: 57 : /* Instance validity flag set during construction */ 58 : bool m_valid{false}; 59 : /* Pointer to EdgeTriggeredEvents instance used for pipe (de)registration if using supported events modes */ 60 : EdgeTriggeredEvents* m_edge_trig_events{nullptr}; 61 : }; 62 : 63 : #endif // BITCOIN_UTIL_WPIPE_H