Skip to content

Commit

Permalink
Add a way to register loopers for quit
Browse files Browse the repository at this point in the history
* BApplication can now take the job to quit a BLooper at
the application quit. It's rejecting requests from windows too.
* BMediaRoster is using now this service in conjunction with the
MediaRosterUndertaker.
* The BeBook specify that we should have a valid BApplication
before to instantiate the BMediaRoster. While in theory we should
add a debugger call when this situation happens, in pratice this
might lead to more problems. For example libraries might use the
media_kit and create a BApplication object, but they aren't
applications, this is a design problem. So I decided to replace it
with a TRACE call for the moment.
  • Loading branch information
Numerio committed Nov 28, 2015
1 parent 893e3de commit 05962bb
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
5 changes: 5 additions & 0 deletions headers/os/app/Application.h
Expand Up @@ -85,6 +85,11 @@ class BApplication : public BLooper {
BHandler* handler);
void SetPulseRate(bigtime_t rate);

// Register a BLooper to be quit before the BApplication
// object is destroyed.
status_t RegisterLooper(BLooper* looper);
status_t UnregisterLooper(BLooper* looper);

// More scripting
virtual status_t GetSupportedSuites(BMessage* data);

Expand Down
42 changes: 42 additions & 0 deletions src/kits/app/Application.cpp
Expand Up @@ -61,6 +61,7 @@ BMessenger be_app_messenger;

pthread_once_t sAppResourcesInitOnce = PTHREAD_ONCE_INIT;
BResources* BApplication::sAppResources = NULL;
BObjectList<BLooper> sOnQuitLooperList;


enum {
Expand Down Expand Up @@ -306,6 +307,13 @@ BApplication::~BApplication()
// tell all loopers(usually windows) to quit. Also, wait for them.
_QuitAllWindows(true);

// quit registered loopers
for (int32 i = 0; i < sOnQuitLooperList.CountItems(); i++) {
BLooper* looper = sOnQuitLooperList.ItemAt(i);
if (looper->Lock())
looper->Quit();
}

// unregister from the roster
BRoster::Private().RemoveApp(Team());

Expand Down Expand Up @@ -960,6 +968,40 @@ BApplication::LooperAt(int32 index) const
}


status_t
BApplication::RegisterLooper(BLooper* looper)
{
BWindow* window = dynamic_cast<BWindow*>(looper);
if (window != NULL)
return B_BAD_VALUE;

if (sOnQuitLooperList.HasItem(looper))
return B_ERROR;

if (sOnQuitLooperList.AddItem(looper) != true)
return B_ERROR;

return B_OK;
}


status_t
BApplication::UnregisterLooper(BLooper* looper)
{
BWindow* window = dynamic_cast<BWindow*>(looper);
if (window != NULL)
return B_BAD_VALUE;

if (!sOnQuitLooperList.HasItem(looper))
return B_ERROR;

if (sOnQuitLooperList.RemoveItem(looper) != true)
return B_ERROR;

return B_OK;
}


bool
BApplication::IsLaunching() const
{
Expand Down
17 changes: 14 additions & 3 deletions src/kits/media/MediaRoster.cpp
Expand Up @@ -42,8 +42,7 @@ char __dont_remove_copyright_from_binary[] = "Copyright (c) 2002-2006 Marcus "

#include <MediaRoster.h>

#include <new>

#include <Application.h>
#include <Autolock.h>
#include <BufferConsumer.h>
#include <BufferProducer.h>
Expand All @@ -58,8 +57,9 @@ char __dont_remove_copyright_from_binary[] = "Copyright (c) 2002-2006 Marcus "
#include <String.h>
#include <TimeSource.h>

#include <AppMisc.h>
#include <new>

#include <AppMisc.h>
#include <DataExchange.h>
#include <debug.h>
#include <DormantNodeManager.h>
Expand All @@ -82,6 +82,7 @@ struct RosterNotification {
int32 what;
};


static bool sServerIsUp = false;
static List<RosterNotification> sNotificationList;
static BLocker sInitLocker("BMediaRoster::Roster locker");
Expand All @@ -94,6 +95,10 @@ class MediaRosterUndertaker {
BAutolock _(sInitLocker);
if (BMediaRoster::CurrentRoster() != NULL
&& BMediaRoster::CurrentRoster()->Lock()) {

if (be_app != NULL)
be_app->UnregisterLooper(BMediaRoster::CurrentRoster());

BMediaRoster::CurrentRoster()->Quit();
}
}
Expand Down Expand Up @@ -2221,6 +2226,9 @@ BMediaRoster::Roster(status_t* out_error)
{
BAutolock lock(sInitLocker);

if (be_app == NULL)
TRACE("Warning! You should have a valid BApplication.");

if (!lock.IsLocked())
return NULL;

Expand All @@ -2240,8 +2248,11 @@ BMediaRoster::Roster(status_t* out_error)
}
if (out_error)
*out_error = err;
} else if (be_app != NULL) {
be_app->RegisterLooper(sDefaultInstance);
}
}

return sDefaultInstance;
}

Expand Down

0 comments on commit 05962bb

Please sign in to comment.