2c3c2c540de03a369a3332a4e7e0e8a30309fc6d
[deb_libcec.git] / src / lib / platform / threads.h
1 #pragma once
2 /*
3 * This file is part of the libCEC(R) library.
4 *
5 * libCEC(R) is Copyright (C) 2011 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 <stdint.h>
35
36 namespace CEC
37 {
38 class IMutex
39 {
40 public:
41 IMutex(bool bRecursive = true) { m_bRecursive = bRecursive ; };
42 virtual ~IMutex(void) {};
43
44 virtual bool TryLock(void) = 0;
45 virtual bool Lock(void) = 0;
46 virtual void Unlock(void) = 0;
47
48 protected:
49 bool m_bRecursive;
50 };
51
52 class ICondition
53 {
54 public:
55 virtual void Broadcast(void) = 0;
56 virtual void Signal(void) = 0;
57 virtual bool Wait(IMutex *mutex, uint32_t iTimeout = 0) = 0;
58
59 static void Sleep(uint32_t iTimeout);
60 };
61
62 class IThread
63 {
64 public:
65 IThread(void);
66 virtual ~IThread(void);
67
68 virtual bool IsRunning(void) const { return m_bRunning; };
69 virtual bool CreateThread(bool bWait = true) = 0;
70 virtual bool IsStopped(void) const { return m_bStop; };
71
72 virtual bool StopThread(bool bWaitForExit = true);
73 virtual bool Sleep(uint32_t iTimeout);
74
75 virtual void *Process(void) = 0;
76
77 protected:
78 bool m_bStop;
79 bool m_bRunning;
80 ICondition *m_threadCondition;
81 IMutex *m_threadMutex;
82 };
83
84 class CLockObject
85 {
86 public:
87 CLockObject(IMutex *mutex, bool bTryLock = false);
88 ~CLockObject(void);
89
90 bool IsLocked(void) const { return m_bLocked; }
91 void Leave(void);
92 void Lock(void);
93
94 private:
95 IMutex *m_mutex;
96 bool m_bLocked;
97 };
98 };