consolidate AtomicBoolean
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
|
||||
#include <pv/pvaDefs.h>
|
||||
#include <pv/event.h>
|
||||
#include <epicsExit.h>
|
||||
|
||||
@@ -357,38 +358,6 @@ void printValue(std::string const & channelName, PVStructure::shared_pointer con
|
||||
std::cout << std::endl << *(pv.get()) << std::endl << std::endl;
|
||||
}
|
||||
|
||||
struct AtomicBoolean_null_deleter
|
||||
{
|
||||
void operator()(void const *) const {}
|
||||
};
|
||||
|
||||
// standard performance on set/clear, use of TR1::shared_ptr lock-free counter for get
|
||||
// alternative is to use boost::atomic
|
||||
class AtomicBoolean
|
||||
{
|
||||
public:
|
||||
AtomicBoolean() : counter(static_cast<void*>(0), AtomicBoolean_null_deleter()) {};
|
||||
|
||||
void set() {
|
||||
mutex.lock();
|
||||
setp = counter;
|
||||
mutex.unlock();
|
||||
}
|
||||
void clear() {
|
||||
mutex.lock();
|
||||
setp.reset();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
bool get() const {
|
||||
return counter.use_count() == 2;
|
||||
}
|
||||
private:
|
||||
TR1::shared_ptr<void> counter;
|
||||
TR1::shared_ptr<void> setp;
|
||||
epics::pvData::Mutex mutex;
|
||||
};
|
||||
|
||||
class ChannelPutRequesterImpl : public ChannelPutRequester
|
||||
{
|
||||
private:
|
||||
|
||||
@@ -17,6 +17,29 @@ typedef struct {
|
||||
|
||||
typedef epicsInt32 pvAccessID;
|
||||
|
||||
class AtomicBoolean
|
||||
{
|
||||
public:
|
||||
AtomicBoolean() : val(false) {}
|
||||
|
||||
void set() {
|
||||
epicsGuard<epicsMutex> G(mutex);
|
||||
val = true;
|
||||
}
|
||||
void clear() {
|
||||
epicsGuard<epicsMutex> G(mutex);
|
||||
val = false;
|
||||
}
|
||||
|
||||
bool get() const {
|
||||
epicsGuard<epicsMutex> G(mutex);
|
||||
return val;
|
||||
}
|
||||
private:
|
||||
bool val;
|
||||
mutable epicsMutex mutex;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
#endif // PVADEFS_H
|
||||
|
||||
@@ -609,38 +609,6 @@ public:
|
||||
};
|
||||
|
||||
|
||||
struct AtomicBoolean_null_deleter
|
||||
{
|
||||
void operator()(void const *) const {}
|
||||
};
|
||||
|
||||
// standard performance on set/clear, use of tr1::shared_ptr lock-free counter for get
|
||||
// alternative is to use boost::atomic
|
||||
class AtomicBoolean
|
||||
{
|
||||
public:
|
||||
AtomicBoolean() : counter(static_cast<void*>(0), AtomicBoolean_null_deleter()) {};
|
||||
|
||||
void set() {
|
||||
mutex.lock();
|
||||
setp = counter;
|
||||
mutex.unlock();
|
||||
}
|
||||
void clear() {
|
||||
mutex.lock();
|
||||
setp.reset();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
bool get() const {
|
||||
return counter.use_count() == 2;
|
||||
}
|
||||
private:
|
||||
std::tr1::shared_ptr<void> counter;
|
||||
std::tr1::shared_ptr<void> setp;
|
||||
epics::pvData::Mutex mutex;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user