d168e906a671ddc8e0caab582c9ae30a58bd2853
[deb_libcec.git] / src / lib / platform / threads.cpp
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
36 using namespace CEC;
37
38 CMutex::CMutex(bool bRecursive /* = true */)
39 {
40 pthread_mutex_init(&m_mutex, bRecursive ? GetMutexAttribute() : NULL);
41 }
42
43 CMutex::~CMutex(void)
44 {
45 pthread_mutex_destroy(&m_mutex);
46 }
47
48 bool CMutex::TryLock(void)
49 {
50 return (pthread_mutex_trylock(&m_mutex) == 0);
51 }
52
53 bool CMutex::Lock(void)
54 {
55 return (pthread_mutex_lock(&m_mutex) == 0);
56 }
57
58 void CMutex::Unlock(void)
59 {
60 pthread_mutex_unlock(&m_mutex);
61 }
62
63 static pthread_mutexattr_t g_mutexAttr;
64 pthread_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
76 CLockObject::CLockObject(CMutex *mutex, bool bTryLock /* = false */) :
77 m_mutex(mutex)
78 {
79 if (m_mutex)
80 m_bLocked = bTryLock ? m_mutex->TryLock() : m_mutex->Lock();
81 }
82
83 CLockObject::~CLockObject(void)
84 {
85 Leave();
86 m_mutex = NULL;
87 }
88
89 void CLockObject::Leave(void)
90 {
91 if (m_mutex && m_bLocked)
92 {
93 m_bLocked = false;
94 m_mutex->Unlock();
95 }
96 }
97
98 void CLockObject::Lock(void)
99 {
100 if (m_mutex)
101 m_bLocked = m_mutex->Lock();
102 }
103
104 CCondition::CCondition(void)
105 {
106 pthread_cond_init(&m_cond, NULL);
107 }
108
109 CCondition::~CCondition(void)
110 {
111 pthread_cond_broadcast(&m_cond);
112 pthread_cond_destroy(&m_cond);
113 }
114
115 void CCondition::Broadcast(void)
116 {
117 pthread_cond_broadcast(&m_cond);
118 }
119
120 void CCondition::Signal(void)
121 {
122 pthread_cond_signal(&m_cond);
123 }
124
125 bool CCondition::Wait(CMutex *mutex, uint32_t iTimeout /* = 0 */)
126 {
127 bool bReturn(false);
128 sched_yield();
129 if (mutex)
130 {
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);
138 abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
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 }
145 }
146
147 return bReturn;
148 }
149
150 void CCondition::Sleep(uint32_t iTimeout)
151 {
152 CCondition w;
153 CMutex m;
154 CLockObject lock(&m);
155 w.Wait(&m, iTimeout);
156 }
157
158 CThread::CThread(void) :
159 m_bStop(false),
160 m_bRunning(false)
161 {
162 }
163
164 CThread::~CThread(void)
165 {
166 StopThread();
167 }
168
169 bool CThread::CreateThread(bool bWait /* = true */)
170 {
171 bool bReturn(false);
172
173 CLockObject lock(&m_threadMutex);
174 m_bStop = false;
175 if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
176 {
177 if (bWait)
178 m_threadCondition.Wait(&m_threadMutex);
179 bReturn = true;
180 }
181
182 return bReturn;
183 }
184
185 void *CThread::ThreadHandler(CThread *thread)
186 {
187 void *retVal = NULL;
188
189 if (thread)
190 {
191 CLockObject lock(&thread->m_threadMutex);
192 thread->m_bRunning = true;
193 lock.Leave();
194 thread->m_threadCondition.Broadcast();
195
196 retVal = thread->Process();
197
198 lock.Lock();
199 thread->m_bRunning = false;
200 lock.Leave();
201 thread->m_threadCondition.Broadcast();
202 }
203
204 return retVal;
205 }
206
207 bool CThread::StopThread(bool bWaitForExit /* = true */)
208 {
209 bool bReturn(true);
210 m_bStop = true;
211
212 m_threadCondition.Broadcast();
213
214 void *retVal;
215 if (bWaitForExit && m_bRunning)
216 bReturn = (pthread_join(m_thread, &retVal) == 0);
217
218 return bReturn;
219 }
220
221 bool CThread::Sleep(uint32_t iTimeout)
222 {
223 CLockObject lock(&m_threadMutex);
224 return m_bStop ? false : m_threadCondition.Wait(&m_threadMutex, iTimeout);
225 }