xref: /haiku/src/servers/app/MessageLooper.h (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2005-2016, Haiku.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 #ifndef MESSAGE_LOOPER_H
9 #define MESSAGE_LOOPER_H
10 
11 
12 #include <PortLink.h>
13 #include <Locker.h>
14 #include <OS.h>
15 
16 
17 class MessageLooper : public BLocker {
18 public:
19 								MessageLooper(const char* name);
20 	virtual						~MessageLooper();
21 
22 	virtual	status_t			Run();
23 	virtual	void				Quit();
24 
25 			status_t			PostMessage(int32 code,
26 									bigtime_t timeout = B_INFINITE_TIMEOUT);
27 
28 			thread_id			Thread() const { return fThread; }
29 			bool				IsQuitting() const { return fQuitting; }
30 			sem_id				DeathSemaphore() const
31 									{ return fDeathSemaphore; }
32 
33 	virtual	port_id				MessagePort() const = 0;
34 
35 	static	status_t			WaitForQuit(sem_id semaphore,
36 									bigtime_t timeout = B_INFINITE_TIMEOUT);
37 
38 private:
39 	virtual	void				_PrepareQuit();
40 	virtual	void				_GetLooperName(char* name, size_t length);
41 	virtual	void				_DispatchMessage(int32 code,
42 									BPrivate::LinkReceiver& link);
43 	virtual	void				_MessageLooper();
44 
45 protected:
46 	static	int32				_message_thread(void*_looper);
47 
48 protected:
49 			thread_id			fThread;
50 			BPrivate::PortLink	fLink;
51 			bool				fQuitting;
52 			sem_id				fDeathSemaphore;
53 };
54 
55 
56 static const int32 kMsgQuitLooper = 'quit';
57 
58 
59 #endif	/* MESSAGE_LOOPER_H */
60