File: thread.h

package info (click to toggle)
libape 1.0.0-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,572 kB
  • ctags: 1,343
  • sloc: sh: 7,342; cpp: 3,418; makefile: 117
file content (351 lines) | stat: -rw-r--r-- 7,575 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (C) 1999 David Sugar <dyfet@tycho.com>
//  
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software 
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
// 
// As a special exception to the GNU General Public License, permission is 
// granted for additional uses of the text contained in its release 
// of APE.
// 
// The exception is that, if you link the APE library with other files
// to produce an executable, this does not by itself cause the
// resulting executable to be covered by the GNU General Public License.
// Your use of that executable is in no way restricted on account of
// linking the APE library code into it.
// 
// This exception does not however invalidate any other reasons why
// the executable file might be covered by the GNU General Public License.
// 
// This exception applies only to the code released under the 
// name APE.  If you copy code from other releases into a copy of
// APE, as the General Public License permits, the exception does
// not apply to the code that you add in this way.  To avoid misleading
// anyone as to the status of such modified files, you must delete
// this exception notice from them.
// 
// If you write modifications of your own for APE, it is your choice
// whether to permit this exception to apply to your modifications.
// If you do not wish that, delete this exception notice.  

#ifndef	__APE_THREAD_H__
#define	__APE_THREAD_H__

#include <setjmp.h>

#ifndef	__APE_WIN32
#define	__APE_WIN32
#include <windows.h>
#define	__EXPORT	__declspec(dllexport)
#endif

#ifndef	__APE_FILEIO_H__
#include <APE/fileio.h>
#endif

typedef	DWORD	tid_t;
typedef DWORD   timeout_t;

#define	MAX_SEM_VALUE	1000000

#define	ENTER_CRITICAL	EnterMutex();
#define	LEAVE_CRITICAL	LeaveMutex();

typedef	enum
{
	THREAD_CANCEL_IMMEDIATE = 1,
	THREAD_CANCEL_DEFERRED,
	THREAD_CANCEL_DEFAULT = THREAD_CANCEL_DEFERRED,
	THREAD_CANCEL_DISABLED,
	THREAD_CANCEL_INITIAL
} thread_cancel_t;

class Thread;
#define sleep(x) delay(x * 1000l)
#undef Yield

BOOL __EXPORT APIENTRY DllMain(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved);
Thread *getAPE(void);
DWORD waitAPE(HANDLE hRef, timeout_t timeout);
void yield(void);
void delay(timeout_t timeout);

class __EXPORT Mutex
{
private:
	CRITICAL_SECTION _mutex;

public:
	~Mutex(void)
		{DeleteCriticalSection(&_mutex);};
	
	Mutex(void)
		{InitializeCriticalSection(&_mutex);};

	inline void EnterMutex(void)
		{EnterCriticalSection(&_mutex);};

	inline void LeaveMutex(void)
		{LeaveCriticalSection(&_mutex);};

	friend inline void operator ++(Mutex &m)
		{EnterCriticalSection(&(m._mutex));};

	friend inline void operator --(Mutex &m)
		{LeaveCriticalSection(&(m._mutex));};
};

class __EXPORT MutexCounter : public Mutex
{
private:
	int counter;

public:
	MutexCounter();

	friend int operator++(MutexCounter &);
	friend int operator--(MutexCounter &);
};

class __EXPORT Event
{
private:
	HANDLE cond;

public:
	Event();

	~Event()
		{CloseHandle(cond);};

	inline void Reset(void)
		{ResetEvent(cond);};

	inline void Signal(void)
		{SetEvent(cond);};

	inline bool Wait(void)
		{return (waitAPE(cond, INFINITE) == WAIT_OBJECT_0);};

	inline void Wait(timeout_t timer)
		{waitAPE(cond, timer);};

	friend inline void reset(Event &ev)
		{ResetEvent(ev.cond);};

	friend inline void signal(Event &ev)
		{SetEvent(ev.cond);};

	friend inline void wait(Event &ev)
		{waitAPE(ev.cond, INFINITE);};

	friend inline void wait(Event &ev, timeout_t timer)
		{waitAPE(ev.cond, timer);};
};

class __EXPORT Semaphore
{
private:
	HANDLE	semObject;

public:
	~Semaphore()
		{CloseHandle(semObject);};

