]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbsupport/scoped_ignore_sigttou.h
Introduce scoped_restore_signal
[thirdparty/binutils-gdb.git] / gdbsupport / scoped_ignore_sigttou.h
CommitLineData
965febe5 1/* Support for signoring SIGTTOU.
44270758 2
3666a048 3 Copyright (C) 2003-2021 Free Software Foundation, Inc.
44270758
AC
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
a9762ec7 9 the Free Software Foundation; either version 3 of the License, or
44270758
AC
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
a9762ec7 18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
44270758 19
965febe5
PA
20#ifndef SCOPED_IGNORE_SIGTTOU_H
21#define SCOPED_IGNORE_SIGTTOU_H
44270758 22
6a7f1c20 23#include "gdbsupport/scoped_ignore_signal.h"
268a13a5 24#include "gdbsupport/job-control.h"
766f8836 25
6a7f1c20
PA
26#ifdef SIGTTOU
27
28/* Simple wrapper that allows lazy initialization / destruction of T.
29 Slightly more efficient than gdb::optional, because it doesn't
30 carry storage to track whether the object has been initialized. */
31template<typename T>
32class lazy_init
33{
34public:
35 void emplace ()
36 {
37 new (&m_u.obj) T ();
38 }
39
40 void reset ()
41 {
42 m_u.obj.~T ();
43 }
44
45private:
46 union u
47 {
48 /* Must define ctor/dtor if T has non-trivial ctor/dtor. */
49 u () {}
50 ~u () {}
51
52 T obj;
53 } m_u;
54};
55
56/* RAII class used to ignore SIGTTOU in a scope. This isn't simply
57 scoped_ignore_signal<SIGTTOU> because we want to check the
58 `job_control' global. */
766f8836
AH
59
60class scoped_ignore_sigttou
61{
62public:
63 scoped_ignore_sigttou ()
64 {
766f8836 65 if (job_control)
6a7f1c20 66 m_ignore_signal.emplace ();
766f8836
AH
67 }
68
69 ~scoped_ignore_sigttou ()
70 {
766f8836 71 if (job_control)
6a7f1c20 72 m_ignore_signal.reset ();
766f8836
AH
73 }
74
75 DISABLE_COPY_AND_ASSIGN (scoped_ignore_sigttou);
76
77private:
6a7f1c20 78 lazy_init<scoped_ignore_signal<SIGTTOU>> m_ignore_signal;
766f8836 79};
44270758 80
6a7f1c20
PA
81#else
82
83using scoped_ignore_sigttou = scoped_ignore_signal_nop;
84
85#endif
86
965febe5 87#endif /* SCOPED_IGNORE_SIGTTOU_H */