From f140cbb7d247e79d1dec8cdf86485146144c1118 Mon Sep 17 00:00:00 2001 From: robocop Date: Sat, 13 Dec 2008 09:19:22 +0000 Subject: [PATCH] Before I put the "explicit" modifier on AtomicCounter's constructor, the compiler was generating a temporary AtomicCounter on the fly and later assigning it to the target AtomicCounter instance to satisfy an assignment of an integer to an AtomicCounter. I'm not sure we want to allow (and worse, foster) the copying of AtomicCounter instances or temporaries created by the compiler, hence I implemented a new method and fixed the code, but I don't know how to write the best code for one group of platforms in fb_atomic.h, hence I wrote the method with a syntax problem to be caught by someone who knows better. --- src/common/classes/fb_atomic.h | 70 +++++++++++++++++++++++++--------- src/common/classes/rwlock.h | 22 +++++++---- src/remote/remote.cpp | 6 +-- 3 files changed, 69 insertions(+), 29 deletions(-) diff --git a/src/common/classes/fb_atomic.h b/src/common/classes/fb_atomic.h index c87247b457..3f18b0c552 100644 --- a/src/common/classes/fb_atomic.h +++ b/src/common/classes/fb_atomic.h @@ -41,31 +41,41 @@ class AtomicCounter public: typedef LONG counter_type; - AtomicCounter(counter_type val = 0) : counter(val) {} + explicit AtomicCounter(counter_type val = 0) : counter(val) {} ~AtomicCounter() {} - counter_type exchangeAdd(counter_type val) { + counter_type exchangeAdd(counter_type val) + { return InterlockedExchangeAdd(&counter, val); } - counter_type operator +=(counter_type val) { + counter_type operator +=(counter_type val) + { return exchangeAdd(val) + val; } - counter_type operator -=(counter_type val) { + counter_type operator -=(counter_type val) + { return exchangeAdd(-val) - val; } - counter_type operator ++() { + counter_type operator ++() + { return InterlockedIncrement(&counter); } - counter_type operator --() { + counter_type operator --() + { return InterlockedDecrement(&counter); } counter_type value() const { return counter; } + counter_type setValue(counter_type val) + { + return InterlockedExchange(&counter, val); + } + private: # if defined(MINGW) counter_type counter; @@ -86,10 +96,11 @@ class AtomicCounter public: typedef int counter_type; - AtomicCounter(counter_type value = 0) : counter(value) {} + explicit AtomicCounter(counter_type value = 0) : counter(value) {} ~AtomicCounter() {} - counter_type exchangeAdd(counter_type value) { + counter_type exchangeAdd(counter_type value) + { register counter_type result; __asm __volatile ( "lock; xaddl %0, %1" @@ -98,24 +109,33 @@ public: return result; } - counter_type operator +=(counter_type value) { + counter_type operator +=(counter_type value) + { return exchangeAdd(value) + value; } - counter_type operator -=(counter_type value) { + counter_type operator -=(counter_type value) + { return exchangeAdd(-value) - value; } - counter_type operator ++() { + counter_type operator ++() + { return exchangeAdd(1) + 1; } - counter_type operator --() { + counter_type operator --() + { return exchangeAdd(-1) - 1; } counter_type value() const { return counter; } + counter_type setValue(counter_type val) + { + return ... + } + private: volatile counter_type counter; }; @@ -138,10 +158,11 @@ class AtomicCounter public: typedef int counter_type; - AtomicCounter(counter_type value = 0) : counter(value) {} + explicit AtomicCounter(counter_type value = 0) : counter(value) {} ~AtomicCounter() {} - counter_type exchangeAdd(counter_type value) { + counter_type exchangeAdd(counter_type value) + { lock.enter(); counter_type temp = counter; counter += value; @@ -149,28 +170,32 @@ public: return temp; } - counter_type operator +=(counter_type value) { + counter_type operator +=(counter_type value) + { lock.enter(); counter_type temp = counter += value; lock.leave(); return temp; } - counter_type operator -=(counter_type value) { + counter_type operator -=(counter_type value) + { lock.enter(); counter_type temp = counter -= value; lock.leave(); return temp; } - counter_type operator ++() { + counter_type operator ++() + { lock.enter(); counter_type temp = counter++; lock.leave(); return temp; } - counter_type operator --() { + counter_type operator --() + { lock.enter(); counter_type temp = counter--; lock.leave(); @@ -179,6 +204,15 @@ public: counter_type value() const { return counter; } + counter_type setValue(counter_type val) + { + lock.enter(); + const counter_type temp = counter; + counter = val; + lock.leave(); + return temp; + } + private: volatile counter_type counter; Mutex lock; diff --git a/src/common/classes/rwlock.h b/src/common/classes/rwlock.h index 33f02811e0..d7637a1928 100644 --- a/src/common/classes/rwlock.h +++ b/src/common/classes/rwlock.h @@ -59,11 +59,10 @@ private: void init() { - lock = 0; + lock.setValue(0); blockedReaders = 0; - blockedWriters = 0; - readers_semaphore = CreateSemaphore(NULL, 0 /*initial count*/, - INT_MAX, NULL); + blockedWriters.setValue(0); + readers_semaphore = CreateSemaphore(NULL, 0 /*initial count*/, INT_MAX, NULL); if (readers_semaphore == NULL) system_call_failed::raise("CreateSemaphore"); writers_event = CreateEvent(NULL, FALSE/*auto-reset*/, FALSE, NULL); @@ -98,8 +97,7 @@ public: } else if (blockedReaders) { MutexLockGuard guard(blockedReadersLock); - if (blockedReaders && - !ReleaseSemaphore(readers_semaphore, blockedReaders, NULL)) + if (blockedReaders && !ReleaseSemaphore(readers_semaphore, blockedReaders, NULL)) { system_call_failed::raise("ReleaseSemaphore"); } @@ -348,7 +346,11 @@ namespace Firebird { class ReadLockGuard { public: - ReadLockGuard(RWLock &alock) : lock(&alock) { lock->beginRead(); } + ReadLockGuard(RWLock &alock) + : lock(&alock) + { + lock->beginRead(); + } ~ReadLockGuard() { release(); } void release() @@ -370,7 +372,11 @@ private: class WriteLockGuard { public: - WriteLockGuard(RWLock &alock) : lock(&alock) { lock->beginWrite(); } + WriteLockGuard(RWLock &alock) + : lock(&alock) + { + lock->beginWrite(); + } ~WriteLockGuard() { release(); } void release() diff --git a/src/remote/remote.cpp b/src/remote/remote.cpp index c0fa05c8d0..3cac7cdf67 100644 --- a/src/remote/remote.cpp +++ b/src/remote/remote.cpp @@ -37,7 +37,7 @@ #include "../common/classes/init.h" #ifdef DEV_BUILD -Firebird::AtomicCounter rem_port::portCounter = 0; +Firebird::AtomicCounter rem_port::portCounter(0); #endif #ifdef REMOTE_DEBUG @@ -734,7 +734,7 @@ void PortsCleanup::registerPort(rem_port* port) Firebird::MemoryPool& pool = *getDefaultMemoryPool(); m_ports = FB_NEW (pool) PortsArray(pool); } - + m_ports->add(port); } @@ -755,7 +755,7 @@ void PortsCleanup::unRegisterPort(rem_port* port) void PortsCleanup::closePorts() { Firebird::MutexLockGuard guard(m_mutex); - + if (m_ports) { rem_port* const* ptr = m_ports->begin();