Commit | Line | Data |
---|---|---|
f00ff009 LOK |
1 | #pragma once |
2 | /* | |
3 | * This file is part of the libCEC(R) library. | |
4 | * | |
b492c10e | 5 | * libCEC(R) is Copyright (C) 2011-2012 Pulse-Eight Limited. All rights reserved. |
f00ff009 LOK |
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), | |
960f33c6 LOK |
43 | m_bRunning(false), |
44 | m_bStopped(false) {} | |
f00ff009 LOK |
45 | |
46 | virtual ~CThread(void) | |
47 | { | |
002fa4ea | 48 | StopThread(0); |
4f3d2262 LOK |
49 | void *retVal; |
50 | ThreadsWait(m_thread, &retVal); | |
065a893a | 51 | #if defined(__WINDOWS__) |
7b6a5b68 | 52 | (void *)retVal; //"unreferenced local variable" warning |
065a893a | 53 | #endif |
f00ff009 LOK |
54 | } |
55 | ||
56 | static void *ThreadHandler(CThread *thread) | |
57 | { | |
58 | void *retVal = NULL; | |
59 | ||
60 | if (thread) | |
61 | { | |
960f33c6 LOK |
62 | { |
63 | CLockObject lock(thread->m_threadMutex); | |
64 | thread->m_bRunning = true; | |
65 | thread->m_bStopped = false; | |
66 | thread->m_threadCondition.Broadcast(); | |
67 | } | |
f00ff009 LOK |
68 | |
69 | retVal = thread->Process(); | |
70 | ||
960f33c6 LOK |
71 | { |
72 | CLockObject lock(thread->m_threadMutex); | |
73 | thread->m_bRunning = false; | |
74 | thread->m_bStopped = true; | |
75 | thread->m_threadCondition.Broadcast(); | |
76 | } | |
f00ff009 LOK |
77 | } |
78 | ||
79 | return retVal; | |
80 | } | |
81 | ||
c12ee8e6 | 82 | virtual bool IsRunning(void) |
f00ff009 LOK |
83 | { |
84 | CLockObject lock(m_threadMutex); | |
85 | return m_bRunning; | |
86 | } | |
87 | ||
c12ee8e6 | 88 | virtual bool IsStopped(void) |
f00ff009 LOK |
89 | { |
90 | CLockObject lock(m_threadMutex); | |
91 | return m_bStop; | |
92 | } | |
93 | ||
c12ee8e6 | 94 | virtual bool CreateThread(bool bWait = true) |
f00ff009 | 95 | { |
ce37a9b1 LOK |
96 | bool bReturn(false); |
97 | CLockObject lock(m_threadMutex); | |
98 | if (!IsRunning()) | |
99 | { | |
100 | m_bStop = false; | |
101 | if (ThreadsCreate(m_thread, CThread::ThreadHandler, ((void*)static_cast<CThread *>(this)))) | |
f00ff009 | 102 | { |
ce37a9b1 LOK |
103 | if (bWait) |
104 | m_threadCondition.Wait(m_threadMutex, m_bRunning); | |
105 | bReturn = true; | |
f00ff009 | 106 | } |
ce37a9b1 | 107 | } |
f00ff009 LOK |
108 | return bReturn; |
109 | } | |
110 | ||
78d138bd LOK |
111 | /*! |
112 | * @brief Stop the thread | |
113 | * @param iWaitMs negative = don't wait, 0 = infinite, or the amount of ms to wait | |
114 | */ | |
115 | virtual bool StopThread(int iWaitMs = 5000) | |
f00ff009 LOK |
116 | { |
117 | bool bReturn(true); | |
118 | bool bRunning(false); | |
119 | { | |
120 | CLockObject lock(m_threadMutex); | |
121 | bRunning = IsRunning(); | |
122 | m_bStop = true; | |
f00ff009 LOK |
123 | } |
124 | ||
78d138bd | 125 | if (bRunning && iWaitMs >= 0) |
f00ff009 | 126 | { |
78d138bd LOK |
127 | CLockObject lock(m_threadMutex); |
128 | bReturn = m_threadCondition.Wait(m_threadMutex, m_bStopped, iWaitMs); | |
129 | } | |
130 | else | |
131 | { | |
132 | bReturn = true; | |
f00ff009 | 133 | } |
78d138bd LOK |
134 | |
135 | return bReturn; | |
f00ff009 LOK |
136 | } |
137 | ||
c12ee8e6 | 138 | virtual bool Sleep(uint32_t iTimeout) |
f00ff009 LOK |
139 | { |
140 | CLockObject lock(m_threadMutex); | |
960f33c6 | 141 | return m_bStop ? false : m_threadCondition.Wait(m_threadMutex, m_bStopped, iTimeout); |
f00ff009 LOK |
142 | } |
143 | ||
144 | virtual void *Process(void) = 0; | |
145 | ||
146 | protected: | |
147 | void SetRunning(bool bSetTo); | |
0b714871 | 148 | CMutex m_threadMutex; |
f00ff009 LOK |
149 | |
150 | private: | |
8f6e48cd LOK |
151 | bool m_bStop; |
152 | bool m_bRunning; | |
153 | bool m_bStopped; | |
154 | CCondition<bool> m_threadCondition; | |
8f6e48cd | 155 | thread_t m_thread; |
f00ff009 LOK |
156 | }; |
157 | }; |