diff --git a/pvAccessApp/utils/namedLockPattern.h b/pvAccessApp/utils/namedLockPattern.h index d57455a..26b6c1c 100644 --- a/pvAccessApp/utils/namedLockPattern.h +++ b/pvAccessApp/utils/namedLockPattern.h @@ -34,10 +34,15 @@ public: virtual ~NamedLockPattern() {}; /** * Acquire synchronization lock for named object. + * + * NOTE: Argument msecs is currently not supported due to + * Darwin OS not supporting pthread_mutex_timedlock. May be changed in the future. + * * @param name name of the object whose lock to acquire. * @param msec the number of milleseconds to wait. * An argument less than or equal to zero means not to wait at all. * @return true if acquired, false othwerwise. + * NOTE: currently this routine always returns true. Look above for explanation. */ bool acquireSynchronizationObject(const Key name, const int64 msec); /** diff --git a/pvAccessApp/utils/referenceCountingLock.cpp b/pvAccessApp/utils/referenceCountingLock.cpp index 2b45005..b618a84 100644 --- a/pvAccessApp/utils/referenceCountingLock.cpp +++ b/pvAccessApp/utils/referenceCountingLock.cpp @@ -8,7 +8,7 @@ namespace epics { namespace pvAccess { ReferenceCountingLock::ReferenceCountingLock(): _references(1) { - pthread_mutexattr_t mutexAttribute; +/* pthread_mutexattr_t mutexAttribute; int32 retval = pthread_mutexattr_init(&mutexAttribute); if(retval != 0) { @@ -31,23 +31,29 @@ ReferenceCountingLock::ReferenceCountingLock(): _references(1) assert(false); } - pthread_mutexattr_destroy(&mutexAttribute); + pthread_mutexattr_destroy(&mutexAttribute);*/ } ReferenceCountingLock::~ReferenceCountingLock() { - pthread_mutex_destroy(&_mutex); +// pthread_mutex_destroy(&_mutex); } bool ReferenceCountingLock::acquire(int64 msecs) { -#ifdef darwin - // timedlock not supported by Darwin OS - return (pthread_mutex_lock(&_mutex) == 0); -#else - struct timespec deltatime; - deltatime.tv_sec = msecs / 1000; - deltatime.tv_nsec = (msecs % 1000) * 1000; + _mutex.lock(); + return true; +/* struct timespec deltatime; + if(msecs > 0) + { + deltatime.tv_sec = msecs / 1000; + deltatime.tv_nsec = (msecs % 1000) * 1000; + } + else + { + deltatime.tv_sec = 0; + deltatime.tv_nsec = 0; + } int32 retval = pthread_mutex_timedlock(&_mutex, &deltatime); if(retval == 0) @@ -55,35 +61,32 @@ bool ReferenceCountingLock::acquire(int64 msecs) return true; } return false; -#endif +*/ } void ReferenceCountingLock::release() { - int retval = pthread_mutex_unlock(&_mutex); + _mutex.unlock(); +/* int retval = pthread_mutex_unlock(&_mutex); if(retval != 0) { //string errMsg = "Error: pthread_mutex_unlock failed: " + string(strerror(retval)); //TODO do something? - } + }*/ } int ReferenceCountingLock::increment() { - //TODO does it really has to be atomic? - return ++_references; - // commented because linking depends on specific version of glibc library - // on i386 target - //return __sync_add_and_fetch(&_references,1); + Lock guard(&_countMutex); + ++_references; + return _references; } int ReferenceCountingLock::decrement() { - //TODO does it really has to be atomic? - return --_references; - // commented because linking depends on specific version of glibc library - // on i386 target - //return __sync_sub_and_fetch(&_references,1); + Lock guard(&_countMutex); + --_references; + return _references; } }} diff --git a/pvAccessApp/utils/referenceCountingLock.h b/pvAccessApp/utils/referenceCountingLock.h index 4153b50..c93f51b 100644 --- a/pvAccessApp/utils/referenceCountingLock.h +++ b/pvAccessApp/utils/referenceCountingLock.h @@ -11,6 +11,7 @@ #include #include +#include #include #include @@ -42,9 +43,15 @@ public: /** * Attempt to acquire lock. * + * NOTE: Argument msecs is currently not supported due to + * Darwin OS not supporting pthread_mutex_timedlock. May be changed in the future. + * * @param msecs the number of milleseconds to wait. * An argument less than or equal to zero means not to wait at all. + * * @return true if acquired, false otherwise. + * NOTE: currently this routine always returns true. Look above for explanation. + * */ bool acquire(int64 msecs); /** @@ -65,7 +72,9 @@ public: int decrement(); private: int _references; - pthread_mutex_t _mutex; + Mutex _mutex; + Mutex _countMutex; + //pthread_mutex_t _mutex; }; diff --git a/testApp/utils/namedLockPatternTest.cpp b/testApp/utils/namedLockPatternTest.cpp index edbfbbe..3470afa 100644 --- a/testApp/utils/namedLockPatternTest.cpp +++ b/testApp/utils/namedLockPatternTest.cpp @@ -156,7 +156,7 @@ void* testWorker2(void* p) assert(namedGuard.acquireSynchronizationObject(addr,timeout)); usleep(1); } -#ifndef darwin + //this thread sleeps a while and gets timeout on lock { sleep(1); @@ -165,9 +165,11 @@ void* testWorker2(void* p) addr.ia.sin_port = 1; addr.ia.sin_family = AF_INET; NamedLock namedGuard(namedLockPattern); - assert(!namedGuard.acquireSynchronizationObject(addr,timeout)); + //TODO swap next two lines this if timed lock used + //assert(!namedGuard.acquireSynchronizationObject(addr,timeout)); + assert(namedGuard.acquireSynchronizationObject(addr,timeout)); } -#endif + return NULL; }