1/* Event pipe for GDB, the GNU debugger.
2
3   Copyright (C) 2021-2023 Free Software Foundation, Inc.
4
5   This file is part of GDB.
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 3 of the License, or
10   (at your option) any later version.
11
12   This program is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20#ifndef COMMON_EVENT_PIPE_H
21#define COMMON_EVENT_PIPE_H
22
23/* An event pipe used as a waitable file in the event loop in place of
24   some other event associated with a signal.  The handler for the
25   signal marks the event pipe to force a wakeup in the event loop.
26   This uses the well-known self-pipe trick.  */
27
28class event_pipe
29{
30public:
31  event_pipe() = default;
32  ~event_pipe();
33
34  DISABLE_COPY_AND_ASSIGN (event_pipe);
35
36  /* Create a new pipe.  */
37  bool open_pipe ();
38
39  /* Close the pipe.  */
40  void close_pipe ();
41
42  /* True if the event pipe has been opened.  */
43  bool is_open () const
44  { return m_fds[0] != -1; }
45
46  /* The file descriptor of the waitable file to use in the event
47     loop.  */
48  int event_fd () const
49  { return m_fds[0]; }
50
51  /* Flush the event pipe.  */
52  void flush ();
53
54  /* Put something in the pipe, so the event loop wakes up.  */
55  void mark ();
56private:
57  int m_fds[2] = { -1, -1 };
58};
59
60#endif /* COMMON_EVENT_PIPE_H */
61