scoped_ignore_sigttou.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Support for signoring SIGTTOU.
  2. Copyright (C) 2003-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef SCOPED_IGNORE_SIGTTOU_H
  15. #define SCOPED_IGNORE_SIGTTOU_H
  16. #include "gdbsupport/scoped_ignore_signal.h"
  17. #include "gdbsupport/job-control.h"
  18. #ifdef SIGTTOU
  19. /* Simple wrapper that allows lazy initialization / destruction of T.
  20. Slightly more efficient than gdb::optional, because it doesn't
  21. carry storage to track whether the object has been initialized. */
  22. template<typename T>
  23. class lazy_init
  24. {
  25. public:
  26. void emplace ()
  27. {
  28. new (&m_u.obj) T ();
  29. }
  30. void reset ()
  31. {
  32. m_u.obj.~T ();
  33. }
  34. private:
  35. union u
  36. {
  37. /* Must define ctor/dtor if T has non-trivial ctor/dtor. */
  38. u () {}
  39. ~u () {}
  40. T obj;
  41. } m_u;
  42. };
  43. /* RAII class used to ignore SIGTTOU in a scope. This isn't simply
  44. scoped_ignore_signal<SIGTTOU> because we want to check the
  45. `job_control' global. */
  46. class scoped_ignore_sigttou
  47. {
  48. public:
  49. scoped_ignore_sigttou ()
  50. {
  51. if (job_control)
  52. m_ignore_signal.emplace ();
  53. }
  54. ~scoped_ignore_sigttou ()
  55. {
  56. if (job_control)
  57. m_ignore_signal.reset ();
  58. }
  59. DISABLE_COPY_AND_ASSIGN (scoped_ignore_sigttou);
  60. private:
  61. lazy_init<scoped_ignore_signal<SIGTTOU, false>> m_ignore_signal;
  62. };
  63. #else
  64. using scoped_ignore_sigttou = scoped_ignore_signal_nop;
  65. #endif
  66. #endif /* SCOPED_IGNORE_SIGTTOU_H */