	Semaphore(size_t count = 0)
		{CreateSemaphore(NULL, count, MAX_SEM_VALUE, NULL);};

	void Post(void)
		{ReleaseSemaphore(semObject, 1, NULL);};

	void Wait(void)
		{waitAPE(semObject, INFINITE);};
};

class __EXPORT Buffer
{
private:
	Mutex lock_head, lock_tail;
	Semaphore size_head, size_tail;
	size_t _size, _used;

protected:
	virtual int OnWait(void *buf) = 0;
	virtual int OnPost(void *buf) = 0;
	virtual int OnPeek(void *buf) = 0;

public:
	Buffer(size_t capacity);

	virtual ~Buffer()
		{return;};

	inline size_t getSize(void)
		{return _size;};

	inline size_t getUsed(void)
		{return _used;};

	int Wait(void *buf);
	int Post(void *buf);
	int Peek(void *buf);
};

class __EXPORT FixedBuffer : public Buffer
{
private:
	char *buf, *head, *tail;
	size_t objsize;

protected:
	int OnPeek(void *data);
	int OnWait(void *data);
	int OnPost(void *data);

public:
	FixedBuffer(size_t capacity, size_t objsize);
	~FixedBuffer();
};

class __EXPORT Pipe
{
private:
	HANDLE reader;
	HANDLE writer;

public:
	Pipe();
	Pipe(const Pipe &p);
	~Pipe();

	inline Read(void *addr, size_t len)
		{return hRead(reader, addr, len);};

	inline Write(void *addr, size_t len)
		{return hWrite(writer, addr, len);};

	friend inline read(Pipe &p, void *addr, size_t len)
		{return hRead(p.reader, addr, len);};

	friend inline write(Pipe &p, void *addr, size_t len)
		{return hWrite(p.writer, addr, len);};

	Pipe &operator=(const Pipe &p);
};

class __EXPORT Thread
{
private:
	static Thread *_main;
	Thread *_parent;
	DWORD _tid;
	HANDLE _cancellation;
	thread_cancel_t	_cancel;
	jmp_buf _env;
	Semaphore *_start;

	static unsigned __stdcall Execute(Thread *th);

protected:
	HANDLE	_hThread;

	virtual void Initial(void)
		{return;};

	virtual void Final(void)
		{return;};

	virtual void Run(void) = 0;

	inline void Exit(void)
		{longjmp(_env, 1);};

	void setCancel(thread_cancel_t cancel);
	void Yield(void);
	void Terminate(void);
	void Sleep(timeout_t timeout);
	DWORD WaitHandle(HANDLE obj, timeout_t timeout);

public:
	inline Thread *getParent(void)
		{return _parent;};

	inline thread_cancel_t getCancel(void)
		{return _cancel;};

	inline bool isRunning(void)
		{return (_tid != 0);};

	inline bool isThread(void)
		{return (_tid == GetCurrentThreadId());};

	Thread(Semaphore *start = NULL, int pri = 0, unsigned stack = 0);
	Thread(bool flag);

	int Start(Semaphore *sem = NULL);

	virtual ~Thread()
		{Terminate();};

	virtual void *getExtended(void)
		{return NULL;};

	virtual void Notify(Thread *th)
		{return;};

	friend inline int start(Thread &th, Semaphore *sem = NULL)
		{return th.Start(sem);};

	friend inline void operator++(Thread &th)
		{th._start->Post();};

	friend DWORD waitAPE(HANDLE hRef, timeout_t timeout);
	friend void delay(timeout_t timeout);
	friend void yield(void);
};

class ThreadKey
{
private:
	DWORD	key;

public:
	ThreadKey();
	~ThreadKey();

	void *getKey(void);
	void setKey(void *key);

	friend inline void *getKey(ThreadKey &th)
		{return th.getKey();};

	friend inline void setKey(ThreadKey &th, void *ptr)
		{th.setKey(ptr);};
};

inline int get(Buffer &b, void *o)
	{return b.Wait(o);};

inline int put(Buffer &b, void *o)
	{return b.Post(o);};

inline int peek(Buffer &b, void *o)
	{return b.Peek(o);};

inline void operator++(Semaphore &s)
	{s.Post();};

inline void operator--(Semaphore &s)
	{s.Wait();};

#endif