Mercurial > projects > hoofbaby
comparison deps/Platinum/ThirdParty/Neptune/Source/Core/NptThreads.h @ 0:3425707ddbf6
Initial import (hopefully this mercurial stuff works...)
author | fraserofthenight |
---|---|
date | Mon, 06 Jul 2009 08:06:28 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3425707ddbf6 |
---|---|
1 /***************************************************************** | |
2 | | |
3 | Neptune - Threads | |
4 | | |
5 | Copyright (c) 2002-2008, Axiomatic Systems, LLC. | |
6 | All rights reserved. | |
7 | | |
8 | Redistribution and use in source and binary forms, with or without | |
9 | modification, are permitted provided that the following conditions are met: | |
10 | * Redistributions of source code must retain the above copyright | |
11 | notice, this list of conditions and the following disclaimer. | |
12 | * Redistributions in binary form must reproduce the above copyright | |
13 | notice, this list of conditions and the following disclaimer in the | |
14 | documentation and/or other materials provided with the distribution. | |
15 | * Neither the name of Axiomatic Systems nor the | |
16 | names of its contributors may be used to endorse or promote products | |
17 | derived from this software without specific prior written permission. | |
18 | | |
19 | THIS SOFTWARE IS PROVIDED BY AXIOMATIC SYSTEMS ''AS IS'' AND ANY | |
20 | EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
21 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
22 | DISCLAIMED. IN NO EVENT SHALL AXIOMATIC SYSTEMS BE LIABLE FOR ANY | |
23 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
24 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
25 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
26 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
27 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
28 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
29 | | |
30 ****************************************************************/ | |
31 | |
32 #ifndef _NPT_THREADS_H_ | |
33 #define _NPT_THREADS_H_ | |
34 | |
35 /*---------------------------------------------------------------------- | |
36 | includes | |
37 +---------------------------------------------------------------------*/ | |
38 #include "NptTypes.h" | |
39 #include "NptConstants.h" | |
40 #include "NptInterfaces.h" | |
41 | |
42 /*---------------------------------------------------------------------- | |
43 | error codes | |
44 +---------------------------------------------------------------------*/ | |
45 const int NPT_ERROR_CALLBACK_HANDLER_SHUTDOWN = NPT_ERROR_BASE_THREADS-0; | |
46 const int NPT_ERROR_CALLBACK_NOTHING_PENDING = NPT_ERROR_BASE_THREADS-1; | |
47 | |
48 /*---------------------------------------------------------------------- | |
49 | NPT_MutexInterface | |
50 +---------------------------------------------------------------------*/ | |
51 class NPT_MutexInterface | |
52 { | |
53 public: | |
54 // methods | |
55 virtual ~NPT_MutexInterface() {} | |
56 virtual NPT_Result Lock() = 0; | |
57 virtual NPT_Result Unlock() = 0; | |
58 }; | |
59 | |
60 /*---------------------------------------------------------------------- | |
61 | NPT_Mutex | |
62 +---------------------------------------------------------------------*/ | |
63 class NPT_Mutex : public NPT_MutexInterface | |
64 { | |
65 public: | |
66 // methods | |
67 NPT_Mutex(); | |
68 ~NPT_Mutex() { delete m_Delegate; } | |
69 NPT_Result Lock() { return m_Delegate->Lock(); } | |
70 NPT_Result Unlock() { return m_Delegate->Unlock(); } | |
71 | |
72 private: | |
73 // members | |
74 NPT_MutexInterface* m_Delegate; | |
75 }; | |
76 | |
77 /*---------------------------------------------------------------------- | |
78 | NPT_AutoLock | |
79 +---------------------------------------------------------------------*/ | |
80 class NPT_AutoLock | |
81 { | |
82 public: | |
83 // methods | |
84 NPT_AutoLock(NPT_Mutex &mutex) : m_Mutex(mutex) { | |
85 m_Mutex.Lock(); | |
86 } | |
87 ~NPT_AutoLock() { | |
88 m_Mutex.Unlock(); | |
89 } | |
90 | |
91 private: | |
92 // members | |
93 NPT_Mutex& m_Mutex; | |
94 }; | |
95 | |
96 /*---------------------------------------------------------------------- | |
97 | NPT_Lock | |
98 +---------------------------------------------------------------------*/ | |
99 template <typename T> | |
100 class NPT_Lock : public T, | |
101 public NPT_Mutex | |
102 { | |
103 }; | |
104 | |
105 /*---------------------------------------------------------------------- | |
106 | NPT_SharedVariableInterface | |
107 +---------------------------------------------------------------------*/ | |
108 class NPT_SharedVariableInterface | |
109 { | |
110 public: | |
111 // methods | |
112 virtual ~NPT_SharedVariableInterface() {} | |
113 virtual void SetValue(int value)= 0; | |
114 virtual int GetValue() = 0; | |
115 virtual NPT_Result WaitUntilEquals(int value, NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) = 0; | |
116 virtual NPT_Result WaitWhileEquals(int value, NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) = 0; | |
117 }; | |
118 | |
119 /*---------------------------------------------------------------------- | |
120 | NPT_SharedVariable | |
121 +---------------------------------------------------------------------*/ | |
122 class NPT_SharedVariable : public NPT_SharedVariableInterface | |
123 { | |
124 public: | |
125 // methods | |
126 NPT_SharedVariable(int value = 0); | |
127 ~NPT_SharedVariable() { delete m_Delegate; } | |
128 void SetValue(int value) { | |
129 m_Delegate->SetValue(value); | |
130 } | |
131 int GetValue() { | |
132 return m_Delegate->GetValue(); | |
133 } | |
134 NPT_Result WaitUntilEquals(int value, NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) { | |
135 return m_Delegate->WaitUntilEquals(value, timeout); | |
136 } | |
137 NPT_Result WaitWhileEquals(int value, NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) { | |
138 return m_Delegate->WaitWhileEquals(value, timeout); | |
139 } | |
140 | |
141 private: | |
142 // members | |
143 NPT_SharedVariableInterface* m_Delegate; | |
144 }; | |
145 | |
146 /*---------------------------------------------------------------------- | |
147 | NPT_AtomicVariableInterface | |
148 +---------------------------------------------------------------------*/ | |
149 class NPT_AtomicVariableInterface | |
150 { | |
151 public: | |
152 // methods | |
153 virtual ~NPT_AtomicVariableInterface() {} | |
154 virtual int Increment() = 0; | |
155 virtual int Decrement() = 0; | |
156 virtual int GetValue() = 0; | |
157 virtual void SetValue(int value) = 0; | |
158 }; | |
159 | |
160 /*---------------------------------------------------------------------- | |
161 | NPT_AtomicVariable | |
162 +---------------------------------------------------------------------*/ | |
163 class NPT_AtomicVariable : public NPT_AtomicVariableInterface | |
164 { | |
165 public: | |
166 // methods | |
167 NPT_AtomicVariable(int value = 0); | |
168 ~NPT_AtomicVariable() { delete m_Delegate; } | |
169 int Increment() { return m_Delegate->Increment();} | |
170 int Decrement() { return m_Delegate->Decrement();} | |
171 void SetValue(int value) { m_Delegate->SetValue(value); } | |
172 int GetValue() { return m_Delegate->GetValue(); } | |
173 | |
174 private: | |
175 // members | |
176 NPT_AtomicVariableInterface* m_Delegate; | |
177 }; | |
178 | |
179 /*---------------------------------------------------------------------- | |
180 | NPT_Runnable | |
181 +---------------------------------------------------------------------*/ | |
182 class NPT_Runnable | |
183 { | |
184 public: | |
185 virtual ~NPT_Runnable() {} | |
186 virtual void Run() = 0; | |
187 }; | |
188 | |
189 /*---------------------------------------------------------------------- | |
190 | NPT_ThreadInterface | |
191 +---------------------------------------------------------------------*/ | |
192 class NPT_ThreadInterface: public NPT_Runnable, public NPT_Interruptible | |
193 { | |
194 public: | |
195 // methods | |
196 virtual ~NPT_ThreadInterface() {} | |
197 virtual NPT_Result Start() = 0; | |
198 virtual NPT_Result Wait(NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) = 0; | |
199 }; | |
200 | |
201 /*---------------------------------------------------------------------- | |
202 | NPT_Thread | |
203 +---------------------------------------------------------------------*/ | |
204 class NPT_Thread : public NPT_ThreadInterface | |
205 { | |
206 public: | |
207 // types | |
208 typedef unsigned long ThreadId; | |
209 | |
210 // class methods | |
211 static ThreadId GetCurrentThreadId(); | |
212 | |
213 // methods | |
214 explicit NPT_Thread(bool detached = false); | |
215 explicit NPT_Thread(NPT_Runnable& target, bool detached = false); | |
216 ~NPT_Thread() { delete m_Delegate; } | |
217 | |
218 // NPT_ThreadInterface methods | |
219 NPT_Result Start() { | |
220 return m_Delegate->Start(); | |
221 } | |
222 NPT_Result Wait(NPT_Timeout timeout = NPT_TIMEOUT_INFINITE) { | |
223 return m_Delegate->Wait(timeout); | |
224 } | |
225 | |
226 // NPT_Runnable methods | |
227 virtual void Run() {} | |
228 | |
229 // NPT_Interruptible methods | |
230 virtual NPT_Result Interrupt() { return m_Delegate->Interrupt(); } | |
231 | |
232 private: | |
233 // members | |
234 NPT_ThreadInterface* m_Delegate; | |
235 }; | |
236 | |
237 | |
238 /*---------------------------------------------------------------------- | |
239 | NPT_ThreadCallbackReceiver | |
240 +---------------------------------------------------------------------*/ | |
241 class NPT_ThreadCallbackReceiver | |
242 { | |
243 public: | |
244 virtual ~NPT_ThreadCallbackReceiver() {} | |
245 virtual void OnCallback(void* args) = 0; | |
246 }; | |
247 | |
248 /*---------------------------------------------------------------------- | |
249 | NPT_ThreadCallbackSlot | |
250 +---------------------------------------------------------------------*/ | |
251 class NPT_ThreadCallbackSlot | |
252 { | |
253 public: | |
254 // types | |
255 class NotificationHelper { | |
256 public: | |
257 virtual ~NotificationHelper() {}; | |
258 virtual void Notify(void) = 0; | |
259 }; | |
260 | |
261 // constructor | |
262 NPT_ThreadCallbackSlot(); | |
263 | |
264 // methods | |
265 NPT_Result ReceiveCallback(NPT_ThreadCallbackReceiver& receiver, NPT_Timeout timeout = 0); | |
266 NPT_Result SendCallback(void* args); | |
267 NPT_Result SetNotificationHelper(NotificationHelper* helper); | |
268 NPT_Result Shutdown(); | |
269 | |
270 protected: | |
271 // members | |
272 volatile void* m_CallbackArgs; | |
273 volatile bool m_Shutdown; | |
274 NPT_SharedVariable m_Pending; | |
275 NPT_SharedVariable m_Ack; | |
276 NPT_Mutex m_ReadLock; | |
277 NPT_Mutex m_WriteLock; | |
278 NotificationHelper* m_NotificationHelper; | |
279 }; | |
280 | |
281 #endif // _NPT_THREADS_H_ |