From 7190e1994763e42d208b5e3f17edd53a0d48546a Mon Sep 17 00:00:00 2001 From: Andrew Stitcher Date: Fri, 18 Sep 2009 19:55:47 +0000 Subject: Rearrange readCredit logic for correctness git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@816763 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/sys/AsynchIOHandler.cpp | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) (limited to 'cpp/src') diff --git a/cpp/src/qpid/sys/AsynchIOHandler.cpp b/cpp/src/qpid/sys/AsynchIOHandler.cpp index 8094abd43d..46d8f3b4f1 100644 --- a/cpp/src/qpid/sys/AsynchIOHandler.cpp +++ b/cpp/src/qpid/sys/AsynchIOHandler.cpp @@ -107,6 +107,23 @@ bool AsynchIOHandler::readbuff(AsynchIO& , AsynchIO::BufferBase* buff) { if (readError) { return false; } + + bool ret = true; + + // Check here for read credit + if (readCredit.get() != InfiniteCredit) { + // TODO In theory should be able to use an atomic operation before taking the lock + // but in practice there seems to be an unexplained race in that case + ScopedLock l(creditLock); + if (--readCredit == 0) { + assert(readCredit.get() >= 0); + if (readCredit.get() == 0) { + aio->stopReading(); + ret = false; + } + } + } + size_t decoded = 0; if (codec) { // Already initiated try { @@ -149,20 +166,7 @@ bool AsynchIOHandler::readbuff(AsynchIO& , AsynchIO::BufferBase* buff) { // Give whole buffer back to aio subsystem aio->queueReadBuffer(buff); } - // Check here for read credit - if (readCredit.get() != InfiniteCredit) { - // TODO In theory should be able to use an atomic operation before taking the lock - // but in practice there seems to be an unexplained race in that case - ScopedLock l(creditLock); - if (--readCredit == 0) { - assert(readCredit.get() >= 0); - if (readCredit.get() == 0) { - aio->stopReading(); - return false; - } - } - } - return true; + return ret; } void AsynchIOHandler::eof(AsynchIO&) { -- cgit v1.2.1