pstore2
signal_cv.hpp
Go to the documentation of this file.
1 //===- include/pstore/os/signal_cv.hpp --------------------*- mode: C++ -*-===//
2 //* _ _ *
3 //* ___(_) __ _ _ __ __ _| | _____ __ *
4 //* / __| |/ _` | '_ \ / _` | | / __\ \ / / *
5 //* \__ \ | (_| | | | | (_| | | | (__ \ V / *
6 //* |___/_|\__, |_| |_|\__,_|_| \___| \_/ *
7 //* |___/ *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
16 
18 
19 #ifndef PSTORE_OS_SIGNAL_CV_HPP
20 #define PSTORE_OS_SIGNAL_CV_HPP
21 
22 #include <atomic>
23 #include <mutex>
24 
25 #include "pstore/os/descriptor.hpp"
26 
27 namespace pstore {
28 
33  public:
35  // No copying or assignment.
38  descriptor_condition_variable & operator= (descriptor_condition_variable const &) = delete;
40 
41  virtual ~descriptor_condition_variable () = default;
42 
44  void notify_all ();
45 
50  void notify_all_no_except () noexcept;
51 
57  void wait (std::unique_lock<std::mutex> & lock);
58 
59  void wait ();
60 
61  pipe_descriptor const & wait_descriptor () const noexcept;
62  void reset ();
63 
64  private:
65 #ifdef _WIN32
66  pipe_descriptor event_;
67 #else
68  pipe_descriptor read_fd_{};
69  pipe_descriptor write_fd_{};
70 
71  static void make_non_blocking (int fd);
72  static int write (int fd) noexcept;
73 #endif
74  };
75 
76 
77  class signal_cv {
78  public:
79  signal_cv () = default;
80 
81  // No copying or assignment.
82  signal_cv (signal_cv const &) = delete;
83  signal_cv (signal_cv &&) = delete;
84 
85  ~signal_cv () noexcept = default;
86 
87  signal_cv & operator= (signal_cv const &) = delete;
88  signal_cv & operator= (signal_cv &&) = delete;
89 
95  void notify_all (int const signal) noexcept {
96  signal_ = signal;
97  cv_.notify_all_no_except ();
98  }
99 
105  void wait (std::unique_lock<std::mutex> & lock) { cv_.wait (lock); }
106  void wait () { cv_.wait (); }
107 
108  pipe_descriptor const & wait_descriptor () const noexcept { return cv_.wait_descriptor (); }
109  void reset () { cv_.reset (); }
110 
111  int signal () const { return signal_.load (); }
112 
113  private:
114  std::atomic<int> signal_{-1};
116  };
117 
118 } // end namespace pstore
119 
120 #endif // PSTORE_OS_SIGNAL_CV_HPP
void wait(std::unique_lock< std::mutex > &lock)
Releases lock and blocks the current executing thread.
Definition: signal_cv_posix.cpp:141
void notify_all()
Unblocks all threads currently waiting for *this.
Definition: signal_cv_posix.cpp:91
void notify_all_no_except() noexcept
Unblocks all threads currently waiting for *this.
Definition: signal_cv_posix.cpp:103
Provides a platorm-independent "descriptor" class for accessing pipes and sockets.
void wait(std::unique_lock< std::mutex > &lock)
Releases lock and blocks the current executing thread.
Definition: signal_cv.hpp:105
On POSIX, This class implements the "self pipe trick" to enable a signal handler to call notify() to ...
Definition: signal_cv.hpp:32
DescriptorTraits is a traits structure of the following form:
Definition: descriptor.hpp:59
Definition: nonpod2.cpp:40
void notify_all(int const signal) noexcept
Unblocks all threads currently waiting for *this.
Definition: signal_cv.hpp:95
Definition: signal_cv.hpp:77