headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / media / request_data.cpp
blob3af50ca71b58029ec58157aefe3803357f4d0b29
1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include <ServerInterface.h>
9 #include <set>
11 #include <Autolock.h>
12 #include <Locker.h>
14 #include <DataExchange.h>
15 #include <debug.h>
18 namespace BPrivate {
19 namespace media {
22 class PortPool : BLocker {
23 public:
24 PortPool();
25 ~PortPool();
27 port_id GetPort();
28 void PutPort(port_id port);
30 private:
31 typedef std::set<port_id> PortSet;
33 PortSet fPool;
37 static PortPool sPortPool;
40 PortPool::PortPool()
42 BLocker("port pool")
47 PortPool::~PortPool()
49 PortSet::iterator iterator = fPool.begin();
51 for (; iterator != fPool.end(); iterator++)
52 delete_port(*iterator);
56 port_id
57 PortPool::GetPort()
59 BAutolock _(this);
61 if (fPool.empty())
62 return create_port(1, "media reply port");
64 port_id port = *fPool.begin();
65 fPool.erase(port);
67 ASSERT(port >= 0);
68 return port;
72 void
73 PortPool::PutPort(port_id port)
75 ASSERT(port >= 0);
77 BAutolock _(this);
79 try {
80 fPool.insert(port);
81 } catch (std::bad_alloc& exception) {
82 delete_port(port);
87 // #pragma mark -
90 request_data::request_data()
92 reply_port = sPortPool.GetPort();
96 request_data::~request_data()
98 sPortPool.PutPort(reply_port);
102 status_t
103 request_data::SendReply(status_t result, reply_data *reply,
104 size_t replySize) const
106 reply->result = result;
107 // we cheat and use the (command_data *) version of SendToPort
108 return SendToPort(reply_port, 0, reinterpret_cast<command_data *>(reply),
109 replySize);
113 } // namespace media
114 } // namespace BPrivate