Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wait(CtrlCHandler&) #3429

Merged
merged 4 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 29 additions & 40 deletions cpp/include/Ice/CtrlCHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,58 +9,47 @@

namespace Ice
{
/**
* Invoked when a signal occurs. The callback must not raise exceptions.
* On Linux and macOS, the callback is NOT a signal handler and can call
* functions that are not async-signal safe.
* @param sig The signal number that occurred.
*/
/// The function called by CtrlCHandler when it catches a signal. This function must not throw exceptions. On Linux
/// and macOS, this function is NOT a signal handler and can call functions that are not async-signal safe.
/// @param sig The signal number that occurred.
/// \headerfile Ice/Ice.h
using CtrlCHandlerCallback = std::function<void(int sig)>;

/**
* Provides a portable way to handle Ctrl-C and Ctrl-C like signals.
* On Linux and macOS, the CtrlCHandler handles SIGHUP, SIGINT and SIGTERM.
* On Windows, it is essentially a wrapper for SetConsoleCtrlHandler().
*
* \headerfile Ice/Ice.h
*/
/// Provides a portable way to handle Ctrl-C and Ctrl-C like signals. On Linux and macOS, the CtrlCHandler handles
/// SIGHUP, SIGINT and SIGTERM. On Windows, it is essentially a wrapper for SetConsoleCtrlHandler.
/// \headerfile Ice/Ice.h
class ICE_API CtrlCHandler
{
public:
/**
* Registers a callback function that handles Ctrl-C like signals.
* On Linux and macOS, this constructor masks the SIGHUP, SIGINT and SIGTERM
* signals and then creates a thread that waits for these signals using sigwait.
* On Windows, this constructor calls SetConsoleCtrlCHandler to register a handler
* routine that calls the supplied callback function.
* Only a single CtrlCHandler object can exist in a process at a give time.
* @param cb The callback function to invoke when a signal is received.
*/
/// Constructs a CtrlCHandler.
/// On Linux and macOS, this constructor masks the SIGHUP, SIGINT and SIGTERM signals and then creates a thread
/// that waits for these signals using sigwait. On Windows, this constructor calls SetConsoleCtrlCHandler to
/// register a handler routine that calls the supplied callback function.
/// Only a single CtrlCHandler object can exist in a process at a give time.
/// @param cb The callback function to invoke when a signal is received. The default (nullptr) means do nothing.
explicit CtrlCHandler(CtrlCHandlerCallback cb = nullptr);

/**
* Unregisters the callback function.
* On Linux and macOS, this destructor joins and terminates the thread created
* by the constructor but does not "unmask" SIGHUP, SIGINT and SIGTERM. As a result,
* these signals are ignored after this destructor completes.
* On Windows, this destructor unregisters the SetConsoleCtrlHandler handler
* routine, and as a result a Ctrl-C or similar signal will terminate the application
* after this destructor completes.
*/
/// Unregisters the callback function.
/// On Linux and macOS, this destructor joins and terminates the thread created by the constructor but does not
/// "unmask" SIGHUP, SIGINT and SIGTERM. As a result, these signals are ignored after this destructor completes.
/// On Windows, this destructor unregisters the SetConsoleCtrlHandler handler routine, and as a result a
/// Ctrl-C or similar signal will terminate the application after this destructor completes.
~CtrlCHandler();

/**
* Replaces the signal callback.
* @param cb The new callback.
* @return The old callback, or nil if no callback is currently set.
*/
/// Replaces the signal callback.
/// @param cb The new callback.
/// @return The old callback, which may be nullptr.
CtrlCHandlerCallback setCallback(CtrlCHandlerCallback cb);

/**
* Obtains the signal callback.
* @return The callback.
*/
/// Obtains the current signal callback.
/// @return The callback.
[[nodiscard]] CtrlCHandlerCallback getCallback() const;

/// Waits until this handler catches a signal.
/// @return The signal number that was caught.
/// @remark This function installs a signal callback. It must not be called when a non-null callback is already
/// installed.
int wait();
};
}

Expand Down
22 changes: 22 additions & 0 deletions cpp/src/Ice/CtrlCHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#endif

#include <cassert>
#include <future>
#include <mutex>

#ifndef _WIN32
Expand Down Expand Up @@ -202,3 +203,24 @@ CtrlCHandler::~CtrlCHandler()
}

#endif

int
CtrlCHandler::wait()
{
promise<int> promise;

CtrlCHandlerCallback oldCallback = setCallback(
[&promise, this](int sig)
{
setCallback(nullptr); // ignore further signals
promise.set_value(sig);
});

if (oldCallback)
{
setCallback(oldCallback);
throw Ice::LocalException{__FILE__, __LINE__, "do not call wait on a CtrlCHandler with a registered callback"};
}

return promise.get_future().get();
}