[haiku-commits] r34540 - in haiku/trunk: headers/private/media src/kits/media

  • From: axeld@xxxxxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Mon, 7 Dec 2009 22:28:09 +0100 (CET)

Author: axeld
Date: 2009-12-07 22:28:08 +0100 (Mon, 07 Dec 2009)
New Revision: 34540
Changeset: http://dev.haiku-os.org/changeset/34540/haiku

Modified:
   haiku/trunk/headers/private/media/PortPool.h
   haiku/trunk/src/kits/media/DataExchange.cpp
   haiku/trunk/src/kits/media/DormantNodeManager.cpp
   haiku/trunk/src/kits/media/MediaRoster.cpp
   haiku/trunk/src/kits/media/PortPool.cpp
Log:
* Rewrote PortPool, and put it into the BPrivate namespace.
* Renamed global variable _PortPool to gPortPool.


Modified: haiku/trunk/headers/private/media/PortPool.h
===================================================================
--- haiku/trunk/headers/private/media/PortPool.h        2009-12-07 21:27:21 UTC 
(rev 34539)
+++ haiku/trunk/headers/private/media/PortPool.h        2009-12-07 21:28:08 UTC 
(rev 34540)
@@ -1,35 +1,41 @@
 /*
- * Copyright 2002, Marcus Overhagen. All Rights Reserved.
+ * Copyright 2009, Axel Dörfler, axeld@xxxxxxxxxxxxxxxxx
  * Distributed under the terms of the MIT License.
  */
