diff options
| author | William A. Kennington III <wak@google.com> | 2018-11-02 17:25:36 -0700 |
|---|---|---|
| committer | William A. Kennington III <wak@google.com> | 2018-11-02 17:57:07 -0700 |
| commit | 9c97d2c4ef34256817fd39bdbaecdfafda4080df (patch) | |
| tree | 78f0751f881ef74e6211cd1541ce965c307c4763 /src | |
| parent | 4460f26c1182f412b5ab07f093a0f078191995ef (diff) | |
| download | stdplus-9c97d2c4ef34256817fd39bdbaecdfafda4080df.tar.gz stdplus-9c97d2c4ef34256817fd39bdbaecdfafda4080df.zip | |
signal: Add idempotent block function
This makes it trivial to block individual signals from being handled by
a thread. Useful when tryng to set up signal handling in event loops.
Tested:
Builds and passes unit tests.
Change-Id: I61739debe2a47ec0ec3e767cf138125c6f59165f
Signed-off-by: William A. Kennington III <wak@google.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/Makefile.am | 4 | ||||
| -rw-r--r-- | src/stdplus/placeholder.cpp | 1 | ||||
| -rw-r--r-- | src/stdplus/placeholder.hpp | 1 | ||||
| -rw-r--r-- | src/stdplus/signal.cpp | 32 | ||||
| -rw-r--r-- | src/stdplus/signal.hpp | 17 |
5 files changed, 51 insertions, 4 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index de5de8d..5e935f4 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -4,5 +4,5 @@ lib_LTLIBRARIES = libstdplus.la libstdplus_la_SOURCES = libstdplus_la_LIBADD = $(COMMON_LIBS) -nobase_include_HEADERS += stdplus/placeholder.hpp -libstdplus_la_SOURCES += stdplus/placeholder.cpp +nobase_include_HEADERS += stdplus/signal.hpp +libstdplus_la_SOURCES += stdplus/signal.cpp diff --git a/src/stdplus/placeholder.cpp b/src/stdplus/placeholder.cpp deleted file mode 100644 index a4c9e2b..0000000 --- a/src/stdplus/placeholder.cpp +++ /dev/null @@ -1 +0,0 @@ -#include <stdplus/placeholder.hpp> diff --git a/src/stdplus/placeholder.hpp b/src/stdplus/placeholder.hpp deleted file mode 100644 index 6f70f09..0000000 --- a/src/stdplus/placeholder.hpp +++ /dev/null @@ -1 +0,0 @@ -#pragma once diff --git a/src/stdplus/signal.cpp b/src/stdplus/signal.cpp new file mode 100644 index 0000000..10f8e39 --- /dev/null +++ b/src/stdplus/signal.cpp @@ -0,0 +1,32 @@ +#include <signal.h> +#include <stdplus/signal.hpp> +#include <system_error> + +namespace stdplus +{ +namespace signal +{ + +void block(int signum) +{ + sigset_t set; + if (sigprocmask(SIG_BLOCK, nullptr, &set) < 0) + { + throw std::system_error(errno, std::generic_category(), + "sigprocmask get"); + } + + if (sigaddset(&set, signum) < 0) + { + throw std::system_error(errno, std::generic_category(), "sigaddset"); + } + + if (sigprocmask(SIG_BLOCK, &set, nullptr) < 0) + { + throw std::system_error(errno, std::generic_category(), + "sigprocmask set"); + } +} + +} // namespace signal +} // namespace stdplus diff --git a/src/stdplus/signal.hpp b/src/stdplus/signal.hpp new file mode 100644 index 0000000..465181b --- /dev/null +++ b/src/stdplus/signal.hpp @@ -0,0 +1,17 @@ +#pragma once + +namespace stdplus +{ +namespace signal +{ + +/** @brief Blocks the signal from being handled by the designated + * sigaction. If the signal is already blocked this does nothing. + * + * @param[in] signum - The int representing the signal to block + * @throws std::system_error if any underlying error occurs. + */ +void block(int signum); + +} // namespace signal +} // namespace stdplus |

