diff options
author | Andrew Stitcher <astitcher@apache.org> | 2012-03-15 14:24:32 +0000 |
---|---|---|
committer | Andrew Stitcher <astitcher@apache.org> | 2012-03-15 14:24:32 +0000 |
commit | 4a5c2eaa9eb38de9b9c4416f4fa37ede1e7d2295 (patch) | |
tree | d6944b45f11ac892b1a1a33113c92b0279314825 /qpid/cpp/src | |
parent | 2179e8f3b41138356f49a16800a2945248fdae17 (diff) | |
download | qpid-python-4a5c2eaa9eb38de9b9c4416f4fa37ede1e7d2295.tar.gz |
QPID-3893: C++ broker appears to segfault during MultipleTransactedBatchProducerTest
Added in lock to preserve the previous read/write concurrency
guarantees of FieldTable in the face of the new caching.
Fix extra unnecessary decodes
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1301001 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/cpp/src')
-rw-r--r-- | qpid/cpp/src/qpid/framing/FieldTable.cpp | 44 |
1 files changed, 39 insertions, 5 deletions
diff --git a/qpid/cpp/src/qpid/framing/FieldTable.cpp b/qpid/cpp/src/qpid/framing/FieldTable.cpp index b696b5e54f..f9dc42916d 100644 --- a/qpid/cpp/src/qpid/framing/FieldTable.cpp +++ b/qpid/cpp/src/qpid/framing/FieldTable.cpp @@ -28,22 +28,45 @@ #include "qpid/Msg.h" #include <assert.h> +// The locking rationale in the FieldTable seems a little odd, but it +// maintains the concurrent guarantees and requirements that were in +// place before the cachedBytes/cachedSize were added: +// +// The FieldTable client code needs to make sure that they call no write +// operation in parallel with any other operation on the FieldTable. +// However multiple parallel read operations are safe. +// +// To this end the only code that is locked is code that can transparently +// change the state of the FieldTable during a read only operation. +// (In other words the code that required the mutable members in the class +// definition!) +// namespace qpid { + +using sys::Mutex; +using sys::ScopedLock; + namespace framing { FieldTable::FieldTable() : - cachedSize(0) + cachedSize(0), + newBytes(false) { } FieldTable::FieldTable(const FieldTable& ft) : cachedBytes(ft.cachedBytes), - cachedSize(ft.cachedSize) + cachedSize(ft.cachedSize), + newBytes(ft.newBytes) { // Only copy the values if we have no raw data // - copying the map is expensive and we can // reconstruct it if necessary from the raw data - if (!cachedBytes && !ft.values.empty()) values = ft.values; + if (cachedBytes) { + newBytes = true; + return; + } + if (!ft.values.empty()) values = ft.values; } FieldTable& FieldTable::operator=(const FieldTable& ft) @@ -52,10 +75,13 @@ FieldTable& FieldTable::operator=(const FieldTable& ft) values.swap(nft.values); cachedBytes.swap(nft.cachedBytes); cachedSize = nft.cachedSize; + newBytes = nft.newBytes; return (*this); } uint32_t FieldTable::encodedSize() const { + ScopedLock<Mutex> l(lock); + if (cachedSize != 0) { return cachedSize; } @@ -238,6 +264,7 @@ void FieldTable::encode(Buffer& buffer) const { i->second->encode(buffer); } // Now create raw bytes in case we are used again + ScopedLock<Mutex> l(lock); cachedSize = buffer.getPosition() - p; cachedBytes = boost::shared_array<uint8_t>(new uint8_t[cachedSize]); buffer.setPosition(p); @@ -261,14 +288,17 @@ void FieldTable::decode(Buffer& buffer){ // Copy data into our buffer cachedBytes = boost::shared_array<uint8_t>(new uint8_t[len + 4]); cachedSize = len + 4; + newBytes = true; buffer.setPosition(p); buffer.getRawData(&cachedBytes[0], cachedSize); } void FieldTable::realDecode() const { + ScopedLock<Mutex> l(lock); + // If we've got no raw data stored up then nothing to do - if (!cachedBytes) + if (!newBytes) return; Buffer buffer((char*)&cachedBytes[0], cachedSize); @@ -286,10 +316,13 @@ void FieldTable::realDecode() const values[name] = ValuePtr(value); } } + newBytes = false; } -void FieldTable::flushRawCache() const +void FieldTable::flushRawCache() { + // We can only flush the cache if there are no cached bytes to decode + assert(newBytes==false); // Avoid recreating shared array unless we actually have one. if (cachedBytes) cachedBytes.reset(); cachedSize = 0; @@ -319,6 +352,7 @@ void FieldTable::erase(const std::string& name) void FieldTable::clear() { values.clear(); + newBytes = false; flushRawCache(); } |