cec: handle CEC_OPCODE_ACTIVE_SOURCE
[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"
35
b9187cc6
LOK
36using namespace CEC;
37
abbca718
LOK
38CMutex::CMutex(void)
39{
40 pthread_mutex_init(&m_mutex, NULL);
abbca718
LOK
41}
42
43CMutex::~CMutex(void)
44{
abbca718
LOK
45 pthread_mutex_destroy(&m_mutex);
46}
47
60fa4578 48bool CMutex::TryLock(void)
abbca718 49{
60fa4578 50 return (pthread_mutex_trylock(&m_mutex) == 0);
abbca718
LOK
51}
52
53bool CMutex::Lock(void)
54{
60fa4578 55 return (pthread_mutex_lock(&m_mutex) == 0);
abbca718
LOK
56}
57
58void CMutex::Unlock(void)
59{
60 pthread_mutex_unlock(&m_mutex);
abbca718
LOK
61}
62
13fd6a66 63CLockObject::CLockObject(CMutex *mutex, bool bTryLock /* = false */) :
60fa4578 64 m_mutex(mutex)
abbca718 65{
5559c36e 66 if (m_mutex)
13fd6a66 67 m_bLocked = bTryLock ? m_mutex->TryLock() : m_mutex->Lock();
abbca718
LOK
68}
69
70CLockObject::~CLockObject(void)
a8f0bd18
LOK
71{
72 Leave();
73 m_mutex = NULL;
74}
75
76void CLockObject::Leave(void)
abbca718 77{
13fd6a66
LOK
78 if (m_mutex && m_bLocked)
79 {
80 m_bLocked = false;
5559c36e 81 m_mutex->Unlock();
13fd6a66 82 }
a8f0bd18
LOK
83}
84
85void CLockObject::Lock(void)
86{
5559c36e 87 if (m_mutex)
13fd6a66 88 m_bLocked = m_mutex->Lock();
abbca718
LOK
89}
90
91CCondition::CCondition(void)
92{
93 pthread_cond_init(&m_cond, NULL);
abbca718
LOK
94}
95
96CCondition::~CCondition(void)
97{
98 pthread_cond_broadcast(&m_cond);
99 pthread_cond_destroy(&m_cond);
100}
101
5559c36e 102void CCondition::Broadcast(void)
abbca718
LOK
103{
104 pthread_cond_broadcast(&m_cond);
105}
106
5559c36e
LOK
107void CCondition::Signal(void)
108{
109 pthread_cond_signal(&m_cond);
110}
111
13fd6a66 112bool CCondition::Wait(CMutex *mutex, uint32_t iTimeout /* = 0 */)
abbca718 113{
60fa4578 114 bool bReturn(false);
5559c36e
LOK
115 sched_yield();
116 if (mutex)
abbca718 117 {
13fd6a66
LOK
118 if (iTimeout > 0)
119 {
120 struct timespec abstime;
121 struct timeval now;
122 gettimeofday(&now, NULL);
123 iTimeout += now.tv_usec / 1000;
124 abstime.tv_sec = now.tv_sec + (time_t)(iTimeout / 1000);
88c05b08 125 abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
13fd6a66
LOK
126 bReturn = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
127 }
128 else
129 {
130 bReturn = (pthread_cond_wait(&m_cond, &mutex->m_mutex) == 0);
131 }
abbca718
LOK
132 }
133
abbca718
LOK
134 return bReturn;
135}
136
25701fa6 137void CCondition::Sleep(uint32_t iTimeout)
abbca718 138{
abbca718
LOK
139 CCondition w;
140 CMutex m;
60fa4578 141 CLockObject lock(&m);
5559c36e 142 w.Wait(&m, iTimeout);
abbca718 143}
60fa4578
LOK
144
145CThread::CThread(void) :
13fd6a66
LOK
146 m_bStop(false),
147 m_bRunning(false)
60fa4578
LOK
148{
149}
150
151CThread::~CThread(void)
152{
25701fa6 153 StopThread();
60fa4578
LOK
154}
155
13fd6a66 156bool CThread::CreateThread(bool bWait /* = true */)
60fa4578
LOK
157{
158 bool bReturn(false);
159
5559c36e
LOK
160 CLockObject lock(&m_threadMutex);
161 m_bStop = false;
60fa4578
LOK
162 if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
163 {
13fd6a66
LOK
164 if (bWait)
165 m_threadCondition.Wait(&m_threadMutex);
60fa4578
LOK
166 bReturn = true;
167 }
168
169 return bReturn;
170}
171
172void *CThread::ThreadHandler(CThread *thread)
173{
5559c36e
LOK
174 void *retVal = NULL;
175
60fa4578 176 if (thread)
13fd6a66 177 {
b5ed9b9e 178 CLockObject lock(&thread->m_threadMutex);
13fd6a66 179 thread->m_bRunning = true;
b5ed9b9e 180 lock.Leave();
13fd6a66 181 thread->m_threadCondition.Broadcast();
b5ed9b9e 182
5559c36e 183 retVal = thread->Process();
b5ed9b9e
LOK
184
185 lock.Lock();
13fd6a66 186 thread->m_bRunning = false;
b5ed9b9e
LOK
187 lock.Leave();
188 thread->m_threadCondition.Broadcast();
13fd6a66 189 }
5559c36e
LOK
190
191 return retVal;
60fa4578
LOK
192}
193
5559c36e 194bool CThread::StopThread(bool bWaitForExit /* = true */)
60fa4578 195{
13fd6a66 196 bool bReturn(true);
60fa4578 197 m_bStop = true;
5559c36e
LOK
198
199 m_threadCondition.Broadcast();
25701fa6 200
13fd6a66
LOK
201 void *retVal;
202 if (bWaitForExit && m_bRunning)
203 bReturn = (pthread_join(m_thread, &retVal) == 0);
5559c36e
LOK
204
205 return bReturn;
206}
207
25701fa6 208bool CThread::Sleep(uint32_t iTimeout)
5559c36e
LOK
209{
210 CLockObject lock(&m_threadMutex);
13fd6a66 211 return m_bStop ? false : m_threadCondition.Wait(&m_threadMutex, iTimeout);
60fa4578 212}