2 * Copyright (c) 2002 Frodo
3 * Portions Copyright (c) by the authors of ffmpeg and xvid
4 * Copyright (C) 2002-2013 Team XBMC
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, see
19 * <http://www.gnu.org/licenses/>.
22 // SingleLock.h: interface for the CSingleLock class.
24 //////////////////////////////////////////////////////////////////////
28 #include "threads/CriticalSection.h"
29 #include "threads/Lockables.h"
32 * This implements a "guard" pattern for a CCriticalSection that
33 * borrows most of it's functionality from boost's unique_lock.
35 class CSingleLock : public XbmcThreads::UniqueLock<CCriticalSection>
38 inline CSingleLock(CCriticalSection& cs) : XbmcThreads::UniqueLock<CCriticalSection>(cs) {}
39 inline CSingleLock(const CCriticalSection& cs) : XbmcThreads::UniqueLock<CCriticalSection> ((CCriticalSection&)cs) {}
41 inline void Leave() { unlock(); }
42 inline void Enter() { lock(); }
44 inline CSingleLock(CCriticalSection& cs, bool dicrim) : XbmcThreads::UniqueLock<CCriticalSection>(cs,true) {}
48 * This implements a "guard" pattern for a CCriticalSection that
49 * works like a CSingleLock but only "try"s the lock and so
50 * it's possible it doesn't actually get it..
52 class CSingleTryLock : public CSingleLock
55 inline CSingleTryLock(CCriticalSection& cs) : CSingleLock(cs,true) {}
57 inline bool IsOwner() const { return owns_lock(); }
61 * This implements a "guard" pattern for exiting all locks
62 * currently being held by the current thread and restoring
63 * those locks on destruction.
65 * This class can be used on a CCriticalSection that isn't owned
66 * by this thread in which case it will do nothing.
70 CCriticalSection& sec;
73 inline CSingleExit(CCriticalSection& cs) : sec(cs), count(cs.exit()) { }
74 inline ~CSingleExit() { sec.restore(count); }