BLooper: API to hijack existing thread.
I need this to use loopers in WebKit, which spawns threads and expects to be able to turn them into event loops later on. This is the pattern already used in BApplication, we may as well make it available elsewhere. Change-Id: I5939ca89d33cb3bcc92567b302c2038d976af598 Reviewed-on: https://review.haiku-os.org/735 Reviewed-by: Axel Dörfler <[email protected]> Reviewed-by: waddlesplash <[email protected]>
This commit is contained in:
committed by
waddlesplash
parent
4bba0571c6
commit
151343ebc8
+25
-10
@@ -1,14 +1,15 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2008-2014 Haiku, Inc. All rights reserved.
|
* Copyright 2008-2018 Haiku, Inc. All rights reserved.
|
||||||
* Distributed under the terms of the MIT License.
|
* Distributed under the terms of the MIT License.
|
||||||
*
|
*
|
||||||
* Authors:
|
* Authors:
|
||||||
* Niels Sascha Reedijk, [email protected]
|
* Niels Sascha Reedijk, [email protected]
|
||||||
* John Scipione, [email protected]
|
* John Scipione, [email protected]
|
||||||
|
* Adrien Destugues, [email protected]
|
||||||
*
|
*
|
||||||
* Corresponds to:
|
* Corresponds to:
|
||||||
* headers/os/app/Looper.h hrev47355
|
* headers/os/app/Looper.h hrev52501
|
||||||
* src/kits/app/Looper.cpp hrev47355
|
* src/kits/app/Looper.cpp hrev52501
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@@ -72,14 +73,14 @@
|
|||||||
derived from BLooper) to customize your window and handle the messages
|
derived from BLooper) to customize your window and handle the messages
|
||||||
sent to that window. You can override Run() in case you want to perform
|
sent to that window. You can override Run() in case you want to perform
|
||||||
additional tasks before (or right after) starting the message loop. You can
|
additional tasks before (or right after) starting the message loop. You can
|
||||||
override QuitRequested() if you want to decline quitting in certain
|
override QuitRequested() if you want to decline quitting in certain
|
||||||
circumstances. You can override Quit() in case you want to perform
|
circumstances. You can override Quit() in case you want to perform
|
||||||
additional procedures during closing time. You can also override
|
additional procedures during closing time. You can also override
|
||||||
DispatchMessage() if you want to do something with all incoming messages
|
DispatchMessage() if you want to do something with all incoming messages
|
||||||
before they are dispatched to a handler.
|
before they are dispatched to a handler.
|
||||||
|
|
||||||
BLooper is one of the major base classes of the Haiku application
|
BLooper is one of the major base classes of the Haiku application
|
||||||
programmers interface. Closely related classes are BMessage, BHandler and
|
programmers interface. Closely related classes are BMessage, BHandler and
|
||||||
BMessenger. It is used in the interface kit, for example by the BWindow
|
BMessenger. It is used in the interface kit, for example by the BWindow
|
||||||
class, which makes sure every window runs it its own thread.
|
class, which makes sure every window runs it its own thread.
|
||||||
|
|
||||||
@@ -115,7 +116,7 @@
|
|||||||
receives a lot of messages, or if the message handling thread runs
|
receives a lot of messages, or if the message handling thread runs
|
||||||
at a lower priority than normal, which would decrease the processing
|
at a lower priority than normal, which would decrease the processing
|
||||||
speed. Finding a suitable value for these custom scenarios would be
|
speed. Finding a suitable value for these custom scenarios would be
|
||||||
done by testing.
|
done by testing.
|
||||||
|
|
||||||
\see Run()
|
\see Run()
|
||||||
|
|
||||||
@@ -127,7 +128,7 @@
|
|||||||
\fn BLooper::~BLooper()
|
\fn BLooper::~BLooper()
|
||||||
\brief Destruct the looper.
|
\brief Destruct the looper.
|
||||||
|
|
||||||
You will never delete a looper yourself. You should pass a
|
You will never delete a looper yourself. You should pass a
|
||||||
\c B_QUIT_REQUESTED message, or if you are destroying the looper from
|
\c B_QUIT_REQUESTED message, or if you are destroying the looper from
|
||||||
inside its own message handling thread, you should call Quit().
|
inside its own message handling thread, you should call Quit().
|
||||||
|
|
||||||
@@ -186,7 +187,7 @@
|
|||||||
\brief Archive a looper to a message
|
\brief Archive a looper to a message
|
||||||
|
|
||||||
Currently, only the name and the port capacity are archived. Any other
|
Currently, only the name and the port capacity are archived. Any other
|
||||||
data, such as the filters, is not stored.
|
data, such as the filters, is not stored.
|
||||||
|
|
||||||
\param data The message to archive the object in.
|
\param data The message to archive the object in.
|
||||||
\param deep This parameter is ignored, as BLooper does not have children.
|
\param deep This parameter is ignored, as BLooper does not have children.
|
||||||
@@ -312,7 +313,7 @@
|
|||||||
/*!
|
/*!
|
||||||
\fn status_t BLooper::PostMessage(BMessage* message, BHandler* handler,
|
\fn status_t BLooper::PostMessage(BMessage* message, BHandler* handler,
|
||||||
BHandler* replyTo)
|
BHandler* replyTo)
|
||||||
\brief Send a \a message to the \a handler associated with this looper,
|
\brief Send a \a message to the \a handler associated with this looper,
|
||||||
and (optionally) request a reply.
|
and (optionally) request a reply.
|
||||||
|
|
||||||
The target \a handler should be associated with this looper. This method
|
The target \a handler should be associated with this looper. This method
|
||||||
@@ -603,11 +604,25 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
/*!
|
||||||
|
\fn thread_id BLooper::Loop()
|
||||||
|
\brief Run the event loop in the current thread.
|
||||||
|
|
||||||
|
This method runs the event loop in an already existing thread. It blocks
|
||||||
|
until the looper stops looping. This can
|
||||||
|
be used to turn an existing thread into a BLooper.
|
||||||
|
|
||||||
|
Make sure the looper is not yet running before you call this method.
|
||||||
|
|
||||||
|
\since Haiku R1
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
\fn void BLooper::Quit()
|
\fn void BLooper::Quit()
|
||||||
\brief Hook method that is called after a \c B_QUIT_REQUESTED message.
|
\brief Hook method that is called after a \c B_QUIT_REQUESTED message.
|
||||||
|
|
||||||
If you want to quit and delete the looper, you should post a
|
If you want to quit and delete the looper, you should post a
|
||||||
\c B_QUIT_REQUESTED message. This will first call the hook method
|
\c B_QUIT_REQUESTED message. This will first call the hook method
|
||||||
QuitRequested(), which can be overridden in child classes in case there
|
QuitRequested(), which can be overridden in child classes in case there
|
||||||
are conditions that would prevent the looper to be quit. If you really
|
are conditions that would prevent the looper to be quit. If you really
|
||||||
|
|||||||
@@ -69,6 +69,7 @@ public:
|
|||||||
|
|
||||||
// Loop control
|
// Loop control
|
||||||
virtual thread_id Run();
|
virtual thread_id Run();
|
||||||
|
void Loop();
|
||||||
virtual void Quit();
|
virtual void Quit();
|
||||||
virtual bool QuitRequested();
|
virtual bool QuitRequested();
|
||||||
bool Lock();
|
bool Lock();
|
||||||
|
|||||||
@@ -590,15 +590,7 @@ BApplication::Run()
|
|||||||
if (fInitError != B_OK)
|
if (fInitError != B_OK)
|
||||||
return fInitError;
|
return fInitError;
|
||||||
|
|
||||||
AssertLocked();
|
Loop();
|
||||||
|
|
||||||
if (fRunCalled)
|
|
||||||
debugger("BApplication::Run was already called. Can only be called once.");
|
|
||||||
|
|
||||||
fThread = find_thread(NULL);
|
|
||||||
fRunCalled = true;
|
|
||||||
|
|
||||||
task_looper();
|
|
||||||
|
|
||||||
delete fPulseRunner;
|
delete fPulseRunner;
|
||||||
return fThread;
|
return fThread;
|
||||||
|
|||||||
@@ -457,6 +457,24 @@ BLooper::Run()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void
|
||||||
|
BLooper::Loop()
|
||||||
|
{
|
||||||
|
AssertLocked();
|
||||||
|
|
||||||
|
if (fRunCalled) {
|
||||||
|
// Not allowed to call Loop() or Run() more than once
|
||||||
|
debugger("can't call BLooper::Loop twice!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
fThread = find_thread(NULL);
|
||||||
|
fRunCalled = true;
|
||||||
|
|
||||||
|
task_looper();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
BLooper::Quit()
|
BLooper::Quit()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user