b5ccf3850815845b1d37e458ab163fcd78e62f93
[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 (void *)retVal; //"unreferenced local variable" warning
52 }
53
54 static void *ThreadHandler(CThread *thread)
55 {
56 void *retVal = NULL;
57
58 if (thread)
59 {
60 {
61 CLockObject lock(thread->m_threadMutex);
62 thread->m_bRunning = true;
63 thread->m_bStopped = false;
64 thread->m_threadCondition.Broadcast();
65 }
66
67 retVal = thread->Process();
68
69 {
70 CLockObject lock(thread->m_threadMutex);
71 thread->m_bRunning = false;
72 thread->m_bStopped = true;
73 thread->m_threadCondition.Broadcast();
74 }
75 }
76
77 return retVal;
78 }
79
80 virtual bool IsRunning(void)
81 {
82 CLockObject lock(m_threadMutex);
83 return m_bRunning;
84 }
85
86 virtual bool IsStopped(void)
87 {
88 CLockObject lock(m_threadMutex);
89 return m_bStop;
90 }
91
92 virtual bool CreateThread(bool bWait = true)
93 {
94 bool bReturn(false);
95 CLockObject lock(m_threadMutex);
96 if (!IsRunning())
97 {
98 m_bStop = false;
99 if (ThreadsCreate(m_thread, CThread::ThreadHandler, ((void*)static_cast<CThread *>(this))))
100 {
101 if (bWait)
102 m_threadCondition.Wait(m_threadMutex, m_bRunning);
103 bReturn = true;
104 }
105 }
106 return bReturn;
107 }
108
109 /*!
110 * @brief Stop the thread
111 * @param iWaitMs negative = don't wait, 0 = infinite, or the amount of ms to wait
112 */
113 virtual bool StopThread(int iWaitMs = 5000)
114 {
115 bool bReturn(true);
116 bool bRunning(false);
117 {
118 CLockObject lock(m_threadMutex);
119 bRunning = IsRunning();
120 m_bStop = true;
121 }
122
123 if (bRunning && iWaitMs >= 0)
124 {
125 CLockObject lock(m_threadMutex);
126 bReturn = m_threadCondition.Wait(m_threadMutex, m_bStopped, iWaitMs);
127 }
128 else
129 {
130 bReturn = true;
131 }
132
133 return bReturn;
134 }
135
136 virtual bool Sleep(uint32_t iTimeout)
137 {
138 CLockObject lock(m_threadMutex);
139 return m_bStop ? false : m_threadCondition.Wait(m_threadMutex, m_bStopped, iTimeout);
140 }
141
142 virtual void *Process(void) = 0;
143
144 protected:
145 void SetRunning(bool bSetTo);
146
147 private:
148 bool m_bStop;
149 bool m_bRunning;
150 bool m_bStopped;
151 CCondition<bool> m_threadCondition;
152 CMutex m_threadMutex;
153 thread_t m_thread;
154 };
155 };