Skip to content

Commit

Permalink
Make SignalHandler more consistent with style guide
Browse files Browse the repository at this point in the history
Suffix class data members with '_', mark existing GetGlobals accessor const,
and make const accessors for `product_name_` and `output_base_`.

PiperOrigin-RevId: 251922411
  • Loading branch information
michajlo authored and irengrig committed Jun 18, 2019
1 parent 46a4057 commit fed402e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
17 changes: 9 additions & 8 deletions src/main/cpp/blaze_util_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,23 @@ class SignalHandler {
typedef void (* Callback)();

static SignalHandler& Get() { return INSTANCE; }
GlobalVariables* GetGlobals() { return _globals; }
void CancelServer() { _cancel_server(); }
GlobalVariables* GetGlobals() const { return globals_; }
const std::string& GetProductName() const { return product_name_; }
const std::string& GetOutputBase() const { return output_base_; }
void CancelServer() { cancel_server_(); }
void Install(const std::string &product_name, const std::string &output_base,
GlobalVariables* globals, Callback cancel_server);
ATTRIBUTE_NORETURN void PropagateSignalOrExit(int exit_code);

std::string _product_name;
std::string _output_base;

private:
static SignalHandler INSTANCE;

GlobalVariables* _globals;
Callback _cancel_server;
std::string product_name_;
std::string output_base_;
GlobalVariables* globals_;
Callback cancel_server_;

SignalHandler() : _globals(nullptr), _cancel_server(nullptr) {}
SignalHandler() : globals_(nullptr), cancel_server_(nullptr) {}
};

// A signal-safe version of fprintf(stderr, ...).
Expand Down
16 changes: 8 additions & 8 deletions src/main/cpp/blaze_util_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -141,23 +141,23 @@ static void handler(int signum) {
if (++sigint_count >= 3) {
SigPrintf(
"\n%s caught third interrupt signal; killed.\n\n",
SignalHandler::Get()._product_name.c_str());
SignalHandler::Get().GetProductName().c_str());
if (SignalHandler::Get().GetGlobals()->server_pid != -1) {
KillServerProcess(
SignalHandler::Get().GetGlobals()->server_pid,
SignalHandler::Get()._output_base);
SignalHandler::Get().GetOutputBase());
}
_exit(1);
}
SigPrintf(
"\n%s caught interrupt signal; shutting down.\n\n",
SignalHandler::Get()._product_name.c_str());
SignalHandler::Get().GetProductName().c_str());
SignalHandler::Get().CancelServer();
break;
case SIGTERM:
SigPrintf(
"\n%s caught terminate signal; shutting down.\n\n",
SignalHandler::Get()._product_name.c_str());
SignalHandler::Get().GetProductName().c_str());
SignalHandler::Get().CancelServer();
break;
case SIGPIPE:
Expand All @@ -178,10 +178,10 @@ void SignalHandler::Install(const string &product_name,
const string &output_base,
GlobalVariables* globals,
SignalHandler::Callback cancel_server) {
_product_name = product_name;
_output_base = output_base;
_globals = globals;
_cancel_server = cancel_server;
product_name_ = product_name;
output_base_ = output_base;
globals_ = globals;
cancel_server_ = cancel_server;

// Unblock all signals.
sigset_t sigset;
Expand Down
14 changes: 7 additions & 7 deletions src/main/cpp/blaze_util_windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,17 @@ BOOL WINAPI ConsoleCtrlHandler(_In_ DWORD ctrlType) {
if (++sigint_count >= 3) {
SigPrintf(
"\n%s caught third Ctrl+C handler signal; killed.\n\n",
SignalHandler::Get()._product_name.c_str());
SignalHandler::Get().GetProductName().c_str());
if (SignalHandler::Get().GetGlobals()->server_pid != -1) {
KillServerProcess(
SignalHandler::Get().GetGlobals()->server_pid,
SignalHandler::Get()._output_base);
SignalHandler::Get().GetOutputBase());
}
_exit(1);
}
SigPrintf(
"\n%s Ctrl+C handler; shutting down.\n\n",
SignalHandler::Get()._product_name.c_str());
SignalHandler::Get().GetProductName().c_str());
SignalHandler::Get().CancelServer();
return TRUE;

Expand All @@ -322,10 +322,10 @@ void SignalHandler::Install(const string &product_name,
const string &output_base,
GlobalVariables* globals,
SignalHandler::Callback cancel_server) {
_product_name = product_name;
_output_base = output_base;
_globals = globals;
_cancel_server = cancel_server;
product_name_ = product_name;
output_base_ = output_base;
globals_ = globals;
cancel_server_ = cancel_server;
::SetConsoleCtrlHandler(&ConsoleCtrlHandler, TRUE);
}

Expand Down

0 comments on commit fed402e

Please sign in to comment.