cec: fixed - the wrong setting was being read when powering on devices. added debug...
[deb_libcec.git] / src / lib / platform / threads / threads.h
CommitLineData
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
36namespace 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);
7b6a5b68 51 (void *)retVal; //"unreferenced local variable" warning
f00ff009
LOK
52 }
53
54 static void *ThreadHandler(CThread *thread)
55 {
56 void *retVal = NULL;
57
58 if (thread)
59 {
960f33c6
LOK
60 {
61 CLockObject lock(thread->m_threadMutex);
62 thread->m_bRunning = true;
63 thread->m_bStopped = false;
64 thread->m_threadCondition.Broadcast();
65 }
f00ff009
LOK
66
67 retVal = thread->Process();
68
960f33c6
LOK
69 {
70 CLockObject lock(thread->m_threadMutex);
71 thread->m_bRunning = false;
72 thread->m_bStopped = true;
73 thread->m_threadCondition.Broadcast();
74 }
f00ff009
LOK
75 }
76
77 return retVal;
78 }
79
c12ee8e6 80 virtual bool IsRunning(void)
f00ff009
LOK
81 {
82 CLockObject lock(m_threadMutex);
83 return m_bRunning;
84 }
85
c12ee8e6 86 virtual bool IsStopped(void)
f00ff009
LOK
87 {
88 CLockObject lock(m_threadMutex);
89 return m_bStop;
90 }
91
c12ee8e6 92 virtual bool CreateThread(bool bWait = true)
f00ff009 93 {
ce37a9b1
LOK
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))))
f00ff009 100 {
ce37a9b1
LOK
101 if (bWait)
102 m_threadCondition.Wait(m_threadMutex, m_bRunning);
103 bReturn = true;
f00ff009 104 }
ce37a9b1 105 }
f00ff009
LOK
106 return bReturn;
107 }
108
78d138bd
LOK
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)
f00ff009
LOK
114 {
115 bool bReturn(true);
116 bool bRunning(false);
117 {
118 CLockObject lock(m_threadMutex);
119 bRunning = IsRunning();
120 m_bStop = true;
f00ff009
LOK
121 }
122
78d138bd 123 if (bRunning && iWaitMs >= 0)
f00ff009 124 {
78d138bd
LOK
125 CLockObject lock(m_threadMutex);
126 bReturn = m_threadCondition.Wait(m_threadMutex, m_bStopped, iWaitMs);
127 }
128 else
129 {
130 bReturn = true;
f00ff009 131 }
78d138bd
LOK
132
133 return bReturn;
f00ff009
LOK
134 }
135
c12ee8e6 136 virtual bool Sleep(uint32_t iTimeout)
f00ff009
LOK
137 {
138 CLockObject lock(m_threadMutex);
960f33c6 139 return m_bStop ? false : m_threadCondition.Wait(m_threadMutex, m_bStopped, iTimeout);
f00ff009
LOK
140 }
141
142 virtual void *Process(void) = 0;
143
144 protected:
145 void SetRunning(bool bSetTo);
0b714871 146 CMutex m_threadMutex;
f00ff009
LOK
147
148 private:
8f6e48cd
LOK
149 bool m_bStop;
150 bool m_bRunning;
151 bool m_bStopped;
152 CCondition<bool> m_threadCondition;
8f6e48cd 153 thread_t m_thread;
f00ff009
LOK
154 };
155};