Directory: | ./ |
---|---|
File: | src/SignalCatcher.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 21 | 21 | 100.0% |
Branches: | 6 | 8 | 75.0% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include <map> | ||
8 | #include <mutex> | ||
9 | |||
10 | #include "SignalCatcher.h" | ||
11 | |||
12 | ///@brief Handles signal with boolean an mutex | ||
13 | struct SignalHandler{ | ||
14 | ///Mutex of the class to avoid conflict manipulation between threads | ||
15 | std::mutex currentMutex; | ||
16 | ///Signal we want to check | ||
17 | int expectedSignal; | ||
18 | ///True if the signal has been recived, false otherwise | ||
19 | bool isSignalRecieved; | ||
20 | }; | ||
21 | |||
22 | ///Map of signal handlers | ||
23 | std::map<int, SignalHandler> phoenix_mapSignalHandler; | ||
24 | |||
25 | ///Signal handler | ||
26 | /** @param signum : recived signal | ||
27 | */ | ||
28 | 2 | void phoenix_sig_handler(int signum){ | |
29 |
1/1✓ Branch 1 taken 2 times.
|
2 | std::map<int, SignalHandler>::iterator it(phoenix_mapSignalHandler.find(signum)); |
30 |
1/2✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
2 | if(it != phoenix_mapSignalHandler.end()){ |
31 |
1/1✓ Branch 2 taken 2 times.
|
2 | std::lock_guard<std::mutex> guard(it->second.currentMutex); |
32 | 2 | it->second.isSignalRecieved = true; | |
33 | 2 | } | |
34 | 2 | } | |
35 | |||
36 | ///Add a signal catcher to handle the given signal | ||
37 | /** @param signalType : type of the signal to be handled | ||
38 | * You should always call this function into your main thread | ||
39 | */ | ||
40 | 2 | void phoenix_addSignalCatcher(int signalType){ | |
41 | 2 | SignalHandler & handler = phoenix_mapSignalHandler[signalType]; //Add the signal to the main map of signals | |
42 | 2 | handler.expectedSignal = signalType; | |
43 | 2 | handler.isSignalRecieved = false; | |
44 | 2 | signal(signalType, phoenix_sig_handler); | |
45 | 2 | } | |
46 | |||
47 | ///Check if the given signal has been recived by the program (or a thread) | ||
48 | /** @param signalType : type of signal to check | ||
49 | * @return true if the signal has been recived, false otherwise | ||
50 | */ | ||
51 | 6 | bool phoenix_isSignalRecived(int signalType){ | |
52 | 6 | bool b(false); | |
53 |
1/1✓ Branch 1 taken 6 times.
|
6 | std::map<int, SignalHandler>::iterator it(phoenix_mapSignalHandler.find(signalType)); |
54 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | if(it != phoenix_mapSignalHandler.end()){ |
55 |
1/1✓ Branch 2 taken 6 times.
|
6 | std::lock_guard<std::mutex> guard(it->second.currentMutex); |
56 | 6 | b = it->second.isSignalRecieved; | |
57 | 6 | } | |
58 | 6 | return b; | |
59 | } | ||
60 | |||
61 | |||
62 | |||
63 | |||
64 |