Fix keymap.
[vuplus_xbmc] / xbmc / threads / SingleLock.h
1 /*
2  *      Copyright (c) 2002 Frodo
3  *      Portions Copyright (c) by the authors of ffmpeg and xvid
4  *      Copyright (C) 2002-2013 Team XBMC
5  *      http://xbmc.org
6  *
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)
10  *  any later version.
11  *
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.
16  *
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/>.
20  *
21  */
22 // SingleLock.h: interface for the CSingleLock class.
23 //
24 //////////////////////////////////////////////////////////////////////
25
26 #pragma once
27
28 #include "threads/CriticalSection.h"
29 #include "threads/Lockables.h"
30
31 /**
32  * This implements a "guard" pattern for a CCriticalSection that
33  *  borrows most of it's functionality from boost's unique_lock.
34  */
35 class CSingleLock : public XbmcThreads::UniqueLock<CCriticalSection>
36 {
37 public:
38   inline CSingleLock(CCriticalSection& cs) : XbmcThreads::UniqueLock<CCriticalSection>(cs) {}
39   inline CSingleLock(const CCriticalSection& cs) : XbmcThreads::UniqueLock<CCriticalSection> ((CCriticalSection&)cs) {}
40
41   inline void Leave() { unlock(); }
42   inline void Enter() { lock(); }
43 protected:
44   inline CSingleLock(CCriticalSection& cs, bool dicrim) : XbmcThreads::UniqueLock<CCriticalSection>(cs,true) {}
45 };
46
47 /**
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..
51  */
52 class CSingleTryLock : public CSingleLock
53 {
54 public:
55   inline CSingleTryLock(CCriticalSection& cs) : CSingleLock(cs,true) {}
56
57   inline bool IsOwner() const { return owns_lock(); }
58 };
59
60 /**
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.
64  *
65  * This class can be used on a CCriticalSection that isn't owned
66  *  by this thread in which case it will do nothing.
67  */
68 class CSingleExit
69 {
70   CCriticalSection& sec;
71   unsigned int count;
72 public:
73   inline CSingleExit(CCriticalSection& cs) : sec(cs), count(cs.exit()) { }
74   inline ~CSingleExit() { sec.restore(count); }
75 };
76