notification_handle.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #pragma once
  2. #include "public.h"
  3. #include <util/system/platform.h>
  4. #ifdef _win_
  5. #include <util/system/pipe.h>
  6. #endif
  7. namespace NYT::NThreading {
  8. ////////////////////////////////////////////////////////////////////////////////
  9. //! Provides a handle which can be used to wake up a polling thread.
  10. /*!
  11. * Internally implemented via |eventfd| API (for Linux) or pipes (for all other platforms).
  12. */
  13. class TNotificationHandle
  14. {
  15. public:
  16. explicit TNotificationHandle(bool blocking = false);
  17. ~TNotificationHandle();
  18. //! Called from an arbitrary thread to wake up the polling thread.
  19. //! Multiple wakeups are coalesced.
  20. void Raise();
  21. //! Called from the polling thread to clear all outstanding notification.
  22. void Clear();
  23. //! Returns the pollable handle, which becomes readable when #Raise is invoked.
  24. int GetFD() const;
  25. private:
  26. #ifdef _linux_
  27. int EventFD_ = -1;
  28. #elif defined(_win_)
  29. TPipeHandle Reader_;
  30. TPipeHandle Writer_;
  31. #else
  32. int PipeFDs_[2] = {-1, -1};
  33. #endif
  34. };
  35. ////////////////////////////////////////////////////////////////////////////////
  36. } // namespace NYT::NThreading