1 /*
2  *  Copyright (c) 2002 Frodo
3  *      Portions Copyright (c) by the authors of ffmpeg and xvid
4  *  Copyright (C) 2002-2018 Team Kodi
5  *  This file is part of Kodi - https://kodi.tv
6  *
7  *  SPDX-License-Identifier: GPL-2.0-or-later
8  *  See LICENSES/README.md for more information.
9  */
10 
11 #pragma once
12 
13 // SingleLock.h: interface for the CSingleLock class.
14 //
15 //////////////////////////////////////////////////////////////////////
16 
17 #include "threads/CriticalSection.h"
18 #include "threads/Lockables.h"
19 
20 /**
21  * This implements a "guard" pattern for a CCriticalSection that
22  *  borrows most of it's functionality from boost's unique_lock.
23  */
24 class CSingleLock : public XbmcThreads::UniqueLock<CCriticalSection>
25 {
26 public:
CSingleLock(CCriticalSection & cs)27   inline explicit CSingleLock(CCriticalSection& cs) : XbmcThreads::UniqueLock<CCriticalSection>(cs) {}
28 
Leave()29   inline void Leave() { unlock(); }
Enter()30   inline void Enter() { lock(); }
31 protected:
CSingleLock(CCriticalSection & cs,bool dicrim)32   inline CSingleLock(CCriticalSection& cs, bool dicrim) : XbmcThreads::UniqueLock<CCriticalSection>(cs,true) {}
33 };
34 
35 
36 /**
37  * This implements a "guard" pattern for exiting all locks
38  *  currently being held by the current thread and restoring
39  *  those locks on destruction.
40  *
41  * This class can be used on a CCriticalSection that isn't owned
42  *  by this thread in which case it will do nothing.
43  */
44 class CSingleExit
45 {
46   CCriticalSection& sec;
47   unsigned int count;
48 public:
CSingleExit(CCriticalSection & cs)49   inline explicit CSingleExit(CCriticalSection& cs) : sec(cs), count(cs.exit()) { }
~CSingleExit()50   inline ~CSingleExit() { sec.restore(count); }
51 };
52 
53