1d14f8cea32560befc14f24267df7e07a55bfbb5
[deb_libcec.git] / src / lib / platform / threads / threads.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved.
6 * libCEC(R) is an original work, containing original code.
7 *
8 * libCEC(R) is a trademark of Pulse-Eight Limited.
9 *
10 * This program is dual-licensed; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 *
24 *
25 * Alternatively, you can license this library under a commercial license,
26 * please contact Pulse-Eight Licensing for more information.
27 *
28 * For more information contact:
29 * Pulse-Eight Licensing <license@pulse-eight.com>
30 * http://www.pulse-eight.com/
31 * http://www.pulse-eight.net/
32 */
33
34 #include "mutex.h"
35
36 namespace PLATFORM
37 {
38 class CThread
39 {
40 public:
41 CThread(void) :
42 m_bStop(false),
43 m_bRunning(false),
44 m_bStopped(false) {}
45
46 virtual ~CThread(void)
47 {
48 StopThread(0);
49 void *retVal;
50 ThreadsWait(m_thread, &retVal);
51 }
52
53 static void *ThreadHandler(CThread *thread)
54 {
55 void *retVal = NULL;
56
57 if (thread)
58 {
59 {
60 CLockObject lock(thread->m_threadMutex);
61 thread->m_bRunning = true;
62 thread->m_bStopped = false;
63 thread->m_threadCondition.Broadcast();
64 }
65
66 retVal = thread->Process();
67
68 {
69 CLockObject lock(thread->m_threadMutex);
70 thread->m_bRunning = false;
71 thread->m_bStopped = true;
72 thread->m_threadCondition.Broadcast();
73 }
74 }
75
76 return retVal;
77 }
78
79 virtual bool IsRunning(void)
80 {
81 CLockObject lock(m_threadMutex);
82 return m_bRunning;
83 }
84
85 virtual bool IsStopped(void)
86 {
87 CLockObject lock(m_threadMutex);
88 return m_bStop;
89 }
90
91 virtual bool CreateThread(bool bWait = true)
92 {
93 bool bReturn(false);
94 CLockObject lock(m_threadMutex);
95 if (!IsRunning())
96 {
97 m_bStop = false;
98 if (ThreadsCreate(m_thread, CThread::ThreadHandler, ((void*)static_cast<CThread *>(this))))
99 {
100 if (bWait)
101 m_threadCondition.Wait(m_threadMutex, m_bRunning);
102 bReturn = true;
103 }
104 }
105 return bReturn;
106 }
107
108 /*!
109 * @brief Stop the thread
110 * @param iWaitMs negative = don't wait, 0 = infinite, or the amount of ms to wait
111 */
112 virtual bool StopThread(int iWaitMs = 5000)
113 {
114 bool bReturn(true);
115 bool bRunning(false);
116 {
117 CLockObject lock(m_threadMutex);
118 bRunning = IsRunning();
119 m_bStop = true;
120 }
121
122 if (bRunning && iWaitMs >= 0)
123 {
124 CLockObject lock(m_threadMutex);
125 bReturn = m_threadCondition.Wait(m_threadMutex, m_bStopped, iWaitMs);
126 }
127 else
128 {
129 bReturn = true;
130 }
131
132 return bReturn;
133 }
134
135 virtual bool Sleep(uint32_t iTimeout)
136 {
137 CLockObject lock(m_threadMutex);
138 return m_bStop ? false : m_threadCondition.Wait(m_threadMutex, m_bStopped, iTimeout);
139 }
140
141 virtual void *Process(void) = 0;
142
143 protected:
144 void SetRunning(bool bSetTo);
145
146 private:
147 bool m_bStop;
148 bool m_bRunning;
149 bool m_bStopped;
150 CCondition<bool> m_threadCondition;
151 CMutex m_threadMutex;
152 thread_t m_thread;
153 };
154 };