Made use of atomic variables

In order to replace mutex-protected integer values (Performance
improvement)


Former-commit-id: d40ed2444111f00dab372f2371fe91cfd9cd2472
This commit is contained in:
Lynix
2013-05-03 23:12:58 +02:00
parent aa1399b750
commit 459c7a4d07
8 changed files with 41 additions and 118 deletions

View File

@@ -54,11 +54,7 @@ NzResource(image),
m_sharedImage(image.m_sharedImage)
{
if (m_sharedImage != &emptyImage)
{
NazaraMutexLock(m_sharedImage->mutex);
m_sharedImage->refCount++;
NazaraMutexUnlock(m_sharedImage->mutex);
}
}
NzImage::NzImage(NzImage&& image) noexcept :
@@ -1220,11 +1216,7 @@ NzImage& NzImage::operator=(const NzImage& image)
m_sharedImage = image.m_sharedImage;
if (m_sharedImage != &emptyImage)
{
NazaraMutexLock(m_sharedImage->mutex);
m_sharedImage->refCount++;
NazaraMutexUnlock(m_sharedImage->mutex);
}
return *this;
}
@@ -1294,7 +1286,6 @@ void NzImage::EnsureOwnership()
if (m_sharedImage == &emptyImage)
return;
NazaraLock(m_sharedImage->mutex);
if (m_sharedImage->refCount > 1)
{
m_sharedImage->refCount--;
@@ -1316,11 +1307,7 @@ void NzImage::ReleaseImage()
if (m_sharedImage == &emptyImage)
return;
NazaraMutexLock(m_sharedImage->mutex);
bool freeSharedImage = (--m_sharedImage->refCount == 0);
NazaraMutexUnlock(m_sharedImage->mutex);
if (freeSharedImage)
if (--m_sharedImage->refCount == 0)
{
for (unsigned int i = 0; i < m_sharedImage->levelCount; ++i)
delete[] m_sharedImage->pixels[i];

View File

@@ -6,6 +6,7 @@
#include <Nazara/Core/Error.hpp>
#include <Nazara/Utility/Config.hpp>
#include <algorithm>
#include <atomic>
#include <cstring>
#include <stdexcept>
#include <vector>
@@ -65,13 +66,17 @@ namespace
struct NzVertexDeclarationImpl
{
NzVertexDeclarationImpl() :
refCount(1)
{
}
std::vector<NzVertexElement> elements;
int elementPos[nzElementStream_Max+1][nzElementUsage_Max+1];
int streamPos[nzElementStream_Max+1];
unsigned int stride[nzElementStream_Max+1] = {0};
unsigned short refCount = 1;
NazaraMutex(mutex)
std::atomic_ushort refCount;
};
NzVertexDeclaration::NzVertexDeclaration(const NzVertexElement* elements, unsigned int elementCount)
@@ -92,11 +97,7 @@ NzResource(),
m_sharedImpl(declaration.m_sharedImpl)
{
if (m_sharedImpl)
{
NazaraMutexLock(m_sharedImpl->mutex);
m_sharedImpl->refCount++;
NazaraMutexUnlock(m_sharedImpl->mutex);
}
}
NzVertexDeclaration::NzVertexDeclaration(NzVertexDeclaration&& declaration) noexcept :
@@ -126,7 +127,7 @@ bool NzVertexDeclaration::Create(const NzVertexElement* elements, unsigned int e
std::memset(&impl->elementPos, -1, (nzElementStream_Max+1)*(nzElementUsage_Max+1)*sizeof(int));
std::memset(&impl->streamPos, -1, (nzElementStream_Max+1)*sizeof(int));
// On copie et trie les éléments
// On copie et trions les éléments
impl->elements.resize(elementCount);
std::memcpy(&impl->elements[0], elements, elementCount*sizeof(NzVertexElement));
std::sort(impl->elements.begin(), impl->elements.end(), VertexElementCompare);
@@ -177,11 +178,7 @@ void NzVertexDeclaration::Destroy()
NotifyDestroy();
NazaraMutexLock(m_sharedImpl->mutex);
bool freeSharedImpl = (--m_sharedImpl->refCount == 0);
NazaraMutexUnlock(m_sharedImpl->mutex);
if (freeSharedImpl)
if (--m_sharedImpl->refCount == 0)
delete m_sharedImpl;
m_sharedImpl = nullptr;
@@ -374,11 +371,7 @@ NzVertexDeclaration& NzVertexDeclaration::operator=(const NzVertexDeclaration& d
m_sharedImpl = declaration.m_sharedImpl;
if (m_sharedImpl)
{
NazaraMutexLock(m_sharedImpl->mutex);
m_sharedImpl->refCount++;
NazaraMutexUnlock(m_sharedImpl->mutex);
}
return *this;
}