cec: stick some interfaces before the pthread stuff, so we can add other implementati...
[deb_libcec.git] / src / lib / platform / threads.cpp
CommitLineData
abbca718
LOK
1/*
2 * This file is part of the libCEC(R) library.
3 *
4 * libCEC(R) is Copyright (C) 2011 Pulse-Eight Limited. All rights reserved.
5 * libCEC(R) is an original work, containing original code.
6 *
7 * libCEC(R) is a trademark of Pulse-Eight Limited.
8 *
9 * This program is dual-licensed; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 *
23 *
24 * Alternatively, you can license this library under a commercial license,
25 * please contact Pulse-Eight Licensing for more information.
26 *
27 * For more information contact:
28 * Pulse-Eight Licensing <license@pulse-eight.com>
29 * http://www.pulse-eight.com/
30 * http://www.pulse-eight.net/
31 */
32
33#include "threads.h"
34#include "timeutils.h"
6f14b512 35#include "os-dependent.h"
abbca718 36
b9187cc6
LOK
37using namespace CEC;
38
6f14b512 39CLockObject::CLockObject(IMutex *mutex, bool bTryLock /* = false */) :
60fa4578 40 m_mutex(mutex)
abbca718 41{
5559c36e 42 if (m_mutex)
13fd6a66 43 m_bLocked = bTryLock ? m_mutex->TryLock() : m_mutex->Lock();
abbca718
LOK
44}
45
46CLockObject::~CLockObject(void)
a8f0bd18
LOK
47{
48 Leave();
49 m_mutex = NULL;
50}
51
52void CLockObject::Leave(void)
abbca718 53{
13fd6a66
LOK
54 if (m_mutex && m_bLocked)
55 {
56 m_bLocked = false;
5559c36e 57 m_mutex->Unlock();
13fd6a66 58 }
a8f0bd18
LOK
59}
60
61void CLockObject::Lock(void)
62{
5559c36e 63 if (m_mutex)
13fd6a66 64 m_bLocked = m_mutex->Lock();
abbca718
LOK
65}
66
6f14b512 67void ICondition::Sleep(uint32_t iTimeout)
abbca718 68{
abbca718
LOK
69 CCondition w;
70 CMutex m;
60fa4578 71 CLockObject lock(&m);
5559c36e 72 w.Wait(&m, iTimeout);
abbca718 73}
60fa4578 74
6f14b512 75IThread::IThread(void) :
13fd6a66
LOK
76 m_bStop(false),
77 m_bRunning(false)
60fa4578 78{
6f14b512
LOK
79 m_threadCondition = new CCondition();
80 m_threadMutex = new CMutex();
60fa4578
LOK
81}
82
6f14b512 83IThread::~IThread(void)
60fa4578 84{
25701fa6 85 StopThread();
6f14b512
LOK
86 delete m_threadCondition;
87 delete m_threadMutex;
60fa4578
LOK
88}
89
6f14b512 90bool IThread::StopThread(bool bWaitForExit /* = true */)
60fa4578
LOK
91{
92 m_bStop = true;
6f14b512
LOK
93 m_threadCondition->Broadcast();
94 bWaitForExit = true;
5559c36e 95
6f14b512 96 return false;
5559c36e
LOK
97}
98
6f14b512 99bool IThread::Sleep(uint32_t iTimeout)
5559c36e 100{
6f14b512
LOK
101 CLockObject lock(m_threadMutex);
102 return m_bStop ? false : m_threadCondition->Wait(m_threadMutex, iTimeout);
60fa4578 103}