cec: stick some interfaces before the pthread stuff, so we can add other implementati...
[deb_libcec.git] / src / lib / platform / threads.cpp
... / ...
CommitLineData
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"
35#include "os-dependent.h"
36
37using namespace CEC;
38
39CLockObject::CLockObject(IMutex *mutex, bool bTryLock /* = false */) :
40 m_mutex(mutex)
41{
42 if (m_mutex)
43 m_bLocked = bTryLock ? m_mutex->TryLock() : m_mutex->Lock();
44}
45
46CLockObject::~CLockObject(void)
47{
48 Leave();
49 m_mutex = NULL;
50}
51
52void CLockObject::Leave(void)
53{
54 if (m_mutex && m_bLocked)
55 {
56 m_bLocked = false;
57 m_mutex->Unlock();
58 }
59}
60
61void CLockObject::Lock(void)
62{
63 if (m_mutex)
64 m_bLocked = m_mutex->Lock();
65}
66
67void ICondition::Sleep(uint32_t iTimeout)
68{
69 CCondition w;
70 CMutex m;
71 CLockObject lock(&m);
72 w.Wait(&m, iTimeout);
73}
74
75IThread::IThread(void) :
76 m_bStop(false),
77 m_bRunning(false)
78{
79 m_threadCondition = new CCondition();
80 m_threadMutex = new CMutex();
81}
82
83IThread::~IThread(void)
84{
85 StopThread();
86 delete m_threadCondition;
87 delete m_threadMutex;
88}
89
90bool IThread::StopThread(bool bWaitForExit /* = true */)
91{
92 m_bStop = true;
93 m_threadCondition->Broadcast();
94 bWaitForExit = true;
95
96 return false;
97}
98
99bool IThread::Sleep(uint32_t iTimeout)
100{
101 CLockObject lock(m_threadMutex);
102 return m_bStop ? false : m_threadCondition->Wait(m_threadMutex, iTimeout);
103}