From 2f9ed888a24130579b97509580f351ce2620c9b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Sat, 5 Dec 2009 11:52:01 +0000 Subject: [PATCH] * The SharedBufferList is now only cloned once in a team, no longer once for each buffer, and once for each buffer group. * Also, SharedBufferList::Get() now gets the area to clone from itself, if necessary, the caller no longer has to provide it. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34502 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/media/SharedBufferList.h | 3 +- src/kits/media/Buffer.cpp | 11 +---- src/kits/media/BufferGroup.cpp | 13 +----- src/kits/media/MediaRoster.cpp | 24 +++++++---- src/kits/media/SharedBufferList.cpp | 55 ++++++++++++++++++------ 5 files changed, 61 insertions(+), 45 deletions(-) diff --git a/headers/private/media/SharedBufferList.h b/headers/private/media/SharedBufferList.h index ead919ec98..37f8a7efb3 100644 --- a/headers/private/media/SharedBufferList.h +++ b/headers/private/media/SharedBufferList.h @@ -16,7 +16,8 @@ namespace BPrivate { class SharedBufferList { public: static area_id Create(SharedBufferList** _list); - static SharedBufferList* Get(area_id area); + static SharedBufferList* Get(); + static void Invalidate(); void Put(); void DeleteGroupAndPut(sem_id groupReclaimSem); diff --git a/src/kits/media/Buffer.cpp b/src/kits/media/Buffer.cpp index c8781e9cf5..bf84561c3f 100644 --- a/src/kits/media/Buffer.cpp +++ b/src/kits/media/Buffer.cpp @@ -201,16 +201,7 @@ BBuffer::BBuffer(const buffer_clone_info& info) if (info.area == 0 && info.buffer == 0) return; - // ask media_server to get the area_id of the shared buffer list - server_get_shared_buffer_area_request areaRequest; - server_get_shared_buffer_area_reply areaReply; - if (QueryServer(SERVER_GET_SHARED_BUFFER_AREA, &areaRequest, - sizeof(areaRequest), &areaReply, sizeof(areaReply)) != B_OK) { - ERROR("BBuffer::BBuffer: SERVER_GET_SHARED_BUFFER_AREA failed\n"); - return; - } - - fBufferList = BPrivate::SharedBufferList::Get(areaReply.area); + fBufferList = BPrivate::SharedBufferList::Get(); if (fBufferList == NULL) { ERROR("BBuffer::BBuffer: _shared_buffer_list::Clone() failed\n"); return; diff --git a/src/kits/media/BufferGroup.cpp b/src/kits/media/BufferGroup.cpp index 7131551c1e..1656426ade 100644 --- a/src/kits/media/BufferGroup.cpp +++ b/src/kits/media/BufferGroup.cpp @@ -393,18 +393,7 @@ BBufferGroup::_Init() return fInitError; } - // ask media_server to get the area_id of the shared buffer list - server_get_shared_buffer_area_request areaRequest; - server_get_shared_buffer_area_reply areaReply; - if (QueryServer(SERVER_GET_SHARED_BUFFER_AREA, &areaRequest, - sizeof(areaRequest), &areaReply, sizeof(areaReply)) != B_OK) { - ERROR("BBufferGroup::InitBufferGroup: SERVER_GET_SHARED_BUFFER_AREA " - "failed\n"); - fInitError = B_ERROR; - return fInitError; - } - - fBufferList = BPrivate::SharedBufferList::Get(areaReply.area); + fBufferList = BPrivate::SharedBufferList::Get(); if (fBufferList == NULL) { ERROR("BBufferGroup::InitBufferGroup: SharedBufferList::Get() " "failed\n"); diff --git a/src/kits/media/MediaRoster.cpp b/src/kits/media/MediaRoster.cpp index 21914cd33f..b9d3881830 100644 --- a/src/kits/media/MediaRoster.cpp +++ b/src/kits/media/MediaRoster.cpp @@ -57,16 +57,19 @@ char __dont_remove_copyright_from_binary[] = "Copyright (c) 2002-2006 Marcus " #include -#include "debug.h" -#include "MediaRosterEx.h" -#include "MediaMisc.h" -#include "PortPool.h" -#include "ServerInterface.h" -#include "DataExchange.h" -#include "DormantNodeManager.h" -#include "Notifications.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include + #include "TimeSourceObjectManager.h" + namespace BPrivate { namespace media { // the BMediaRoster destructor is private, @@ -3206,6 +3209,8 @@ BMediaRoster::~BMediaRoster() QueryServer(SERVER_UNREGISTER_APP, &request, sizeof(request), &reply, sizeof(reply)); + BPrivate::SharedBufferList::Invalidate(); + // Unset the global instance pointer, the destructor is also called // if a client app calls Lock(); and Quit(); directly. sDefaultInstance = NULL; @@ -3238,7 +3243,8 @@ status_t BMediaRoster::_Reserved_MediaRoster_7(void *) { return B_ERROR; } BMediaRoster::BMediaRoster() - : BLooper("_BMediaRoster_", B_URGENT_DISPLAY_PRIORITY, + : + BLooper("_BMediaRoster_", B_URGENT_DISPLAY_PRIORITY, B_LOOPER_PORT_DEFAULT_CAPACITY) { CALLED(); diff --git a/src/kits/media/SharedBufferList.cpp b/src/kits/media/SharedBufferList.cpp index b7e8ed2ced..2b4786686a 100644 --- a/src/kits/media/SharedBufferList.cpp +++ b/src/kits/media/SharedBufferList.cpp @@ -17,9 +17,18 @@ #include +#include #include +#include -#include "debug.h" +#include +#include + + +static BPrivate::SharedBufferList* sList; +static area_id sArea; +static int32 sRefCount; +static BLocker sLocker("shared buffer list"); namespace BPrivate { @@ -50,21 +59,41 @@ SharedBufferList::Create(SharedBufferList** _list) /*static*/ SharedBufferList* -SharedBufferList::Get(area_id id) +SharedBufferList::Get() { CALLED(); - // TODO: map this only once per team! - SharedBufferList* list; - area_id area = clone_area("shared buffer list clone", (void**)&list, - B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, id); - if (area < 0) { - ERROR("SharedBufferList::Clone() clone area: %ld err = %s\n", id, - strerror(area)); + BAutolock _(sLocker); + + if (atomic_add(&sRefCount, 1) > 0 && sList != NULL) + return sList; + + // ask media_server to get the area_id of the shared buffer list + server_get_shared_buffer_area_request areaRequest; + server_get_shared_buffer_area_reply areaReply; + if (QueryServer(SERVER_GET_SHARED_BUFFER_AREA, &areaRequest, + sizeof(areaRequest), &areaReply, sizeof(areaReply)) != B_OK) { + ERROR("SharedBufferList::Get() SERVER_GET_SHARED_BUFFER_AREA failed\n"); return NULL; } - return list; + sArea = clone_area("shared buffer list clone", (void**)&sList, + B_ANY_ADDRESS, B_READ_AREA | B_WRITE_AREA, areaReply.area); + if (sArea < 0) { + ERROR("SharedBufferList::Get() clone area %ld: %s\n", + areaReply.area, strerror(sArea)); + return NULL; + } + + return sList; +} + + +/*static*/ void +SharedBufferList::Invalidate() +{ + delete_area(sArea); + sList = NULL; } @@ -72,10 +101,10 @@ void SharedBufferList::Put() { CALLED(); + BAutolock _(sLocker); - area_id area = area_for(this); - if (area >= 0) - delete_area(area); + if (atomic_add(&sRefCount, -1) == 1) + Invalidate(); }