2006-06-14 22:31:15 +04:00
|
|
|
/*
|
2007-01-26 15:42:18 +03:00
|
|
|
* Copyright 2001-2007, Haiku Inc. All Rights Reserved.
|
2006-06-14 22:31:15 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Erik Jaesler (erik@cgsoftware.com)
|
|
|
|
*/
|
2002-07-09 16:24:59 +04:00
|
|
|
#ifndef _LOOPER_H
|
|
|
|
#define _LOOPER_H
|
|
|
|
|
|
|
|
|
|
|
|
#include <BeBuild.h>
|
|
|
|
#include <Handler.h>
|
|
|
|
#include <List.h>
|
|
|
|
#include <OS.h>
|
|
|
|
|
|
|
|
|
|
|
|
class BMessage;
|
|
|
|
class BMessageQueue;
|
2002-07-15 19:51:31 +04:00
|
|
|
namespace BPrivate {
|
2007-01-26 16:59:56 +03:00
|
|
|
class BDirectMessageTarget;
|
2002-07-15 19:51:31 +04:00
|
|
|
class BLooperList;
|
|
|
|
}
|
2002-07-09 16:24:59 +04:00
|
|
|
|
2006-06-14 22:31:15 +04:00
|
|
|
// Port (Message Queue) Capacity
|
2002-07-09 16:24:59 +04:00
|
|
|
#define B_LOOPER_PORT_DEFAULT_CAPACITY 100
|
|
|
|
|
|
|
|
|
|
|
|
class BLooper : public BHandler {
|
|
|
|
public:
|
2006-06-14 22:31:15 +04:00
|
|
|
BLooper(const char* name = NULL,
|
2002-07-09 16:24:59 +04:00
|
|
|
int32 priority = B_NORMAL_PRIORITY,
|
|
|
|
int32 port_capacity = B_LOOPER_PORT_DEFAULT_CAPACITY);
|
2006-06-14 22:31:15 +04:00
|
|
|
virtual ~BLooper();
|
|
|
|
|
|
|
|
// Archiving
|
|
|
|
BLooper(BMessage* data);
|
|
|
|
static BArchivable* Instantiate(BMessage* data);
|
|
|
|
virtual status_t Archive(BMessage* data, bool deep = true) const;
|
|
|
|
|
|
|
|
// Message transmission
|
|
|
|
status_t PostMessage(uint32 command);
|
|
|
|
status_t PostMessage(BMessage* message);
|
|
|
|
status_t PostMessage(uint32 command, BHandler* handler,
|
2007-01-26 15:42:18 +03:00
|
|
|
BHandler* replyTo = NULL);
|
2006-06-14 22:31:15 +04:00
|
|
|
status_t PostMessage(BMessage* message, BHandler* handler,
|
2007-01-26 15:42:18 +03:00
|
|
|
BHandler* replyTo = NULL);
|
2006-06-14 22:31:15 +04:00
|
|
|
|
|
|
|
virtual void DispatchMessage(BMessage* message, BHandler* handler);
|
|
|
|
virtual void MessageReceived(BMessage* msg);
|
|
|
|
BMessage* CurrentMessage() const;
|
|
|
|
BMessage* DetachCurrentMessage();
|
|
|
|
BMessageQueue* MessageQueue() const;
|
|
|
|
bool IsMessageWaiting() const;
|
|
|
|
|
|
|
|
// Message handlers
|
|
|
|
void AddHandler(BHandler* handler);
|
|
|
|
bool RemoveHandler(BHandler* handler);
|
|
|
|
int32 CountHandlers() const;
|
|
|
|
BHandler* HandlerAt(int32 index) const;
|
|
|
|
int32 IndexOf(BHandler* handler) const;
|
|
|
|
|
|
|
|
BHandler* PreferredHandler() const;
|
|
|
|
void SetPreferredHandler(BHandler* handler);
|
|
|
|
|
|
|
|
// Loop control
|
|
|
|
virtual thread_id Run();
|
|
|
|
virtual void Quit();
|
|
|
|
virtual bool QuitRequested();
|
|
|
|
bool Lock();
|
|
|
|
void Unlock();
|
|
|
|
bool IsLocked() const;
|
|
|
|
status_t LockWithTimeout(bigtime_t timeout);
|
|
|
|
thread_id Thread() const;
|
|
|
|
team_id Team() const;
|
2007-01-26 15:42:18 +03:00
|
|
|
static BLooper* LooperForThread(thread_id thread);
|
2006-06-14 22:31:15 +04:00
|
|
|
|
|
|
|
// Loop debugging
|
|
|
|
thread_id LockingThread() const;
|
|
|
|
int32 CountLocks() const;
|
|
|
|
int32 CountLockRequests() const;
|
|
|
|
sem_id Sem() const;
|
|
|
|
|
|
|
|
// Scripting
|
|
|
|
virtual BHandler* ResolveSpecifier(BMessage* msg, int32 index,
|
|
|
|
BMessage* specifier, int32 form,
|
|
|
|
const char* property);
|
|
|
|
virtual status_t GetSupportedSuites(BMessage* data);
|
|
|
|
|
|
|
|
// Message filters (also see BHandler).
|
|
|
|
virtual void AddCommonFilter(BMessageFilter* filter);
|
|
|
|
virtual bool RemoveCommonFilter(BMessageFilter* filter);
|
|
|
|
virtual void SetCommonFilterList(BList* filters);
|
|
|
|
BList* CommonFilterList() const;
|
|
|
|
|
2007-01-26 15:42:18 +03:00
|
|
|
// Private or reserved
|
2006-06-14 22:31:15 +04:00
|
|
|
virtual status_t Perform(perform_code d, void* arg);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// called from overridden task_looper
|
2006-06-14 22:31:15 +04:00
|
|
|
BMessage* MessageFromPort(bigtime_t = B_INFINITE_TIMEOUT);
|
2002-07-09 16:24:59 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
typedef BHandler _inherited;
|
|
|
|
friend class BWindow;
|
|
|
|
friend class BApplication;
|
|
|
|
friend class BMessenger;
|
|
|
|
friend class BView;
|
|
|
|
friend class BHandler;
|
2002-07-15 19:51:31 +04:00
|
|
|
friend class BPrivate::BLooperList;
|
2002-07-09 16:24:59 +04:00
|
|
|
friend port_id _get_looper_port_(const BLooper* );
|
|
|
|
|
2006-06-14 22:31:15 +04:00
|
|
|
virtual void _ReservedLooper1();
|
|
|
|
virtual void _ReservedLooper2();
|
|
|
|
virtual void _ReservedLooper3();
|
|
|
|
virtual void _ReservedLooper4();
|
|
|
|
virtual void _ReservedLooper5();
|
|
|
|
virtual void _ReservedLooper6();
|
|
|
|
|
|
|
|
BLooper(const BLooper&);
|
|
|
|
BLooper& operator=(const BLooper&);
|
|
|
|
|
|
|
|
BLooper(int32 priority, port_id port,
|
|
|
|
const char* name);
|
|
|
|
|
|
|
|
status_t _PostMessage(BMessage* msg, BHandler* handler,
|
|
|
|
BHandler* reply_to);
|
|
|
|
|
|
|
|
static status_t _Lock(BLooper* loop, port_id port,
|
|
|
|
bigtime_t timeout);
|
|
|
|
static status_t _LockComplete(BLooper* loop, int32 old,
|
|
|
|
thread_id this_tid, sem_id sem,
|
|
|
|
bigtime_t timeout);
|
2007-01-26 15:42:18 +03:00
|
|
|
void _InitData(const char* name, int32 priority, int32 capacity);
|
2006-06-14 22:31:15 +04:00
|
|
|
void AddMessage(BMessage* msg);
|
|
|
|
void _AddMessagePriv(BMessage* msg);
|
|
|
|
static status_t _task0_(void* arg);
|
|
|
|
|
|
|
|
void* ReadRawFromPort(int32* code,
|
|
|
|
bigtime_t tout = B_INFINITE_TIMEOUT);
|
|
|
|
BMessage* ReadMessageFromPort(bigtime_t tout = B_INFINITE_TIMEOUT);
|
|
|
|
virtual BMessage* ConvertToMessage(void* raw, int32 code);
|
|
|
|
virtual void task_looper();
|
|
|
|
void _QuitRequested(BMessage* msg);
|
|
|
|
bool AssertLocked() const;
|
|
|
|
BHandler* _TopLevelFilter(BMessage* msg, BHandler* target);
|
|
|
|
BHandler* _HandlerFilter(BMessage* msg, BHandler* target);
|
|
|
|
BHandler* _ApplyFilters(BList* list, BMessage* msg,
|
|
|
|
BHandler* target);
|
|
|
|
void check_lock();
|
|
|
|
BHandler* resolve_specifier(BHandler* target, BMessage* msg);
|
|
|
|
void UnlockFully();
|
|
|
|
|
2007-01-26 16:59:56 +03:00
|
|
|
BPrivate::BDirectMessageTarget* fDirectTarget;
|
2006-06-14 22:31:15 +04:00
|
|
|
BMessage* fLastMessage;
|
|
|
|
port_id fMsgPort;
|
2007-01-26 15:42:18 +03:00
|
|
|
int32 fAtomicCount;
|
2006-06-14 22:31:15 +04:00
|
|
|
sem_id fLockSem;
|
2007-01-26 15:42:18 +03:00
|
|
|
int32 fOwnerCount;
|
2006-06-14 22:31:15 +04:00
|
|
|
thread_id fOwner;
|
2007-01-26 15:42:18 +03:00
|
|
|
thread_id fThread;
|
2007-08-09 02:43:52 +04:00
|
|
|
addr_t fCachedStack;
|
2006-06-14 22:31:15 +04:00
|
|
|
int32 fInitPriority;
|
|
|
|
BHandler* fPreferred;
|
|
|
|
BList fHandlers;
|
|
|
|
BList* fCommonFilters;
|
|
|
|
bool fTerminating;
|
|
|
|
bool fRunCalled;
|
2007-08-09 02:43:52 +04:00
|
|
|
uint32 _reserved[11];
|
2002-07-09 16:24:59 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // _LOOPER_H
|