xref: /haiku/headers/os/app/Looper.h (revision 7749d0bb0c358a3279b1b9cc76d8376e900130a5)
1 /*
2  * Copyright 2001-2008, Haiku Inc. All Rights Reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Erik Jaesler (erik@cgsoftware.com)
7  */
8 #ifndef _LOOPER_H
9 #define _LOOPER_H
10 
11 
12 #include <BeBuild.h>
13 #include <Handler.h>
14 #include <List.h>
15 #include <OS.h>
16 
17 
18 class BMessage;
19 class BMessageQueue;
20 namespace BPrivate {
21 	class BDirectMessageTarget;
22 	class BLooperList;
23 }
24 
25 // Port (Message Queue) Capacity
26 #define B_LOOPER_PORT_DEFAULT_CAPACITY	200
27 
28 
29 class BLooper : public BHandler {
30 public:
31 							BLooper(const char* name = NULL,
32 								int32 priority = B_NORMAL_PRIORITY,
33 								int32 port_capacity
34 									= B_LOOPER_PORT_DEFAULT_CAPACITY);
35 	virtual					~BLooper();
36 
37 	// Archiving
38 							BLooper(BMessage* data);
39 	static	BArchivable*	Instantiate(BMessage* data);
40 	virtual	status_t		Archive(BMessage* data, bool deep = true) const;
41 
42 	// Message transmission
43 			status_t		PostMessage(uint32 command);
44 			status_t		PostMessage(BMessage* message);
45 			status_t		PostMessage(uint32 command, BHandler* handler,
46 								BHandler* replyTo = NULL);
47 			status_t		PostMessage(BMessage* message, BHandler* handler,
48 								BHandler* replyTo = NULL);
49 
50 	virtual	void			DispatchMessage(BMessage* message,
51 								BHandler* handler);
52 	virtual	void			MessageReceived(BMessage* message);
53 			BMessage*		CurrentMessage() const;
54 			BMessage*		DetachCurrentMessage();
55 			BMessageQueue*	MessageQueue() const;
56 			bool			IsMessageWaiting() const;
57 
58 	// Message handlers
59 			void			AddHandler(BHandler* handler);
60 			bool			RemoveHandler(BHandler* handler);
61 			int32			CountHandlers() const;
62 			BHandler*		HandlerAt(int32 index) const;
63 			int32			IndexOf(BHandler* handler) const;
64 
65 			BHandler*		PreferredHandler() const;
66 			void			SetPreferredHandler(BHandler* handler);
67 
68 	// Loop control
69 	virtual	thread_id		Run();
70 	virtual	void			Quit();
71 	virtual	bool			QuitRequested();
72 			bool			Lock();
73 			void			Unlock();
74 			bool			IsLocked() const;
75 			status_t		LockWithTimeout(bigtime_t timeout);
76 			thread_id		Thread() const;
77 			team_id			Team() const;
78 	static	BLooper*		LooperForThread(thread_id thread);
79 
80 	// Loop debugging
81 			thread_id		LockingThread() const;
82 			int32			CountLocks() const;
83 			int32			CountLockRequests() const;
84 			sem_id			Sem() const;
85 
86 	// Scripting
87 	virtual BHandler*		ResolveSpecifier(BMessage* msg, int32 index,
88 								BMessage* specifier, int32 form,
89 								const char* property);
90 	virtual status_t		GetSupportedSuites(BMessage* data);
91 
92 	// Message filters (also see BHandler).
93 	virtual	void			AddCommonFilter(BMessageFilter* filter);
94 	virtual	bool			RemoveCommonFilter(BMessageFilter* filter);
95 	virtual	void			SetCommonFilterList(BList* filters);
96 			BList*			CommonFilterList() const;
97 
98 	// Private or reserved
99 	virtual status_t		Perform(perform_code d, void* arg);
100 
101 protected:
102 		// called from overridden task_looper
103 			BMessage*		MessageFromPort(bigtime_t = B_INFINITE_TIMEOUT);
104 
105 private:
106 	typedef BHandler _inherited;
107 	friend class BWindow;
108 	friend class BApplication;
109 	friend class BMessenger;
110 	friend class BView;
111 	friend class BHandler;
112 	friend class BPrivate::BLooperList;
113 	friend port_id _get_looper_port_(const BLooper* );
114 
115 	virtual	void			_ReservedLooper1();
116 	virtual	void			_ReservedLooper2();
117 	virtual	void			_ReservedLooper3();
118 	virtual	void			_ReservedLooper4();
119 	virtual	void			_ReservedLooper5();
120 	virtual	void			_ReservedLooper6();
121 
122 							BLooper(const BLooper&);
123 			BLooper&		operator=(const BLooper&);
124 
125 							BLooper(int32 priority, port_id port,
126 								const char* name);
127 
128 			status_t		_PostMessage(BMessage* msg, BHandler* handler,
129 								BHandler* reply_to);
130 
131 	static	status_t		_Lock(BLooper* loop, port_id port,
132 								bigtime_t timeout);
133 	static	status_t		_LockComplete(BLooper* loop, int32 old,
134 								thread_id this_tid, sem_id sem,
135 								bigtime_t timeout);
136 			void			_InitData(const char* name, int32 priority, int32 capacity);
137 			void			AddMessage(BMessage* msg);
138 			void			_AddMessagePriv(BMessage* msg);
139 	static	status_t		_task0_(void* arg);
140 
141 			void*			ReadRawFromPort(int32* code,
142 								bigtime_t tout = B_INFINITE_TIMEOUT);
143 			BMessage*		ReadMessageFromPort(bigtime_t tout = B_INFINITE_TIMEOUT);
144 	virtual	BMessage*		ConvertToMessage(void* raw, int32 code);
145 	virtual	void			task_looper();
146 			void			_QuitRequested(BMessage* msg);
147 			bool			AssertLocked() const;
148 			BHandler*		_TopLevelFilter(BMessage* msg, BHandler* target);
149 			BHandler*		_HandlerFilter(BMessage* msg, BHandler* target);
150 			BHandler*		_ApplyFilters(BList* list, BMessage* msg,
151 								BHandler* target);
152 			void			check_lock();
153 			BHandler*		resolve_specifier(BHandler* target, BMessage* msg);
154 			void			UnlockFully();
155 
156 			BPrivate::BDirectMessageTarget* fDirectTarget;
157 			BMessage*		fLastMessage;
158 			port_id			fMsgPort;
159 			int32			fAtomicCount;
160 			sem_id			fLockSem;
161 			int32			fOwnerCount;
162 			thread_id		fOwner;
163 			thread_id		fThread;
164 			addr_t			fCachedStack;
165 			int32			fInitPriority;
166 			BHandler*		fPreferred;
167 			BList			fHandlers;
168 			BList*			fCommonFilters;
169 			bool			fTerminating;
170 			bool			fRunCalled;
171 			uint32			_reserved[11];
172 };
173 
174 #endif	// _LOOPER_H
175