2 * Copyright (C) 2005-2011 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 #include <boost/thread/recursive_mutex.hpp>
29 * This template will take any implementation of the "Lockable" concept
30 * and allow it to be used as an "Exitable Lockable."
32 * Something that implements the "Lockable concept" simply means that
33 * it has the three methods:
39 * "Exitable" specifially means that, no matter how deep the recursion
40 * on the mutex/critical section, we can exit from it and then restore
43 * This requires us to extend the Lockable so that we can keep track of the
44 * number of locks that have been recursively acquired so that we can
45 * undo it, and then restore that (See class CSingleExit).
47 * All xbmc code expects Lockables to be recursive.
49 template<class L> class CountingLockable
56 inline CountingLockable() : count(0) {}
58 // boost::thread Lockable concept
59 inline void lock() { mutex.lock(); count++; }
60 inline bool try_lock() { return mutex.try_lock() ? count++, true : false; }
61 inline void unlock() { count--; mutex.unlock(); }
64 * This implements the "exitable" behavior mentioned above.
66 inline unsigned int exit()
68 // it's possibe we don't actually own the lock
73 ret = count - 1; // The -1 is because we don't want
74 // to count the try_lock increment.
75 while (count > 0) // This will also unlock the try_lock.
83 * Restore a previous exit to the provided level.
85 inline void restore(unsigned int restoreCount)
87 for (unsigned int i = 0; i < restoreCount; i++)
91 inline unsigned int getCount() { return count; }
93 inline L& getLockable() { return mutex; }
98 * A CCriticalSection is a CountingLockable whose implementation is a boost
101 * This is not a typedef because of a number of "class CCriticalSection;"
102 * forward declarations in the code that break when it's done that way.
104 class CCriticalSection : public XbmcThreads::CountingLockable<boost::recursive_mutex> {};