Turned the loaded images map into a singleton.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34368 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Ingo Weinhold
2009-11-30 11:40:20 +00:00
parent adcb834590
commit 4a6fb0a865
+65 -12
View File
@@ -1,5 +1,5 @@
/* /*
* Copyright 2001-2009, Haiku. * Copyright 2001-2009, Haiku, Inc.
* Distributed under the terms of the MIT License. * Distributed under the terms of the MIT License.
* *
* Authors: * Authors:
@@ -12,6 +12,10 @@
/*! BShelf stores replicant views that are dropped onto it */ /*! BShelf stores replicant views that are dropped onto it */
#include <Shelf.h>
#include <pthread.h>
#include <AutoLock.h> #include <AutoLock.h>
#include <Beep.h> #include <Beep.h>
#include <Dragger.h> #include <Dragger.h>
@@ -24,7 +28,6 @@
#include <Point.h> #include <Point.h>
#include <PropertyInfo.h> #include <PropertyInfo.h>
#include <Rect.h> #include <Rect.h>
#include <Shelf.h>
#include <String.h> #include <String.h>
#include <View.h> #include <View.h>
@@ -39,9 +42,55 @@
#include <utility> #include <utility>
namespace {
typedef std::map<BString, std::pair<image_id, int32> > LoadedImageMap; typedef std::map<BString, std::pair<image_id, int32> > LoadedImageMap;
static LoadedImageMap sLoadedImages;
static BLocker sLoadedImageMapLocker("BShelf loaded image map"); struct LoadedImages {
LoadedImageMap images;
LoadedImages()
:
fLock("BShelf loaded image map")
{
}
bool Lock()
{
return fLock.Lock();
}
void Unlock()
{
fLock.Unlock();
}
static LoadedImages* Default()
{
if (sDefaultInstance == NULL)
pthread_once(&sDefaultInitOnce, &_InitSingleton);
return sDefaultInstance;
}
private:
static void _InitSingleton()
{
sDefaultInstance = new LoadedImages;
}
private:
BLocker fLock;
static pthread_once_t sDefaultInitOnce;
static LoadedImages* sDefaultInstance;
};
pthread_once_t LoadedImages::sDefaultInitOnce = PTHREAD_ONCE_INIT;
LoadedImages* LoadedImages::sDefaultInstance = NULL;
} // unnamed namespace
static property_info sShelfPropertyList[] = { static property_info sShelfPropertyList[] = {
{ {
@@ -1167,15 +1216,17 @@ BShelf::_DeleteReplicant(replicant_data* item)
const char* signature = NULL; const char* signature = NULL;
if (item->message->FindString("add_on", &signature) == B_OK if (item->message->FindString("add_on", &signature) == B_OK
&& signature != NULL) { && signature != NULL) {
AutoLock<BLocker> lock(sLoadedImageMapLocker); LoadedImages* loadedImages = LoadedImages::Default();
AutoLock<LoadedImages> lock(loadedImages);
if (lock.IsLocked()) { if (lock.IsLocked()) {
LoadedImageMap::iterator it = sLoadedImages.find(BString(signature)); LoadedImageMap::iterator it = loadedImages->images.find(
BString(signature));
if (it != sLoadedImages.end()) { if (it != loadedImages->images.end()) {
(*it).second.second--; (*it).second.second--;
if ((*it).second.second <= 0) { if ((*it).second.second <= 0) {
unload_add_on((*it).second.first); unload_add_on((*it).second.first);
sLoadedImages.erase(it); loadedImages->images.erase(it);
} }
} }
} }
@@ -1264,12 +1315,14 @@ BShelf::_AddReplicant(BMessage *data, BPoint *location, uint32 uniqueID)
// Update use count for image // Update use count for image
const char* signature = NULL; const char* signature = NULL;
if (data->FindString("add_on", &signature) == B_OK && signature != NULL) { if (data->FindString("add_on", &signature) == B_OK && signature != NULL) {
AutoLock<BLocker> lock(sLoadedImageMapLocker); LoadedImages* loadedImages = LoadedImages::Default();
AutoLock<LoadedImages> lock(loadedImages);
if (lock.IsLocked()) { if (lock.IsLocked()) {
LoadedImageMap::iterator it = sLoadedImages.find(BString(signature)); LoadedImageMap::iterator it = loadedImages->images.find(
BString(signature));
if (it == sLoadedImages.end()) if (it == loadedImages->images.end())
sLoadedImages.insert(LoadedImageMap::value_type( loadedImages->images.insert(LoadedImageMap::value_type(
BString(signature), std::pair<image_id, int>(image, 1))); BString(signature), std::pair<image_id, int>(image, 1)));
else else
(*it).second.second++; (*it).second.second++;