OnixS C++ ICE Binary Order Entry Handler 1.1.1
API Documentation
Loading...
Searching...
No Matches
Signal.h
Go to the documentation of this file.
1/*
2 * Copyright Onix Solutions Limited [OnixS]. All rights reserved.
3 *
4 * This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
5 * and international copyright treaties.
6 *
7 * Access to and use of the software is governed by the terms of the applicable ONIXS Software
8 * Services Agreement (the Agreement) and Customer end user license agreements granting
9 * a non-assignable, non-transferable and non-exclusive license to use the software
10 * for it's own data processing purposes under the terms defined in the Agreement.
11 *
12 * Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
13 * of this source code or associated reference material to any other location for further reproduction
14 * or redistribution, and any amendments to this copyright notice, are expressly prohibited.
15 *
16 * Any reproduction or redistribution for sale or hiring of the Software not in accordance with
17 * the terms of the Agreement is a violation of copyright law.
18 */
19#pragma once
20
23
24#include <csignal>
25#include <atomic>
26#include <string>
27#include <sstream>
28#include <iostream>
29
30#ifdef _WIN32
31#include <conio.h>
32#else
33#include <pthread.h>
34#include <cstdlib>
35#include <vector>
36#include <sys/poll.h>
37#endif
38
39namespace Samples {
40
45{
46 static void manageSignals() noexcept
47 {
48 manageSignalsImpl();
49 }
50
51 ONIXS_ICEBOE_FORCEINLINE
52 static bool interruptDetected() noexcept
53 {
54 return interruptDetectedFlag().load(std::memory_order_relaxed);
55 }
56
57 static void waitUntilKey(const std::string& message)
58 {
59 std::clog << "\nPress <Ctrl+C> to " << message << ".." << std::endl;
60
61 while(!interruptDetected())
63 }
64
65private:
66 ONIXS_ICEBOE_FORCEINLINE
67 static std::atomic<bool>& interruptDetectedFlag() noexcept
68 {
69 static std::atomic<bool> interruptDetected {false};
70 return interruptDetected;
71 }
72
73#ifndef _WIN32
74 [[noreturn]]
75 static void errorAbort(int status, const char* reason)
76 {
77 std::cerr << "Aborted due to status " << status << ": " << (reason ? reason : "no error message") << std::endl;
78 exit(1);
79 }
80
81 [[noreturn]]
82 static void* signalWaiter(void* /*arg*/)
83 {
84 try {ONIXS_ICEBOE_NAMESPACE::Threading::ThisThread::affinity(0);} catch(...){}
85
86 while (true)
87 {
88 int signalNumber;
89 sigwait(&signalSet(), &signalNumber);
90
91 if(signalNumber == SIGINT)
92 interruptDetectedFlag().store(true);
93 else
94 std::cerr << "Signal " << signalNumber << " received and suppressed." << std::endl;
95 }
96 }
97
98 static void manageSignalsImpl() noexcept
99 {
100 constexpr int signals[] = { SIGINT, SIGPIPE };
101
102 pthread_t signalThreadId;
103
104 // Mask signals in the primary thread. Child threads will inherit this signal mask.
105 sigemptyset(&signalSet());
106 std::for_each(std::begin(signals), std::end(signals), [&](int signal){sigaddset(&signalSet(), signal);});
107
108 {
109 const auto status = pthread_sigmask(SIG_BLOCK, &signalSet(), nullptr);
110 if (status != 0)
111 errorAbort(status, "Set signal mask");
112 }
113 {
114 const auto status = pthread_create(&signalThreadId, nullptr, signalWaiter, nullptr);
115 if (status != 0)
116 errorAbort(status, "Create signalWaiter");
117 }
118 }
119
120 static sigset_t& signalSet() noexcept
121 {
122 static sigset_t set;
123 return set;
124 }
125#else
126 static void manageSignalsImpl() noexcept
127 {
128 signal(SIGINT, &SignalHelper::signalHandler);
129 }
130
131 static void signalHandler(int signal)
132 {
133 if (SIGINT == signal)
134 interruptDetectedFlag().store(true);
135 }
136#endif
137};
138
139}
static void sleep(size_t milliseconds)
Suspends the execution of the current thread for the given amount of time.
The code below illustrates how to manage signals:
Definition Signal.h:45
static ONIXS_ICEBOE_FORCEINLINE bool interruptDetected() noexcept
Definition Signal.h:52
static void waitUntilKey(const std::string &message)
Definition Signal.h:57
static void manageSignals() noexcept
Definition Signal.h:46