-#ifndef _POOL_PORT_H_
-#define _POOL_PORT_H_
+#ifndef PORT_POOL_H
+#define PORT_POOL_H
 
 
-class PortPool {
+#include <set>
+
+#include <Locker.h>
+
+
+namespace BPrivate {
+
+
+class PortPool : BLocker {
 public:
-       PortPool();
-       ~PortPool();
+                                                               PortPool();
+                                                               ~PortPool();
 
-       port_id GetPort();
-       void PutPort(port_id port);
+                       port_id                         GetPort();
+                       void                            PutPort(port_id port);
 
 private:
-       void Lock();
-       void Unlock();
+                       typedef std::set<port_id> PortSet;
 
-       struct PortInfo {
-               port_id port;
-               bool used;
-       };
-
-       PortInfo *      pool;
-       int             count;
-       int             maxcount;
-       int32           locker_atom;
-       sem_id          locker_sem;
+                       PortSet                         fPool;
 };
 
-extern PortPool *_PortPool;
 
-#endif
+extern PortPool* gPortPool;
+
+
+}      // namespace BPrivate
+
+
+using BPrivate::gPortPool;
+
+
+#endif // PORT_POOL_H

Modified: haiku/trunk/src/kits/media/DataExchange.cpp
===================================================================
--- haiku/trunk/src/kits/media/DataExchange.cpp 2009-12-07 21:27:21 UTC (rev 
34539)
+++ haiku/trunk/src/kits/media/DataExchange.cpp 2009-12-07 21:28:08 UTC (rev 
34540)
@@ -179,7 +179,7 @@
        status_t rv;
        int32 code;
 
-       request->reply_port = _PortPool->GetPort();
+       request->reply_port = gPortPool->GetPort();
 
        rv = write_port_etc(requestport, msgcode, request, requestsize, 
B_RELATIVE_TIMEOUT, TIMEOUT);
 
@@ -192,20 +192,20 @@
                        find_media_addon_server_port();
                        requestport = MediaAddonServerPort;
                } else {
-                       _PortPool->PutPort(request->reply_port);
+                       gPortPool->PutPort(request->reply_port);
                        return rv;
                }
 
                rv = write_port_etc(requestport, msgcode, request, requestsize, 
B_RELATIVE_TIMEOUT, TIMEOUT);
                if (rv != B_OK) {
                        ERROR("QueryPort: retrying write_port failed, msgcode 
0x%lx, port %ld, error %#lx (%s)\n", msgcode, requestport, rv, strerror(rv));
-                       _PortPool->PutPort(request->reply_port);
+                       gPortPool->PutPort(request->reply_port);
                        return rv;
                }
        }
 
        rv = read_port_etc(request->reply_port, &code, reply, replysize, 
B_RELATIVE_TIMEOUT, TIMEOUT);
-       _PortPool->PutPort(request->reply_port);
+       gPortPool->PutPort(request->reply_port);
 
        if (rv < B_OK) {
                ERROR("QueryPort: read_port failed, msgcode 0x%lx, port %ld, 
error %#lx (%s)\n", msgcode, request->reply_port, rv, strerror(rv));

Modified: haiku/trunk/src/kits/media/DormantNodeManager.cpp
===================================================================
--- haiku/trunk/src/kits/media/DormantNodeManager.cpp   2009-12-07 21:27:21 UTC 
(rev 34539)
+++ haiku/trunk/src/kits/media/DormantNodeManager.cpp   2009-12-07 21:28:08 UTC 
(rev 34540)
@@ -213,12 +213,12 @@
                return 0;
        }
 
-       msg.reply_port = _PortPool->GetPort();
+       msg.reply_port = gPortPool->GetPort();
        msg.ref = ref;
 
        status = write_port(port, SERVER_REGISTER_MEDIAADDON, &msg, 
sizeof(msg));
        if (status != B_OK) {
-               _PortPool->PutPort(msg.reply_port);
+               gPortPool->PutPort(msg.reply_port);
                ERROR("DormantNodeManager::RegisterAddon failed, couldn't talk 
to "
                        "media server\n");
                return 0;
@@ -228,7 +228,7 @@
        int32 code;
        status = read_port(msg.reply_port, &code, &reply, sizeof(reply));
 
-       _PortPool->PutPort(msg.reply_port);
+       gPortPool->PutPort(msg.reply_port);
 
        if (status < B_OK) {
                ERROR("DormantNodeManager::RegisterAddon failed, couldn't talk 
to "
@@ -269,11 +269,11 @@
 
        server_get_mediaaddon_ref_request msg;
        msg.addon_id = id;
-       msg.reply_port = _PortPool->GetPort();
+       msg.reply_port = gPortPool->GetPort();
        status_t status = write_port(port, SERVER_GET_MEDIAADDON_REF, &msg,
                sizeof(msg));
        if (status != B_OK) {
-               _PortPool->PutPort(msg.reply_port);
+               gPortPool->PutPort(msg.reply_port);
                return status;
        }
 
@@ -281,7 +281,7 @@
        int32 code;
        status = read_port(msg.reply_port, &code, &reply, sizeof(reply));
 
-       _PortPool->PutPort(msg.reply_port);
+       gPortPool->PutPort(msg.reply_port);
 
        if (status < B_OK)
                return status;

Modified: haiku/trunk/src/kits/media/MediaRoster.cpp
===================================================================
--- haiku/trunk/src/kits/media/MediaRoster.cpp  2009-12-07 21:27:21 UTC (rev 
34539)
+++ haiku/trunk/src/kits/media/MediaRoster.cpp  2009-12-07 21:28:08 UTC (rev 
34540)
@@ -1378,14 +1378,14 @@
 
        msg.numer = numer;
        msg.denom = denom;
-       msg.reply_port = _PortPool->GetPort();
+       msg.reply_port = gPortPool->GetPort();
        rv = write_port(producer.node, PRODUCER_SET_PLAY_RATE, &msg, 
sizeof(msg));
        if (rv != B_OK) {
-               _PortPool->PutPort(msg.reply_port);
+               gPortPool->PutPort(msg.reply_port);
                return rv;
        }
        rv = read_port(msg.reply_port, &code, &reply, sizeof(reply));
-       _PortPool->PutPort(msg.reply_port);
+       gPortPool->PutPort(msg.reply_port);
        return (rv < B_OK) ? rv : reply.result;
 }
 
@@ -2298,11 +2298,11 @@
        }
        msg.require_kinds = requireKinds;
        msg.deny_kinds = denyKinds;
-       msg.reply_port = _PortPool->GetPort();
+       msg.reply_port = gPortPool->GetPort();
 
        rv = write_port(port, SERVER_GET_DORMANT_NODES, &msg, sizeof(msg));
        if (rv != B_OK) {
-               _PortPool->PutPort(msg.reply_port);
+               gPortPool->PutPort(msg.reply_port);
                return rv;
        }
 
@@ -2311,7 +2311,7 @@
 
        rv = read_port(msg.reply_port, &code, &reply, sizeof(reply));
        if (rv < B_OK) {
-               _PortPool->PutPort(msg.reply_port);
+               gPortPool->PutPort(msg.reply_port);
                return rv;
        }
 
@@ -2323,7 +2323,7 @@
                if (rv < B_OK)
                        reply.result = rv;
        }
-       _PortPool->PutPort(msg.reply_port);
+       gPortPool->PutPort(msg.reply_port);
 
        return reply.result;
 }
