haiku/src/kits/media/PortPool.cpp
Adrien Destugues 083314c2cb Create the port pool in MediaRosterUndertaker
Follow up to #15135, which created a regression, apps would crash if
using the port pool before creating a BMediaRoster instance. Now the
port pool is entirely managed by the MediaRosterUndertaker, which is
less confusing.

Fixes the media_addon_server crash on shutdown for me as well, but I
don't remember why (I had studied the code and came to the conclusion
that this should fix it, but I don't remember the reasoning).

Fixes #15263, and regressions for apps using the port pool before they
instanciate the BMediaRoster.

Change-Id: I887dbc590d8ee9de391b6eae0206944bfe99325f
Reviewed-on: https://review.haiku-os.org/c/haiku/+/1897
Reviewed-by: Axel Dörfler <axeld@pinc-software.de>
2019-10-05 21:09:43 +00:00

74 lines
955 B
C++

/*
* Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
* Distributed under the terms of the MIT License.
*/
#include <set>
#include <Autolock.h>
#include <Locker.h>
#include <MediaDebug.h>
#include "PortPool.h"
namespace BPrivate {
namespace media {
PortPool* gPortPool;
// managed by MediaRosterUndertaker.
PortPool::PortPool()
:
BLocker("port pool")
{
}
PortPool::~PortPool()
{
PortSet::iterator iterator = fPool.begin();
for (; iterator != fPool.end(); iterator++)
delete_port(*iterator);
}
port_id
PortPool::GetPort()
{
BAutolock _(this);
if (fPool.empty())
return create_port(1, "media reply port");
port_id port = *fPool.begin();
fPool.erase(port);
ASSERT(port >= 0);
return port;
}
void
PortPool::PutPort(port_id port)
{
ASSERT(port >= 0);
BAutolock _(this);
try {
fPool.insert(port);
} catch (std::bad_alloc& exception) {
delete_port(port);
}
}
} // namespace media
} // namespace BPrivate