Remove LiveTV menu.
[vuplus_xbmc] / xbmc / threads / SharedSection.h
1 #pragma once
2
3 /*
4  *      Copyright (C) 2005-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
23 #include "threads/Condition.h"
24 #include "threads/SingleLock.h"
25 #include "threads/Helpers.h"
26
27 /**
28  * A CSharedSection is a mutex that satisfies the Shared Lockable concept (see Lockables.h).
29  */
30 class CSharedSection
31 {
32   CCriticalSection sec;
33   XbmcThreads::ConditionVariable actualCv;
34   XbmcThreads::TightConditionVariable<XbmcThreads::InversePredicate<unsigned int&> > cond;
35
36   unsigned int sharedCount;
37
38 public:
39   inline CSharedSection() : cond(actualCv,XbmcThreads::InversePredicate<unsigned int&>(sharedCount)), sharedCount(0)  {}
40
41   inline void lock() { CSingleLock l(sec); while (sharedCount) cond.wait(l); sec.lock(); }
42   inline bool try_lock() { return (sec.try_lock() ? ((sharedCount == 0) ? true : (sec.unlock(), false)) : false); }
43   inline void unlock() { sec.unlock(); }
44
45   inline void lock_shared() { CSingleLock l(sec); sharedCount++; }
46   inline bool try_lock_shared() { return (sec.try_lock() ? sharedCount++, sec.unlock(), true : false); }
47   inline void unlock_shared() { CSingleLock l(sec); sharedCount--; if (!sharedCount) { cond.notifyAll(); } }
48 };
49
50 class CSharedLock : public XbmcThreads::SharedLock<CSharedSection>
51 {
52 public:
53   inline CSharedLock(CSharedSection& cs) : XbmcThreads::SharedLock<CSharedSection>(cs) {}
54   inline CSharedLock(const CSharedSection& cs) : XbmcThreads::SharedLock<CSharedSection>((CSharedSection&)cs) {}
55
56   inline bool IsOwner() const { return owns_lock(); }
57   inline void Enter() { lock(); }
58   inline void Leave() { unlock(); }
59 };
60
61 class CExclusiveLock : public XbmcThreads::UniqueLock<CSharedSection>
62 {
63 public:
64   inline CExclusiveLock(CSharedSection& cs) : XbmcThreads::UniqueLock<CSharedSection>(cs) {}
65   inline CExclusiveLock(const CSharedSection& cs) : XbmcThreads::UniqueLock<CSharedSection> ((CSharedSection&)cs) {}
66
67   inline bool IsOwner() const { return owns_lock(); }
68   inline void Leave() { unlock(); }
69   inline void Enter() { lock(); }
70 };
71