1/*
2 * Copyright 2009, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <set>
8
9#include <Autolock.h>
10#include <Locker.h>
11
12#include <MediaDebug.h>
13
14#include "PortPool.h"
15
16
17namespace BPrivate {
18namespace media {
19
20
21PortPool* gPortPool;
22	// managed by MediaRosterUndertaker.
23
24
25PortPool::PortPool()
26	:
27	BLocker("port pool")
28{
29}
30
31
32PortPool::~PortPool()
33{
34	PortSet::iterator iterator = fPool.begin();
35
36	for (; iterator != fPool.end(); iterator++)
37		delete_port(*iterator);
38}
39
40
41port_id
42PortPool::GetPort()
43{
44	BAutolock _(this);
45
46	if (fPool.empty())
47		return create_port(1, "media reply port");
48
49	port_id port = *fPool.begin();
50	fPool.erase(port);
51
52	ASSERT(port >= 0);
53	return port;
54}
55
56
57void
58PortPool::PutPort(port_id port)
59{
60	ASSERT(port >= 0);
61
62	BAutolock _(this);
63
64	try {
65		fPool.insert(port);
66	} catch (std::bad_alloc& exception) {
67		delete_port(port);
68	}
69}
70
71
72}	// namespace media
73}	// namespace BPrivate
74