- cleanups

This commit is contained in:
2019-03-21 18:40:05 +02:00
parent 3fa88ff9d4
commit 14b034e94c
4 changed files with 69 additions and 42 deletions

View File

@@ -8,26 +8,46 @@ template <class T>
class SafeSingleton
{
protected:
static std::atomic<T*> SharedInstance;
static std::mutex mMutex;
static std::atomic<T*> SharedInstance;
static std::mutex mMutex;
public:
static T& instance()
{
T* tmp = SharedInstance.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (tmp == nullptr)
static T& instance()
{
std::lock_guard<std::mutex> lock(mMutex);
tmp = SharedInstance.load(std::memory_order_relaxed);
T* tmp = SharedInstance.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (tmp == nullptr)
{
tmp = new T();
std::atomic_thread_fence(std::memory_order_release);
SharedInstance.store(tmp, std::memory_order_relaxed);
std::lock_guard<std::mutex> lock(mMutex);
tmp = SharedInstance.load(std::memory_order_relaxed);
if (tmp == nullptr)
{
tmp = new T();
std::atomic_thread_fence(std::memory_order_release);
SharedInstance.store(tmp, std::memory_order_relaxed);
}
}
return *tmp;
}
template<typename X>
static T& precreate(X n)
{
T* tmp = SharedInstance.load(std::memory_order_relaxed);
std::atomic_thread_fence(std::memory_order_acquire);
if (tmp == nullptr)
{
std::lock_guard<std::mutex> lock(mMutex);
tmp = SharedInstance.load(std::memory_order_relaxed);
if (tmp == nullptr)
{
tmp = new T(n);
std::atomic_thread_fence(std::memory_order_release);
SharedInstance.store(tmp, std::memory_order_relaxed);
return *tmp;
}
}
throw std::runtime_error("Singletone instance is created already");
}
return *tmp;
}
};
template <class T>

View File

@@ -1,4 +1,4 @@
/* Copyright(C) 2007-2018 VoIPobjects (voipobjects.com)
/* Copyright(C) 2007-2019 VoIPobjects (voipobjects.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
@@ -99,7 +99,8 @@ protected:
// - Handlers are ALWAYS executed in the Timer Queue worker thread.
// - Handlers execution order is NOT guaranteed
//
class TimerQueue {
class TimerQueue
{
public:
TimerQueue();
~TimerQueue();
@@ -135,7 +136,8 @@ private:
bool m_finish = false;
uint64_t m_idcounter = 0;
struct WorkItem {
struct WorkItem
{
Clock::time_point end;
uint64_t id; // id==0 means it was cancelled
std::function<void(bool)> handler;
@@ -145,7 +147,8 @@ private:
std::mutex m_mtx;
// Inheriting from priority_queue, so we can access the internal container
class Queue : public std::priority_queue<WorkItem, std::vector<WorkItem>,
std::greater<WorkItem>> {
std::greater<WorkItem>>
{
public:
std::vector<WorkItem>& getContainer();
} m_items;