changed: update version strings for beta4
[xbmc.git] / xbmc / utils / SingleLock.cpp
blobd07f8b87187d5626658bef6f0c87b02ab0a526f1
1 /*
2 * XBMC Media Center
3 * Copyright (c) 2002 Frodo
4 * Portions Copyright (c) by the authors of ffmpeg and xvid
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "SingleLock.h"
24 //////////////////////////////////////////////////////////////////////
25 // Construction/Destruction
26 //////////////////////////////////////////////////////////////////////
28 CSingleLock::CSingleLock(CCriticalSection& cs)
29 : m_cs( cs )
30 , m_bIsOwner( false )
32 Enter();
35 CSingleLock::CSingleLock(const CCriticalSection& cs)
36 : m_cs( const_cast<CCriticalSection&>(cs) )
37 , m_bIsOwner( false )
39 Enter();
42 CSingleLock::~CSingleLock()
44 Leave();
47 bool CSingleLock::IsOwner() const
49 return m_bIsOwner;
52 bool CSingleLock::Enter()
54 // Test if we already own the critical section
55 if ( true == m_bIsOwner )
57 return true;
60 // Blocking call
61 ::EnterCriticalSection( m_cs );
62 m_bIsOwner = true;
64 return m_bIsOwner;
67 void CSingleLock::Leave()
69 if ( false == m_bIsOwner )
71 return ;
74 ::LeaveCriticalSection( m_cs );
75 m_bIsOwner = false;
78 CSingleExit::CSingleExit(CCriticalSection& cs)
79 : m_cs( cs )
80 , m_count(0)
82 Exit();
85 CSingleExit::CSingleExit(const CCriticalSection& cs)
86 : m_cs(const_cast<CCriticalSection&>(cs))
87 , m_count(0)
89 Exit();
92 CSingleExit::~CSingleExit()
94 Restore();
97 void CSingleExit::Exit()
99 if(m_count == 0)
100 m_count = ::ExitCriticalSection(m_cs);
103 void CSingleExit::Restore()
105 if(m_count)
107 RestoreCriticalSection(m_cs, m_count);
108 m_count = 0;