From b429df54d297c79ddbfe1f3267cb07d8a2f6a23e Mon Sep 17 00:00:00 2001 From: Ingo Weinhold Date: Mon, 30 Nov 2009 16:33:41 +0000 Subject: [PATCH] Use pthread_once() to guard the singleton initialization. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@34384 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- .../disk_device/DiskSystemAddOnManager.cpp | 31 +++++++------------ .../disk_device/DiskSystemAddOnManager.h | 2 ++ 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/kits/storage/disk_device/DiskSystemAddOnManager.cpp b/src/kits/storage/disk_device/DiskSystemAddOnManager.cpp index 35ad85c56e..e6ec607506 100644 --- a/src/kits/storage/disk_device/DiskSystemAddOnManager.cpp +++ b/src/kits/storage/disk_device/DiskSystemAddOnManager.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2007, Ingo Weinhold, bonefish@users.sf.net. + * Copyright 2007-2009, Ingo Weinhold, bonefish@users.sf.net. * Distributed under the terms of the MIT License. */ @@ -17,7 +17,6 @@ #include #include -#include #include #include @@ -32,7 +31,7 @@ using std::nothrow; -// sManager +static pthread_once_t sManagerInitOnce = PTHREAD_ONCE_INIT; DiskSystemAddOnManager* DiskSystemAddOnManager::sManager = NULL; @@ -78,22 +77,8 @@ struct DiskSystemAddOnManager::StringSet : std::set { DiskSystemAddOnManager* DiskSystemAddOnManager::Default() { - if (!sManager) { - DiskSystemAddOnManager* manager = new DiskSystemAddOnManager(); - - BPrivate::gInitializationLock.Lock(); - - // set manager, if no one beat us to it - if (!sManager) { - sManager = manager; - manager = NULL; - } - - BPrivate::gInitializationLock.Unlock(); - - // delete the object we created, if someone else was quicker - delete manager; - } + if (sManager == NULL) + pthread_once(&sManagerInitOnce, &_InitSingleton); return sManager; } @@ -238,6 +223,13 @@ DiskSystemAddOnManager::DiskSystemAddOnManager() } +/*static*/ void +DiskSystemAddOnManager::_InitSingleton() +{ + sManager = new DiskSystemAddOnManager(); +} + + // _AddOnAt DiskSystemAddOnManager::AddOn* DiskSystemAddOnManager::_AddOnAt(int32 index) const @@ -369,4 +361,3 @@ DiskSystemAddOnManager::_LoadAddOns(StringSet& alreadyLoaded, return B_OK; } - diff --git a/src/kits/storage/disk_device/DiskSystemAddOnManager.h b/src/kits/storage/disk_device/DiskSystemAddOnManager.h index 65fe70c45b..a7798e54c5 100644 --- a/src/kits/storage/disk_device/DiskSystemAddOnManager.h +++ b/src/kits/storage/disk_device/DiskSystemAddOnManager.h @@ -42,6 +42,8 @@ private: DiskSystemAddOnManager(); + static void _InitSingleton(); + AddOn* _AddOnAt(int32 index) const; void _PutAddOn(int32 index);