1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef RECURSIVE_SCOPED_LOCK_H 4 #define RECURSIVE_SCOPED_LOCK_H 5 6 #include <boost/version.hpp> 7 #include <boost/thread/locks.hpp> 8 #include <boost/thread/recursive_mutex.hpp> 9 10 11 namespace Threading { 12 13 typedef boost::recursive_mutex RecursiveMutex; 14 15 class RecursiveScopedLock { 16 char sl_lock[sizeof(boost::recursive_mutex::scoped_lock)]; 17 public: 18 RecursiveScopedLock(boost::recursive_mutex& m, bool locked = true) { 19 #if (BOOST_VERSION >= 103500) 20 if (locked) { 21 new (sl_lock) boost::recursive_mutex::scoped_lock(m); 22 } else { 23 new (sl_lock) boost::recursive_mutex::scoped_lock(m, boost::defer_lock); 24 } 25 #else 26 new (sl_lock) boost::recursive_mutex::scoped_lock(m, locked); 27 #endif 28 } ~RecursiveScopedLock()29 virtual ~RecursiveScopedLock() { 30 #if (BOOST_VERSION >= 103500) 31 ((boost::recursive_mutex::scoped_lock*)sl_lock)->~unique_lock(); 32 #else 33 ((boost::recursive_mutex::scoped_lock*)sl_lock)->~scoped_lock(); 34 #endif 35 } 36 }; 37 38 } 39 40 #endif // RECURSIVE_SCOPED_LOCK_H 41