resolve cyclic dependency with zstd
[platform/upstream/cmake.git] / Source / cmUVSignalHackRAII.h
1 /* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
2    file Copyright.txt or https://cmake.org/licensing for details.  */
3 #pragma once
4 #include "cmConfigure.h" // IWYU pragma: keep
5
6 #include <cm3p/uv.h>
7
8 #if defined(CMAKE_USE_SYSTEM_LIBUV) && !defined(_WIN32) &&                    \
9   UV_VERSION_MAJOR == 1 && UV_VERSION_MINOR < 19
10 #  define CMAKE_UV_SIGNAL_HACK
11 #  include "cmUVHandlePtr.h"
12 /*
13    libuv does not use SA_RESTART on its signal handler, but C++ streams
14    depend on it for reliable i/o operations.  This RAII helper convinces
15    libuv to install its handler, and then revises the handler to add the
16    SA_RESTART flag.  We use a distinct uv loop that never runs to avoid
17    ever really getting a callback.  libuv may fill the hack loop's signal
18    pipe and then stop writing, but that won't break any real loops.
19  */
20 class cmUVSignalHackRAII
21 {
22   uv_loop_t HackLoop;
23   cm::uv_signal_ptr HackSignal;
24   static void HackCB(uv_signal_t*, int) {}
25
26 public:
27   cmUVSignalHackRAII()
28   {
29     uv_loop_init(&this->HackLoop);
30     this->HackSignal.init(this->HackLoop);
31     this->HackSignal.start(HackCB, SIGCHLD);
32     struct sigaction hack_sa;
33     sigaction(SIGCHLD, nullptr, &hack_sa);
34     if (!(hack_sa.sa_flags & SA_RESTART)) {
35       hack_sa.sa_flags |= SA_RESTART;
36       sigaction(SIGCHLD, &hack_sa, nullptr);
37     }
38   }
39   ~cmUVSignalHackRAII()
40   {
41     this->HackSignal.stop();
42     uv_loop_close(&this->HackLoop);
43   }
44 };
45 #endif