1/*
2 * Copyright 2005, 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
17class MessageLooper : public BLocker {
18	public:
19		MessageLooper(const char* name);
20		virtual ~MessageLooper();
21
22		virtual	bool	Run();
23		virtual	void	Quit();
24
25		status_t		PostMessage(int32 code, bigtime_t timeout = B_INFINITE_TIMEOUT);
26		thread_id		Thread() const { return fThread; }
27		bool			IsQuitting() const { return fQuitting; }
28		sem_id			DeathSemaphore() const { return fDeathSemaphore; }
29
30		virtual port_id	MessagePort() const = 0;
31
32		static status_t	WaitForQuit(sem_id semaphore,
33							bigtime_t timeout = B_INFINITE_TIMEOUT);
34
35	private:
36		virtual void	_PrepareQuit();
37		virtual void	_GetLooperName(char* name, size_t length);
38		virtual void	_DispatchMessage(int32 code, BPrivate::LinkReceiver &link);
39		virtual void	_MessageLooper();
40
41	protected:
42		static int32	_message_thread(void *_looper);
43
44	protected:
45		thread_id		fThread;
46		BPrivate::PortLink fLink;
47		bool			fQuitting;
48		sem_id			fDeathSemaphore;
49};
50
51static const int32 kMsgQuitLooper = 'quit';
52
53#endif	/* MESSAGE_LOOPER_H */
54