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:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2001-2009, Haiku.
|
||||
* Copyright 2001-2009, Haiku, Inc.
|
||||
* Distributed under the terms of the MIT License.
|
||||
*
|
||||
* Authors:
|
||||
@@ -12,6 +12,10 @@
|
||||
|
||||
/*! BShelf stores replicant views that are dropped onto it */
|
||||
|
||||
#include <Shelf.h>
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#include <AutoLock.h>
|
||||
#include <Beep.h>
|
||||
#include <Dragger.h>
|
||||
@@ -24,7 +28,6 @@
|
||||
#include <Point.h>
|
||||
#include <PropertyInfo.h>
|
||||
#include <Rect.h>
|
||||
#include <Shelf.h>
|
||||
#include <String.h>
|
||||
#include <View.h>
|
||||
|
||||
@@ -39,9 +42,55 @@
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace {
|
||||
|
||||
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[] = {
|
||||
{
|
||||
@@ -1167,15 +1216,17 @@ BShelf::_DeleteReplicant(replicant_data* item)
|
||||
const char* signature = NULL;
|
||||
if (item->message->FindString("add_on", &signature) == B_OK
|
||||
&& signature != NULL) {
|
||||
AutoLock<BLocker> lock(sLoadedImageMapLocker);
|
||||
LoadedImages* loadedImages = LoadedImages::Default();
|
||||
AutoLock<LoadedImages> lock(loadedImages);
|
||||
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--;
|
||||
if ((*it).second.second <= 0) {
|
||||
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
|
||||
const char* 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()) {
|
||||
LoadedImageMap::iterator it = sLoadedImages.find(BString(signature));
|
||||
LoadedImageMap::iterator it = loadedImages->images.find(
|
||||
BString(signature));
|
||||
|
||||
if (it == sLoadedImages.end())
|
||||
sLoadedImages.insert(LoadedImageMap::value_type(
|
||||
if (it == loadedImages->images.end())
|
||||
loadedImages->images.insert(LoadedImageMap::value_type(
|
||||
BString(signature), std::pair<image_id, int>(image, 1)));
|
||||
else
|
||||
(*it).second.second++;
|
||||
|
||||
Reference in New Issue
Block a user