mirror of
https://github.com/OPM/ResInsight.git
synced 2025-01-01 03:37:15 -06:00
#11072 Refactor: remove homemade atomic counter.
std::atomic was introduced in C++11, and provides same functionality in platform-independent way. Fixes #11072.
This commit is contained in:
parent
c759a11888
commit
3255b649ca
@ -725,34 +725,6 @@ endif()
|
||||
# Vizualization Framework
|
||||
# ##############################################################################
|
||||
|
||||
# Allow use of non-threadsafe reference counter in cvf::Object on systems with
|
||||
# no atomics support
|
||||
if(CMAKE_COMPILER_IS_GNUCC)
|
||||
|
||||
if(NOT DEFINED HAVE_GCC_SYNC_FUNCTIONS)
|
||||
check_c_source_compiles(
|
||||
"int main(int argc, char **argv) {
|
||||
int a;
|
||||
__sync_add_and_fetch(&a, 1);
|
||||
__sync_fetch_and_add(&a, 1);
|
||||
__sync_sub_and_fetch(&a, 1);
|
||||
__sync_fetch_and_sub(&a, 1); }"
|
||||
HAVE_GCC_SYNC_FUNCTIONS
|
||||
)
|
||||
endif()
|
||||
|
||||
if(HAVE_GCC_SYNC_FUNCTIONS)
|
||||
message(STATUS "GCC synchronization functions detected")
|
||||
else()
|
||||
message(
|
||||
STATUS
|
||||
"GCC synchronization functions NOT detected, fallback to non threadsafe reference counting"
|
||||
)
|
||||
add_definitions(-DCVF_USE_NON_THREADSAFE_REFERENCE_COUNT)
|
||||
endif()
|
||||
|
||||
endif()
|
||||
|
||||
# !!! For now, we force Qt to version 5
|
||||
message(STATUS "Forcing setting of CEE_USE_QT5 to ON")
|
||||
set(CEE_USE_QT5
|
||||
|
@ -19,7 +19,6 @@ cvfArray.inl
|
||||
cvfArrayWrapperConst.h
|
||||
cvfArrayWrapperToEdit.h
|
||||
cvfAssert.h
|
||||
cvfAtomicCounter.h
|
||||
cvfBase.h
|
||||
cvfBase64.h
|
||||
cvfCharArray.h
|
||||
@ -75,7 +74,6 @@ cvfVersion.h
|
||||
|
||||
set(CEE_SOURCE_FILES
|
||||
cvfAssert.cpp
|
||||
cvfAtomicCounter.cpp
|
||||
cvfBase64.cpp
|
||||
cvfCharArray.cpp
|
||||
cvfCodeLocation.cpp
|
||||
|
@ -1,187 +0,0 @@
|
||||
//##################################################################################################
|
||||
//
|
||||
// Custom Visualization Core library
|
||||
// Copyright (C) 2014 Ceetron Solutions AS
|
||||
//
|
||||
// This library may be used under the terms of either the GNU General Public License or
|
||||
// the GNU Lesser General Public License as follows:
|
||||
//
|
||||
// GNU General Public License Usage
|
||||
// This library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
|
||||
// for more details.
|
||||
//
|
||||
// GNU Lesser General Public License Usage
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
|
||||
// for more details.
|
||||
//
|
||||
//##################################################################################################
|
||||
|
||||
|
||||
#include "cvfAtomicCounter.h"
|
||||
|
||||
// Some older GCC version do not support atomics, we have seen this for RHEL5
|
||||
#if defined(CVF_ATOMIC_COUNTER_CLASS_EXISTS)
|
||||
|
||||
namespace cvf {
|
||||
|
||||
#ifdef WIN32
|
||||
#pragma warning (push)
|
||||
#pragma warning (disable: 4668)
|
||||
#include <windows.h>
|
||||
#pragma warning (pop)
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(int initialValue)
|
||||
: m_counter(initialValue)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::~AtomicCounter()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::operator int () const
|
||||
{
|
||||
return m_counter;
|
||||
}
|
||||
|
||||
int AtomicCounter::operator ++ () // prefix
|
||||
{
|
||||
return InterlockedIncrement(&m_counter);
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator ++ (int) // postfix
|
||||
{
|
||||
int result = InterlockedIncrement(&m_counter);
|
||||
return --result;
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator -- () // prefix
|
||||
{
|
||||
return InterlockedDecrement(&m_counter);
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator -- (int) // postfix
|
||||
{
|
||||
int result = InterlockedDecrement(&m_counter);
|
||||
return ++result;
|
||||
}
|
||||
|
||||
|
||||
#elif defined(CVF_IOS) || defined(CVF_OSX)
|
||||
|
||||
AtomicCounter::AtomicCounter(int initialValue)
|
||||
: m_counter(initialValue)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
AtomicCounter::~AtomicCounter()
|
||||
{
|
||||
}
|
||||
|
||||
AtomicCounter::operator int () const
|
||||
{
|
||||
return m_counter;
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator ++ () // prefix
|
||||
{
|
||||
return OSAtomicIncrement32(&m_counter);
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator ++ (int) // postfix
|
||||
{
|
||||
int result = OSAtomicIncrement32(&m_counter);
|
||||
return --result;
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator -- () // prefix
|
||||
{
|
||||
return OSAtomicDecrement32(&m_counter);
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator -- (int) // postfix
|
||||
{
|
||||
int result = OSAtomicDecrement32(&m_counter);
|
||||
return ++result;
|
||||
}
|
||||
|
||||
|
||||
#elif defined(CVF_GCC_DEFINED)
|
||||
|
||||
|
||||
AtomicCounter::AtomicCounter(int initialValue)
|
||||
: m_counter(initialValue)
|
||||
{
|
||||
}
|
||||
|
||||
AtomicCounter::~AtomicCounter()
|
||||
{
|
||||
}
|
||||
|
||||
AtomicCounter::operator int () const
|
||||
{
|
||||
return m_counter;
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator ++ () // prefix
|
||||
{
|
||||
return __sync_add_and_fetch(&m_counter, 1);
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator ++ (int) // postfix
|
||||
{
|
||||
return __sync_fetch_and_add(&m_counter, 1);
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator -- () // prefix
|
||||
{
|
||||
return __sync_sub_and_fetch(&m_counter, 1);
|
||||
}
|
||||
|
||||
|
||||
int AtomicCounter::operator -- (int) // postfix
|
||||
{
|
||||
return __sync_fetch_and_sub(&m_counter, 1);
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
} // namespace cvf
|
||||
|
||||
|
||||
|
||||
#endif // CVF_ATOMICS_COMPILED
|
@ -1,91 +0,0 @@
|
||||
//##################################################################################################
|
||||
//
|
||||
// Custom Visualization Core library
|
||||
// Copyright (C) 2014 Ceetron Solutions AS
|
||||
//
|
||||
// This library may be used under the terms of either the GNU General Public License or
|
||||
// the GNU Lesser General Public License as follows:
|
||||
//
|
||||
// GNU General Public License Usage
|
||||
// This library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
|
||||
// for more details.
|
||||
//
|
||||
// GNU Lesser General Public License Usage
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
|
||||
// for more details.
|
||||
//
|
||||
//##################################################################################################
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "cvfBase.h"
|
||||
|
||||
#ifdef WIN32
|
||||
#define CVF_ATOMIC_COUNTER_CLASS_EXISTS
|
||||
#elif defined(CVF_IOS) || defined(CVF_OSX)
|
||||
#include <libkern/OSAtomic.h>
|
||||
#define CVF_ATOMIC_COUNTER_CLASS_EXISTS
|
||||
#elif defined __GNUC__
|
||||
#define CVF_GCC_DEFINED
|
||||
#define CVF_ATOMIC_COUNTER_CLASS_EXISTS
|
||||
#endif
|
||||
|
||||
#if defined(CVF_ATOMIC_COUNTER_CLASS_EXISTS)
|
||||
|
||||
namespace cvf {
|
||||
|
||||
// Inspired by Poco
|
||||
|
||||
|
||||
class AtomicCounter
|
||||
{
|
||||
public:
|
||||
explicit AtomicCounter(int initialValue);
|
||||
~AtomicCounter();
|
||||
|
||||
operator int () const;
|
||||
|
||||
int operator ++ (); // prefix
|
||||
int operator ++ (int); // postfix
|
||||
|
||||
int operator -- (); // prefix
|
||||
int operator -- (int); // postfix
|
||||
|
||||
private:
|
||||
|
||||
CVF_DISABLE_COPY_AND_ASSIGN(AtomicCounter);
|
||||
|
||||
#ifdef WIN32
|
||||
typedef volatile long ImplType;
|
||||
#elif defined(CVF_IOS) || defined(CVF_OSX)
|
||||
typedef int32_t ImplType;
|
||||
#else
|
||||
typedef int ImplType;
|
||||
#endif
|
||||
|
||||
ImplType m_counter;
|
||||
};
|
||||
|
||||
|
||||
} // namespace cvf
|
||||
|
||||
#endif
|
@ -41,12 +41,8 @@
|
||||
#include "cvfSystem.h"
|
||||
|
||||
#include <set>
|
||||
#include <atomic>
|
||||
|
||||
#include "cvfAtomicCounter.h"
|
||||
|
||||
#if !defined(CVF_ATOMIC_COUNTER_CLASS_EXISTS) && !defined(CVF_USE_NON_THREADSAFE_REFERENCE_COUNT)
|
||||
#error No support for atomics. Define CVF_USE_NON_THREADSAFE_REFERENCE_COUNT to be able to compile
|
||||
#endif
|
||||
|
||||
namespace cvf {
|
||||
|
||||
@ -71,15 +67,7 @@ public:
|
||||
static void dumpActiveObjectInstances();
|
||||
|
||||
private:
|
||||
|
||||
#if defined(CVF_USE_NON_THREADSAFE_REFERENCE_COUNT)
|
||||
mutable int m_refCount;
|
||||
#elif defined(CVF_ATOMIC_COUNTER_CLASS_EXISTS)
|
||||
mutable AtomicCounter m_refCount;
|
||||
#else
|
||||
#error No support for atomics. Define CVF_USE_NON_THREADSAFE_REFERENCE_COUNT to be able to compile
|
||||
#endif
|
||||
|
||||
mutable std::atomic<int> m_refCount;
|
||||
|
||||
CVF_DISABLE_COPY_AND_ASSIGN(Object);
|
||||
};
|
||||
|
@ -12,7 +12,6 @@ set(CEE_LIBS LibCore)
|
||||
set(CEE_SOURCE_FILES
|
||||
cvfArray-Test.cpp
|
||||
cvfArrayWrapper-Test.cpp
|
||||
cvfAtomicCounter-Test.cpp
|
||||
cvfBase-Test.cpp
|
||||
cvfBase64-Test.cpp
|
||||
cvfCharArray-Test.cpp
|
||||
|
@ -1,141 +0,0 @@
|
||||
//##################################################################################################
|
||||
//
|
||||
// Custom Visualization Core library
|
||||
// Copyright (C) 2014 Ceetron Solutions AS
|
||||
//
|
||||
// This library may be used under the terms of either the GNU General Public License or
|
||||
// the GNU Lesser General Public License as follows:
|
||||
//
|
||||
// GNU General Public License Usage
|
||||
// This library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU General Public License at <<http://www.gnu.org/licenses/gpl.html>>
|
||||
// for more details.
|
||||
//
|
||||
// GNU Lesser General Public License Usage
|
||||
// This library is free software; you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation; either version 2.1 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful, but WITHOUT ANY
|
||||
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE.
|
||||
//
|
||||
// See the GNU Lesser General Public License at <<http://www.gnu.org/licenses/lgpl-2.1.html>>
|
||||
// for more details.
|
||||
//
|
||||
//##################################################################################################
|
||||
|
||||
|
||||
|
||||
#include "cvfAtomicCounter.h"
|
||||
|
||||
#ifdef CVF_ATOMIC_COUNTER_CLASS_EXISTS
|
||||
|
||||
#include "cvfDebugTimer.h"
|
||||
#include "cvfObject.h"
|
||||
#include "cvfCollection.h"
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
using namespace cvf;
|
||||
|
||||
class MyObj : public Object
|
||||
{
|
||||
public:
|
||||
MyObj() { num_ = 0; }
|
||||
MyObj(int num) { num_ = num; }
|
||||
|
||||
int num() const { return num_; }
|
||||
void num(int num) { num_ = num; }
|
||||
|
||||
bool operator<(const MyObj& rhs)
|
||||
{
|
||||
return num_ < rhs.num_;
|
||||
}
|
||||
|
||||
private:
|
||||
int num_;
|
||||
};
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(DISABLED_ObjectConstructionBenchmark, TestBasicObjectConstruction)
|
||||
{
|
||||
int objectCount = 1000000;
|
||||
int iterationCount = 5;
|
||||
|
||||
String sNumber(objectCount);
|
||||
String refCountTxt = String("TestBasicObjectConstruction : ") + sNumber;
|
||||
DebugTimer tim(refCountTxt.toAscii().ptr());
|
||||
|
||||
for (int iteration = 0; iteration < iterationCount; iteration++)
|
||||
{
|
||||
for (int i = 0; i < objectCount; i++)
|
||||
{
|
||||
MyObj* r2 = new MyObj();
|
||||
|
||||
r2->addRef();
|
||||
r2->release();
|
||||
}
|
||||
|
||||
tim.reportLapTimeMS();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class ObjectReferencingSharedObject : public Object
|
||||
{
|
||||
public:
|
||||
ObjectReferencingSharedObject() { }
|
||||
|
||||
cvf::ref<MyObj> m_sharedObject;
|
||||
};
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(DISABLED_ObjectConstructionBenchmark, TestReferenceOtherObject)
|
||||
{
|
||||
int objectCount = 1000000;
|
||||
int iterationCount = 5;
|
||||
|
||||
String sNumber(objectCount);
|
||||
String refCountTxt = String("TestReferenceOtherObjectClass : ") + sNumber;
|
||||
DebugTimer tim(refCountTxt.toAscii().ptr());
|
||||
|
||||
for (int iteration = 0; iteration < iterationCount; iteration++)
|
||||
{
|
||||
cvf::ref<MyObj> sharedObj = new MyObj();
|
||||
|
||||
std::vector< cvf::ref<ObjectReferencingSharedObject> > col;
|
||||
col.resize(objectCount);
|
||||
|
||||
for (int i = 0; i < objectCount; i++)
|
||||
{
|
||||
cvf::ref<ObjectReferencingSharedObject> newObj = new ObjectReferencingSharedObject();
|
||||
newObj->m_sharedObject = sharedObj.p();
|
||||
|
||||
col[i] = newObj;
|
||||
}
|
||||
|
||||
String sNumber(sharedObj->refCount());
|
||||
String refCountTxt = String("Shared object reference count : ") + sNumber;
|
||||
|
||||
tim.reportLapTimeMS(refCountTxt.toAscii().ptr());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#endif //#ifdef CVF_ATOMIC_COUNTER_CLASS_EXISTS
|
Loading…
Reference in New Issue
Block a user