@@ -2624,18 +2624,18 @@
        xfer_server_get_dormant_flavor_info msg;
        msg.addon = addonID;
        msg.flavor_id = flavorID;
-       msg.reply_port = _PortPool->GetPort();
+       msg.reply_port = gPortPool->GetPort();
        status_t status = write_port(port, SERVER_GET_DORMANT_FLAVOR_INFO, &msg,
                sizeof(msg));
        if (status != B_OK) {
                free(reply);
-               _PortPool->PutPort(msg.reply_port);
+               gPortPool->PutPort(msg.reply_port);
                return status;
        }
 
        int32 code;
        status = read_port(msg.reply_port, &code, reply, 16000);
-       _PortPool->PutPort(msg.reply_port);
+       gPortPool->PutPort(msg.reply_port);
 
        if (status < B_OK) {
                free(reply);

Modified: haiku/trunk/src/kits/media/PortPool.cpp
===================================================================
--- haiku/trunk/src/kits/media/PortPool.cpp     2009-12-07 21:27:21 UTC (rev 
34539)
+++ haiku/trunk/src/kits/media/PortPool.cpp     2009-12-07 21:28:08 UTC (rev 
34540)
@@ -1,88 +1,68 @@
-/***********************************************************************
- * Copyright (c) 2002 Marcus Overhagen. All Rights Reserved.
- * This file may be used under the terms of the OpenBeOS License.
- *
- * A pool of kernel ports
- ***********************************************************************/
-#include <OS.h>
-#include <stdlib.h>
-#include "PortPool.h"
-#include "debug.h"
+/*
+ * Copyright 2009, Axel Dörfler, axeld@xxxxxxxxxxxxxxxxx
+ * Distributed under the terms of the MIT License.
+ */
 
-PortPool _ThePortPool;
-PortPool *_PortPool = &_ThePortPool;
 
+#include <PortPool.h>
+
+#include <Autolock.h>
+
+#include <debug.h>
+
+
+namespace BPrivate {
+
+
+static PortPool sPortPool;
+PortPool* gPortPool = &sPortPool;
+
+
 PortPool::PortPool()
+       :
+       BLocker("port pool")
 {
-       locker_atom = 0;
-       locker_sem = create_sem(0,"port pool lock");
-       count = 0;
-       maxcount = 0;
-       pool = 0;
 }
 
+
 PortPool::~PortPool()
 {
-       for (int i = 0; i < maxcount; i++)
-               delete_port(pool[i].port);
-       delete_sem(locker_sem);
-       if (pool)
-               free(pool);
+       PortSet::iterator iterator = fPool.begin();
+
+       for (; iterator != fPool.end(); iterator++)
+               delete_port(*iterator);
 }
 
-port_id 
+
+port_id
 PortPool::GetPort()
 {
-       port_id port = -1;
-       Lock();
-       if (count == maxcount) {
-               maxcount += 3;
-               pool = (PortInfo *)realloc(pool,sizeof(PortInfo) * maxcount);
-               if (pool == NULL)
-                       debugger("out of memory in PortPool::GetPort()\n");
-               for (int i = count; i < maxcount; i++) {
-                       pool[i].used = false;
-                       pool[i].port = create_port(1,"some reply port");
-               }
-       }
-       count++;
-       for (int i = 0; i < maxcount; i++)
-               if (pool[i].used == false) {
-                       port = pool[i].port;
-                       pool[i].used = true;
-                       break;
-               }
-       Unlock();
-       if (port < 0)
-               debugger("wrong port in PortPool::GetPort()\n");
+       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)
 {
-       Lock();
-       count--;
-       for (int i = 0; i < maxcount; i++)
-               if (pool[i].port == port) {
-                       pool[i].used = false;
-                       break;
-               }
-       Unlock();
-}
+       ASSERT(port >= 0);
 
-void
-PortPool::Lock()
-{ 
-       if (atomic_add(&locker_atom, 1) > 0) {
-               while (B_INTERRUPTED == acquire_sem(locker_sem))
-                       ;
+       BAutolock _(this);
+
+       try {
+               fPool.insert(port);
+       } catch (std::bad_alloc& exception) {
+               delete_port(port);
        }
 }
 
-void
-PortPool::Unlock()
-{ 
-       if (atomic_add(&locker_atom, -1) > 1)
-               release_sem(locker_sem);
-}
+
+}      // namespace BPrivate


Other related posts:

  • » [haiku-commits] r34540 - in haiku/trunk: headers/private/media src/kits/media - axeld