Skip to content

Commit 6e08c92

Browse files
committed
Derive singleton BasicCCDBManager from identical non-singleton class
The BasicCCDBManager with caching enabled is not thread safe since the cache might be cleaned in one thread while the objects stored there are still in use by other thread. In could use multiple instances of the manager, one per element of threads pool, but currently the BasicCCDBManager is a singleton. Therefore, all its functionality is delegated to non-singleton class CCDBManagerInstance while the BasicCCDBManager is derived from the CCDBManagerInstance, hiding its c-tor as private, so it is still a singleton
1 parent 7f690ec commit 6e08c92

File tree

2 files changed

+28
-13
lines changed

2 files changed

+28
-13
lines changed

CCDB/include/CCDB/BasicCCDBManager.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,17 @@
2626
namespace o2::ccdb
2727
{
2828

29-
/// A simple (singleton) class offering simplified access to CCDB (mainly for MC simulation)
29+
/// A simple class offering simplified access to CCDB (mainly for MC simulation)
3030
/// The class encapsulates timestamp and URL and is easily usable from detector code.
31-
class BasicCCDBManager
31+
///
32+
/// The CDBManager allowing caching of retrieved objects is by definition not thread safe,
33+
/// therefore, to provide a possibility of multithread processing, one should foresee possibility
34+
/// of multiple instances of the manager. CCDBManagerInstance serves to this purpose
35+
///
36+
/// In cases where caching is not needed or just 1 instance of the manager is enough, one case use
37+
/// a singleton version BasicCCDBManager
38+
39+
class CCDBManagerInstance
3240
{
3341
struct CachedObject {
3442
std::shared_ptr<void> objPtr;
@@ -39,11 +47,9 @@ class BasicCCDBManager
3947
};
4048

4149
public:
42-
static BasicCCDBManager& instance()
50+
CCDBManagerInstance(std::string const& path) : mCCDBAccessor{}
4351
{
44-
const std::string ccdbUrl{"http://ccdb-test.cern.ch:8080"};
45-
static BasicCCDBManager inst{ccdbUrl};
46-
return inst;
52+
mCCDBAccessor.init(path);
4753
}
4854

4955
/// set a URL to query from
@@ -120,11 +126,6 @@ class BasicCCDBManager
120126
void resetCreatedNotBefore() { mCreatedNotBefore = 0; }
121127

122128
private:
123-
BasicCCDBManager(std::string const& path) : mCCDBAccessor{}
124-
{
125-
mCCDBAccessor.init(path);
126-
}
127-
128129
// we access the CCDB via the CURL based C++ API
129130
o2::ccdb::CcdbApi mCCDBAccessor;
130131
std::unordered_map<std::string, CachedObject> mCache; //! map for {path, CachedObject} associations
@@ -139,7 +140,7 @@ class BasicCCDBManager
139140
};
140141

141142
template <typename T>
142-
T* BasicCCDBManager::getForTimeStamp(std::string const& path, long timestamp)
143+
T* CCDBManagerInstance::getForTimeStamp(std::string const& path, long timestamp)
143144
{
144145
if (!isCachingEnabled()) {
145146
return mCCDBAccessor.retrieveFromTFileAny<T>(path, mMetaData, timestamp, nullptr, "",
@@ -168,6 +169,20 @@ T* BasicCCDBManager::getForTimeStamp(std::string const& path, long timestamp)
168169
return ptr;
169170
}
170171

172+
class BasicCCDBManager : public CCDBManagerInstance
173+
{
174+
public:
175+
static BasicCCDBManager& instance()
176+
{
177+
const std::string ccdbUrl{"http://ccdb-test.cern.ch:8080"};
178+
static BasicCCDBManager inst{ccdbUrl};
179+
return inst;
180+
}
181+
182+
private:
183+
using CCDBManagerInstance::CCDBManagerInstance;
184+
};
185+
171186
} // namespace o2::ccdb
172187

173188
#endif //O2_BASICCCDBMANAGER_H

CCDB/src/BasicCCDBManager.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace o2
1919
namespace ccdb
2020
{
2121

22-
void BasicCCDBManager::setURL(std::string const& url)
22+
void CCDBManagerInstance::setURL(std::string const& url)
2323
{
2424
mCCDBAccessor.init(url);
2525
}

0 commit comments

Comments
 (0)