libcec v0.5 (WIP)
[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(void)
39 {
40 pthread_mutex_init(&m_mutex, 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 CLockObject::CLockObject(CMutex *mutex) :
64 m_mutex(mutex)
65 {
66 if (m_mutex)
67 m_mutex->Lock();
68 }
69
70 CLockObject::~CLockObject(void)
71 {
72 Leave();
73 m_mutex = NULL;
74 }
75
76 void CLockObject::Leave(void)
77 {
78 if (m_mutex)
79 m_mutex->Unlock();
80 }
81
82 void CLockObject::Lock(void)
83 {
84 if (m_mutex)
85 m_mutex->Lock();
86 }
87
88 CCondition::CCondition(void)
89 {
90 pthread_cond_init(&m_cond, NULL);
91 }
92
93 CCondition::~CCondition(void)
94 {
95 pthread_cond_broadcast(&m_cond);
96 pthread_cond_destroy(&m_cond);
97 }
98
99 void CCondition::Broadcast(void)
100 {
101 pthread_cond_broadcast(&m_cond);
102 }
103
104 void CCondition::Signal(void)
105 {
106 pthread_cond_signal(&m_cond);
107 }
108
109 bool CCondition::Wait(CMutex *mutex, uint32_t iTimeout)
110 {
111 bool bReturn(false);
112 sched_yield();
113 if (mutex)
114 {
115 struct timespec abstime;
116 struct timeval now;
117 gettimeofday(&now, NULL);
118 iTimeout += now.tv_usec / 1000;
119 abstime.tv_sec = now.tv_sec + (time_t)(iTimeout / 1000);
120 abstime.tv_nsec = (int32_t)((iTimeout % (uint32_t)1000) * (uint32_t)1000000);
121 bReturn = (pthread_cond_timedwait(&m_cond, &mutex->m_mutex, &abstime) == 0);
122 }
123
124 return bReturn;
125 }
126
127 void CCondition::Sleep(uint32_t iTimeout)
128 {
129 CCondition w;
130 CMutex m;
131 CLockObject lock(&m);
132 w.Wait(&m, iTimeout);
133 }
134
135 CThread::CThread(void) :
136 m_bRunning(false),
137 m_bStop(false)
138 {
139 }
140
141 CThread::~CThread(void)
142 {
143 StopThread();
144 }
145
146 bool CThread::CreateThread(void)
147 {
148 bool bReturn(false);
149
150 CLockObject lock(&m_threadMutex);
151 m_bStop = false;
152 if (!m_bRunning && pthread_create(&m_thread, NULL, (void *(*) (void *))&CThread::ThreadHandler, (void *)this) == 0)
153 {
154 m_bRunning = true;
155 bReturn = true;
156 }
157
158 return bReturn;
159 }
160
161 void *CThread::ThreadHandler(CThread *thread)
162 {
163 void *retVal = NULL;
164
165 if (thread)
166 retVal = thread->Process();
167 thread->m_bRunning = false;
168
169 return retVal;
170 }
171
172 bool CThread::StopThread(bool bWaitForExit /* = true */)
173 {
174 bool bReturn(false);
175 m_bStop = true;
176
177 m_threadCondition.Broadcast();
178 if (m_bRunning)
179 {
180 void *retVal;
181 if (bWaitForExit)
182 bReturn = (pthread_join(m_thread, &retVal) == 0);
183
184 m_bRunning = false;
185 }
186
187 return bReturn;
188 }
189
190 bool CThread::Sleep(uint32_t iTimeout)
191 {
192 CLockObject lock(&m_threadMutex);
193 return m_bStop ? false :m_threadCondition.Wait(&m_threadMutex, iTimeout);
194 }