mavtables  0.2.1
MAVLink router and firewall.
RecursionGuard.hpp
Go to the documentation of this file.
1 // MAVLink router and firewall.
2 // Copyright (C) 2018 Michael R. Shannon <mrshannon.aerospace@gmail.com>
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 
18 #ifndef RECURSIONGUARD_HPP_
19 #define RECURSIONGUARD_HPP_
20 
21 
22 #include <mutex>
23 #include <thread>
24 
25 #include "RecursionData.hpp"
26 
27 
28 /** Guard against recursion.
29  *
30  * A recursion guard is an RAII (Resource Acquisition Is Initialization) data
31  * structure used to raise an error upon recursion. The constructor marks a
32  * \ref RecursionData structure, acquiring ownership of the containing function
33  * (within the given thread). Recursion guards treat calls from different
34  * threads separately, therefore, it will not guard against re-entrantcy.
35  *
36  * An example of how to use this is:
37  *
38  * ```
39  * #include "RecursionGuard.hpp"
40  *
41  * int a_function(int value)
42  * {
43  * // shared data between calls
44  * static RecursionData rdata;
45  * // take ownership of the call
46  * RecursionGuard rguard(rdata);
47  * return b_function(value);
48  * // the recursion guard is released upon destruction of rguard
49  * }
50  * ```
51  *
52  * %If `b_function`, or any function it calls, ever calls `a_function` then
53  * this will throw \ref RecursionError. However, if multiple threads call
54  * `a_function` (possibly at the same time) but `b_function` does not call
55  * `a_function` then no error will be thrown.
56  *
57  * \sa RecursionData
58  * \sa RecursionError
59  */
61 {
62  public:
65 
66  private:
67  RecursionData &data_;
68 };
69 
70 
71 #endif // RECURSIONGUARD_HPP_
RecursionGuard(RecursionData &data)