4 * Copyright (C) 2005-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/>.
23 #include "threads/Condition.h"
24 #include "threads/SingleLock.h"
25 #include "threads/Helpers.h"
28 * A CSharedSection is a mutex that satisfies the Shared Lockable concept (see Lockables.h).
33 XbmcThreads::ConditionVariable actualCv;
34 XbmcThreads::TightConditionVariable<XbmcThreads::InversePredicate<unsigned int&> > cond;
36 unsigned int sharedCount;
39 inline CSharedSection() : cond(actualCv,XbmcThreads::InversePredicate<unsigned int&>(sharedCount)), sharedCount(0) {}
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(); }
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(); } }
50 class CSharedLock : public XbmcThreads::SharedLock<CSharedSection>
53 inline CSharedLock(CSharedSection& cs) : XbmcThreads::SharedLock<CSharedSection>(cs) {}
54 inline CSharedLock(const CSharedSection& cs) : XbmcThreads::SharedLock<CSharedSection>((CSharedSection&)cs) {}
56 inline bool IsOwner() const { return owns_lock(); }
57 inline void Enter() { lock(); }
58 inline void Leave() { unlock(); }
61 class CExclusiveLock : public XbmcThreads::UniqueLock<CSharedSection>
64 inline CExclusiveLock(CSharedSection& cs) : XbmcThreads::UniqueLock<CSharedSection>(cs) {}
65 inline CExclusiveLock(const CSharedSection& cs) : XbmcThreads::UniqueLock<CSharedSection> ((CSharedSection&)cs) {}
67 inline bool IsOwner() const { return owns_lock(); }
68 inline void Leave() { unlock(); }
69 inline void Enter() { lock(); }