localStaticLock - static local thread-safety

This commit is contained in:
Matej Sekoranja
2012-10-08 12:19:21 +02:00
parent 07f9a8c0f6
commit f88f0b4a76
5 changed files with 77 additions and 3 deletions
+2
View File
@@ -23,6 +23,7 @@ INC += messageQueue.h
INC += destroyable.h
INC += status.h
INC += sharedPtr.h
INC += localStaticLock.h
LIBSRCS += byteBuffer.cpp
LIBSRCS += bitSet.cpp
@@ -35,6 +36,7 @@ LIBSRCS += timeFunction.cpp
LIBSRCS += timer.cpp
LIBSRCS += status.cpp
LIBSRCS += messageQueue.cpp
LIBSRCS += localStaticLock.cpp
SRC_DIRS += $(PVDATA)/pv
+4 -3
View File
@@ -528,10 +528,11 @@ FieldConstPtr FieldCreate::deserialize(ByteBuffer* buffer, DeserializableControl
FieldCreatePtr FieldCreate::getFieldCreate()
{
static FieldCreatePtr fieldCreate;
static Mutex mutex;
Lock xx(mutex);
LOCAL_STATIC_LOCK;
static FieldCreatePtr fieldCreate;
static Mutex mutex;
Lock xx(mutex);
if(fieldCreate.get()==0) fieldCreate = FieldCreatePtr(new FieldCreate());
return fieldCreate;
}
+39
View File
@@ -0,0 +1,39 @@
/* localStaticLock.cpp */
/**
* Copyright - See the COPYRIGHT that is included with this distribution.
* EPICS pvData is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*/
/**
* @author mse
*/
#include <pv/localStaticLock.h>
static int nifty_counter;
static epics::pvData::Mutex* g_localStaticInitMutex;
epics::pvData::Mutex& getLocalStaticInitMutex()
{
return *g_localStaticInitMutex;
}
// The counter is initialized at load-time, i.e., before any of the static objects are initialized.
MutexInitializer::MutexInitializer ()
{
if (0 == nifty_counter++)
{
// Initialize static members.
g_localStaticInitMutex = new epics::pvData::Mutex();
}
}
MutexInitializer::~MutexInitializer ()
{
if (0 == --nifty_counter)
{
// Clean-up.
delete g_localStaticInitMutex;
}
}
+31
View File
@@ -0,0 +1,31 @@
/* localStaticLock.h */
/**
* Copyright - See the COPYRIGHT that is included with this distribution.
* EPICS pvData is distributed subject to a Software License Agreement found
* in file LICENSE that is included with this distribution.
*/
/**
* @author mse
*/
#ifndef LOCALSTATICLOCK_H
#define LOCALSTATICLOCK_H
#include <pv/lock.h>
extern epics::pvData::Mutex& getLocalStaticInitMutex();
#if defined(__GNUC__) && __GNUC__ >= 4
// noop
#define LOCAL_STATIC_LOCK
#else
#define LOCAL_STATIC_LOCK epics::pvData::Lock localStaticInitMutexLock(getLocalStaticInitMutex());
#endif
static class MutexInitializer {
public:
MutexInitializer ();
~MutexInitializer ();
} localStaticMutexInitializer; // Note object here in the header.
#endif /* LOCALSTATICLOCK_H */
+1
View File
@@ -27,6 +27,7 @@ typedef unsigned int uintptr_t;
#endif
#include <pv/sharedPtr.h>
#include <pv/localStaticLock.h>
namespace epics { namespace pvData {