mavtables  0.2.1
MAVLink router and firewall.
RecursionGuard.cpp
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 #include <mutex>
19 #include <thread>
20 
21 #include "RecursionData.hpp"
22 #include "RecursionError.hpp"
23 #include "RecursionGuard.hpp"
24 
25 
26 /** Construct a RecursionGuard.
27  *
28  * This marks the given \ref RecursionData structure, ensuring it cannot be
29  * used to construct another guard without raising a \ref RecursionError.
30  *
31  * \param data The object used to prevent recursion.
32  * \throws RecursionError if the given \p data structure has already been
33  * marked by a \ref RecursionGuard instance that is still in scope.
34  */
36  : data_(data)
37 {
38  std::lock_guard<std::mutex> lock(data_.mutex_);
39  std::thread::id id = std::this_thread::get_id();
40 
41  if (!(data_.calling_threads_.insert(id).second))
42  {
43  throw RecursionError("Recursion detected.");
44  }
45 }
46 
47 
48 /** Release the lock on the contain \ref RecursionData.
49  *
50  * This allows the function to be called again without error.
51  */
53 {
54  std::lock_guard<std::mutex> lock(data_.mutex_);
55  data_.calling_threads_.erase(std::this_thread::get_id());
56 }
RecursionGuard(RecursionData &data)