e29578c728a1b5b0abf5288b5898615433fb6498
[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 #if defined(__WINDOWS__)
52 (void *)retVal; //"unreferenced local variable" warning
53 #endif
54 }
55
56 static void *ThreadHandler(CThread *thread)
57 {
58 void *retVal = NULL;
59
60 if (thread)
61 {
62 {
63 CLockObject lock(thread->m_threadMutex);
64 thread->m_bRunning = true;
65 thread->m_bStopped = false;
66 thread->m_threadCondition.Broadcast();
67 }
68
69 retVal = thread->Process();
70
71 {
72 CLockObject lock(thread->m_threadMutex);
73 thread->m_bRunning = false;
74 thread->m_bStopped = true;
75 thread->m_threadCondition.Broadcast();
76 }
77 }
78
79 return retVal;
80 }
81
82 virtual bool IsRunning(void)
83 {
84 CLockObject lock(m_threadMutex);
85 return m_bRunning;
86 }
87
88 virtual bool IsStopped(void)
89 {
90 CLockObject lock(m_threadMutex);
91 return m_bStop;
92 }
93
94 virtual bool CreateThread(bool bWait = true)
95 {
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))))
102 {
103 if (bWait)
104 m_threadCondition.Wait(m_threadMutex, m_bRunning);
105 bReturn = true;
106 }
107 }
108 return bReturn;
109 }
110
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)
116 {
117 bool bReturn(true);
118 bool bRunning(false);
119 {
120 CLockObject lock(m_threadMutex);
121 bRunning = IsRunning();
122 m_bStop = true;
123 }
124
125 if (bRunning && iWaitMs >= 0)
126 {
127 CLockObject lock(m_threadMutex);
128 bReturn = m_threadCondition.Wait(m_threadMutex, m_bStopped, iWaitMs);
129 }
130 else
131 {
132 bReturn = true;
133 }
134
135 return bReturn;
136 }
137
138 virtual bool Sleep(uint32_t iTimeout)
139 {
140 CLockObject lock(m_threadMutex);
141 return m_bStop ? false : m_threadCondition.Wait(m_threadMutex, m_bStopped, iTimeout);
142 }
143
144 virtual void *Process(void) = 0;
145
146 protected:
147 void SetRunning(bool bSetTo);
148 CMutex m_threadMutex;
149
150 private:
151 bool m_bStop;
152 bool m_bRunning;
153 bool m_bStopped;
154 CCondition<bool> m_threadCondition;
155 thread_t m_thread;
156 };
157 };