Merge pull request #16 from finson/65894f58514a9ab61ae20e45cfde06c9d45600e4
[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
f52ac1fb 38CMutex::CMutex(bool bRecursive /* = true */)
abbca718 39{
f52ac1fb 40 pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : 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
3154de8d
LOK
63static pthread_mutexattr_t g_mutexAttr;
64pthread_mutexattr_t *CMutex::GetMutexAttribute()
65{
66 static bool bAttributeInitialised = false;
67 if (!bAttributeInitialised)
68 {
69 pthread_mutexattr_init(&g_mutexAttr);
70 pthread_mutexattr_settype(&g_mutexAttr, PTHREAD_MUTEX_RECURSIVE);
71 bAttributeInitialised = true;
72 }
73 return &g_mutexAttr;
74}
75
13fd6a66 76CLockObject::CLockObject(CMutex *mutex, bool bTryLock /* = false */) :
60fa4578 77 m_mutex(mutex)
abbca718 78{
5559c36e 79 if (m_mutex)
13fd6a66 80 m_bLocked = bTryLock ? m_mutex->TryLock() : m_mutex->Lock();
abbca718
LOK
81}
82
83CLockObject::~CLockObject(void)
a8f0bd18
LOK
84{
85 Leave();
86 m_mutex = NULL;
87}
88
89void CLockObject::Leave(void)
abbca718 90{
13fd6a66
LOK
91 if (m_mutex && m_bLocked)
92 {
93 m_bLocked = false;
5559c36e 94 m_mutex->Unlock();
13fd6a66 95 }
a8f0bd18
LOK
96}
97
98void CLockObject::Lock(void)
99{
5559c36e 100 if (m_mutex)
13fd6a66 101 m_bLocked = m_mutex->Lock();
abbca718
LOK
102}
103
104CCondition::CCondition(void)
105{
106 pthread_cond_init(&m_cond, NULL);
abbca718
LOK
107}
108
109CCondition::~CCondition(void)
110{
111 pthread_cond_broadcast(&m_cond);
112 pthread_cond_destroy(&m_cond);
113}
114
5559c36e 115void CCondition::Broadcast(void)
abbca718
LOK
116{
117 pthread_cond_broadcast(&m_cond);
118}
119
5559c36e
LOK
120void CCondition::Signal(void)
121{
122 pthread_cond_signal(&m_cond);
123}
124
13fd6a66 125bool CCondition::Wait(CMutex *mutex, uint32_t iTimeout /* = 0 */)
abbca718 126{
60fa4578 127 bool bReturn(false);
5559c36e
LOK
128 sched_yield();
129 if (mutex)
abbca718 130 {
13fd6a66
LOK
131 if (iTimeout > 0)
132 {
133 struct timespec abstime;
134 struct timeval now;
135 gettimeofday(&now, NULL);
136 iTimeout += now.tv_usec / 1000;
137 abstime.tv_sec = now.tv_sec + (time_t)(iTimeout / 1000);
88c05b08 138 abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
13fd6a66
LOK
139 bReturn = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
140 }
141 else
142 {
143 bReturn = (pthread_cond_wait(&m_cond, &mutex->m_mutex) == 0);
144 }
abbca718
LOK
145 }
146
abbca718
LOK
147 return bReturn;
148}
149
25701fa6 150void CCondition::Sleep(uint32_t iTimeout)
abbca718 151{
abbca718
LOK
152 CCondition w;
153 CMutex m;
60fa4578 154 CLockObject lock(&m);
5559c36e 155 w.Wait(&m, iTimeout);
abbca718 156}
60fa4578
LOK
157
158CThread::CThread(void) :
13fd6a66
LOK
159 m_bStop(false),
160 m_bRunning(false)
60fa4578
LOK
161{
162}
163
164CThread::~CThread(void)
165{
25701fa6 166 StopThread();
60fa4578
LOK
167}
168
13fd6a66 169bool CThread::CreateThread(bool bWait /* = true */)
60fa4578
LOK
170{
171 bool bReturn(false);
172
5559c36e
LOK
173 CLockObject lock(&m_threadMutex);
174 m_bStop = false;
60fa4578
LOK
175 if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
176 {
13fd6a66
LOK
177 if (bWait)
178 m_threadCondition.Wait(&m_threadMutex);
60fa4578
LOK
179 bReturn = true;
180 }
181
182 return bReturn;
183}
184
185void *CThread::ThreadHandler(CThread *thread)
186{
5559c36e
LOK
187 void *retVal = NULL;
188
60fa4578 189 if (thread)
13fd6a66 190 {
b5ed9b9e 191 CLockObject lock(&thread->m_threadMutex);
13fd6a66 192 thread->m_bRunning = true;
b5ed9b9e 193 lock.Leave();
13fd6a66 194 thread->m_threadCondition.Broadcast();
b5ed9b9e 195
5559c36e 196 retVal = thread->Process();
b5ed9b9e
LOK
197
198 lock.Lock();
13fd6a66 199 thread->m_bRunning = false;
b5ed9b9e
LOK
200 lock.Leave();
201 thread->m_threadCondition.Broadcast();
13fd6a66 202 }
5559c36e
LOK
203
204 return retVal;
60fa4578
LOK
205}
206
5559c36e 207bool CThread::StopThread(bool bWaitForExit /* = true */)
60fa4578 208{
13fd6a66 209 bool bReturn(true);
60fa4578 210 m_bStop = true;
5559c36e
LOK
211
212 m_threadCondition.Broadcast();
25701fa6 213
13fd6a66
LOK
214 void *retVal;
215 if (bWaitForExit && m_bRunning)
216 bReturn = (pthread_join(m_thread, &retVal) == 0);
5559c36e
LOK
217
218 return bReturn;
219}
220
25701fa6 221bool CThread::Sleep(uint32_t iTimeout)
5559c36e
LOK
222{
223 CLockObject lock(&m_threadMutex);
13fd6a66 224 return m_bStop ? false : m_threadCondition.Wait(&m_threadMutex, iTimeout);
60fa4578 225}