You've already forked mariadb-columnstore-engine
mirror of
https://github.com/mariadb-corporation/mariadb-columnstore-engine.git
synced 2025-07-30 19:23:07 +03:00
clang format apply
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@ -42,22 +42,21 @@
|
||||
|
||||
namespace rwlock
|
||||
{
|
||||
|
||||
/// the layout of the shmseg
|
||||
struct State
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
volatile LONG writerswaiting;
|
||||
volatile LONG writing;
|
||||
volatile LONG readerswaiting;
|
||||
volatile LONG reading;
|
||||
volatile LONG writerswaiting;
|
||||
volatile LONG writing;
|
||||
volatile LONG readerswaiting;
|
||||
volatile LONG reading;
|
||||
#else
|
||||
volatile int writerswaiting;
|
||||
volatile int writing;
|
||||
volatile int readerswaiting;
|
||||
volatile int reading;
|
||||
volatile int writerswaiting;
|
||||
volatile int writing;
|
||||
volatile int readerswaiting;
|
||||
volatile int reading;
|
||||
#endif
|
||||
boost::interprocess::interprocess_semaphore sems[3];
|
||||
boost::interprocess::interprocess_semaphore sems[3];
|
||||
};
|
||||
|
||||
/* the lock state without the semaphores, passed out by timed_write_lock() for
|
||||
@ -66,57 +65,58 @@ class RWLockMonitor
|
||||
struct LockState
|
||||
{
|
||||
#ifdef _MSC_VER
|
||||
LONG writerswaiting;
|
||||
LONG writing;
|
||||
LONG readerswaiting;
|
||||
LONG reading;
|
||||
bool mutexLocked;
|
||||
LONG writerswaiting;
|
||||
LONG writing;
|
||||
LONG readerswaiting;
|
||||
LONG reading;
|
||||
bool mutexLocked;
|
||||
#else
|
||||
int writerswaiting;
|
||||
int writing;
|
||||
int readerswaiting;
|
||||
int reading;
|
||||
bool mutexLocked;
|
||||
int writerswaiting;
|
||||
int writing;
|
||||
int readerswaiting;
|
||||
int reading;
|
||||
bool mutexLocked;
|
||||
#endif
|
||||
};
|
||||
|
||||
class RWLockShmImpl
|
||||
{
|
||||
public:
|
||||
static RWLockShmImpl* makeRWLockShmImpl(int key, bool* excl = 0);
|
||||
public:
|
||||
static RWLockShmImpl* makeRWLockShmImpl(int key, bool* excl = 0);
|
||||
|
||||
boost::interprocess::shared_memory_object fStateShm;
|
||||
boost::interprocess::mapped_region fRegion;
|
||||
State* fState;
|
||||
boost::interprocess::shared_memory_object fStateShm;
|
||||
boost::interprocess::mapped_region fRegion;
|
||||
State* fState;
|
||||
|
||||
std::string keyString()
|
||||
{
|
||||
return fKeyString;
|
||||
}
|
||||
private:
|
||||
explicit RWLockShmImpl(int key, bool excl = false);
|
||||
~RWLockShmImpl();
|
||||
RWLockShmImpl(const RWLockShmImpl& rhs);
|
||||
RWLockShmImpl& operator=(const RWLockShmImpl& rhs);
|
||||
std::string fKeyString;
|
||||
std::string keyString()
|
||||
{
|
||||
return fKeyString;
|
||||
}
|
||||
|
||||
private:
|
||||
explicit RWLockShmImpl(int key, bool excl = false);
|
||||
~RWLockShmImpl();
|
||||
RWLockShmImpl(const RWLockShmImpl& rhs);
|
||||
RWLockShmImpl& operator=(const RWLockShmImpl& rhs);
|
||||
std::string fKeyString;
|
||||
};
|
||||
|
||||
class not_excl : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
return "not_excl";
|
||||
}
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
return "not_excl";
|
||||
}
|
||||
};
|
||||
|
||||
class wouldblock : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
return "wouldblock";
|
||||
}
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
return "wouldblock";
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief Implements RW locks for use across threads & processes
|
||||
@ -138,142 +138,140 @@ public:
|
||||
*/
|
||||
class RWLock
|
||||
{
|
||||
public:
|
||||
public:
|
||||
// semaphore numbers
|
||||
static const int MUTEX = 0;
|
||||
static const int READERS = 1;
|
||||
static const int WRITERS = 2;
|
||||
|
||||
// semaphore numbers
|
||||
static const int MUTEX = 0;
|
||||
static const int READERS = 1;
|
||||
static const int WRITERS = 2;
|
||||
/** @brief Keyed constructor.
|
||||
*
|
||||
* Instantiate an RWLock with the given key. All instances that
|
||||
* share a key share the same lock.
|
||||
*
|
||||
* @param key The key
|
||||
* @param excl If true and this is the first instance with the
|
||||
* supplied key, it will return holding the write lock. If true and
|
||||
* this is not the first instance, it will throw not_excl. The intent
|
||||
* is similar to the IPC_EXCL flag in the sem/shm implementations.
|
||||
*/
|
||||
EXPORT explicit RWLock(int key, bool* excl = 0);
|
||||
|
||||
/** @brief Keyed constructor.
|
||||
*
|
||||
* Instantiate an RWLock with the given key. All instances that
|
||||
* share a key share the same lock.
|
||||
*
|
||||
* @param key The key
|
||||
* @param excl If true and this is the first instance with the
|
||||
* supplied key, it will return holding the write lock. If true and
|
||||
* this is not the first instance, it will throw not_excl. The intent
|
||||
* is similar to the IPC_EXCL flag in the sem/shm implementations.
|
||||
*/
|
||||
EXPORT explicit RWLock(int key, bool* excl = 0);
|
||||
EXPORT ~RWLock();
|
||||
|
||||
EXPORT ~RWLock();
|
||||
/** @brief Grab a read lock
|
||||
*
|
||||
* Grab a read lock. This will block iff writers are waiting or
|
||||
* a writer is active. The version with priority ignores any
|
||||
* waiting threads and grabs the lock.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void read_lock(bool block = true);
|
||||
|
||||
/** @brief Grab a read lock
|
||||
*
|
||||
* Grab a read lock. This will block iff writers are waiting or
|
||||
* a writer is active. The version with priority ignores any
|
||||
* waiting threads and grabs the lock.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void read_lock(bool block = true);
|
||||
EXPORT void read_lock_priority(bool block = true);
|
||||
|
||||
EXPORT void read_lock_priority(bool block = true);
|
||||
/** @brief Release a read lock.
|
||||
*
|
||||
* Release a read lock.
|
||||
*/
|
||||
EXPORT void read_unlock();
|
||||
|
||||
/** @brief Release a read lock.
|
||||
*
|
||||
* Release a read lock.
|
||||
*/
|
||||
EXPORT void read_unlock();
|
||||
/** @brief Grab a write lock
|
||||
*
|
||||
* Grab a write lock. This will block while another writer or reader is
|
||||
* active and will have exclusive access on waking.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void write_lock(bool block = true);
|
||||
|
||||
/** @brief Grab a write lock
|
||||
*
|
||||
* Grab a write lock. This will block while another writer or reader is
|
||||
* active and will have exclusive access on waking.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void write_lock(bool block = true);
|
||||
/** @brief A timed write lock.
|
||||
*
|
||||
* Queues up for the write lock for a specified amount of time. Returns
|
||||
* true if it got the lock, return false if it timed out first.
|
||||
* If the timeout happens, it will also return the lock state if passed
|
||||
* a non-NULL LockState struct. This is a specialization for supporting
|
||||
* the RWLockMonitor class.
|
||||
*/
|
||||
EXPORT bool timed_write_lock(const struct timespec& ts, struct LockState* state = 0);
|
||||
|
||||
/** @brief A timed write lock.
|
||||
*
|
||||
* Queues up for the write lock for a specified amount of time. Returns
|
||||
* true if it got the lock, return false if it timed out first.
|
||||
* If the timeout happens, it will also return the lock state if passed
|
||||
* a non-NULL LockState struct. This is a specialization for supporting
|
||||
* the RWLockMonitor class.
|
||||
*/
|
||||
EXPORT bool timed_write_lock(const struct timespec& ts,
|
||||
struct LockState* state = 0);
|
||||
/** @brief Release a write lock.
|
||||
*
|
||||
* Release a write lock.
|
||||
*/
|
||||
EXPORT void write_unlock();
|
||||
|
||||
/** @brief Release a write lock.
|
||||
*
|
||||
* Release a write lock.
|
||||
*/
|
||||
EXPORT void write_unlock();
|
||||
/* note: these haven't been proven yet */
|
||||
|
||||
/* note: these haven't been proven yet */
|
||||
/** @brief Upgrade a read lock to a write lock
|
||||
*
|
||||
* Upgrade a read lock to a write lock. It may have to block
|
||||
* if there are other readers currently reading. No guarantees of atomicity.
|
||||
*/
|
||||
EXPORT void upgrade_to_write();
|
||||
|
||||
/** @brief Upgrade a read lock to a write lock
|
||||
*
|
||||
* Upgrade a read lock to a write lock. It may have to block
|
||||
* if there are other readers currently reading. No guarantees of atomicity.
|
||||
*/
|
||||
EXPORT void upgrade_to_write();
|
||||
/** @brief Downgrade a write lock to a read lock
|
||||
*
|
||||
* Downgrade a write lock to a read lock. The conversion happens
|
||||
* atomically.
|
||||
*/
|
||||
EXPORT void downgrade_to_read();
|
||||
|
||||
/** @brief Downgrade a write lock to a read lock
|
||||
*
|
||||
* Downgrade a write lock to a read lock. The conversion happens
|
||||
* atomically.
|
||||
*/
|
||||
EXPORT void downgrade_to_read();
|
||||
/** @brief Reset the lock's state (Use with caution!)
|
||||
*
|
||||
* If the lock gets into a bad state in testing or something,
|
||||
* this will reset the state.
|
||||
* @warning This is safe only if there are no other threads using this
|
||||
* lock.
|
||||
*/
|
||||
EXPORT void reset();
|
||||
|
||||
/** @brief Reset the lock's state (Use with caution!)
|
||||
*
|
||||
* If the lock gets into a bad state in testing or something,
|
||||
* this will reset the state.
|
||||
* @warning This is safe only if there are no other threads using this
|
||||
* lock.
|
||||
*/
|
||||
EXPORT void reset();
|
||||
/* These are for white box testing only */
|
||||
inline void lock()
|
||||
{
|
||||
down(MUTEX, true);
|
||||
}
|
||||
inline void unlock()
|
||||
{
|
||||
up(MUTEX);
|
||||
}
|
||||
inline int getWriting() const
|
||||
{
|
||||
return fPImpl->fState->writing;
|
||||
}
|
||||
inline int getReading() const
|
||||
{
|
||||
return fPImpl->fState->reading;
|
||||
}
|
||||
inline int getWritersWaiting() const
|
||||
{
|
||||
return fPImpl->fState->writerswaiting;
|
||||
}
|
||||
inline int getReadersWaiting() const
|
||||
{
|
||||
return fPImpl->fState->readerswaiting;
|
||||
}
|
||||
LockState getLockState();
|
||||
|
||||
/* These are for white box testing only */
|
||||
inline void lock()
|
||||
{
|
||||
down(MUTEX, true);
|
||||
}
|
||||
inline void unlock()
|
||||
{
|
||||
up(MUTEX);
|
||||
}
|
||||
inline int getWriting() const
|
||||
{
|
||||
return fPImpl->fState->writing;
|
||||
}
|
||||
inline int getReading() const
|
||||
{
|
||||
return fPImpl->fState->reading;
|
||||
}
|
||||
inline int getWritersWaiting() const
|
||||
{
|
||||
return fPImpl->fState->writerswaiting;
|
||||
}
|
||||
inline int getReadersWaiting() const
|
||||
{
|
||||
return fPImpl->fState->readerswaiting;
|
||||
}
|
||||
LockState getLockState();
|
||||
private:
|
||||
RWLock(const RWLock& rwl);
|
||||
RWLock& operator=(const RWLock& rwl);
|
||||
|
||||
private:
|
||||
RWLock(const RWLock& rwl);
|
||||
RWLock& operator=(const RWLock& rwl);
|
||||
inline int getSemval(int) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
void down(int num, bool block = true);
|
||||
bool timed_down(int num, const boost::posix_time::ptime& ts); // to support timed_write_lock()
|
||||
void up(int num);
|
||||
|
||||
inline int getSemval(int) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
void down(int num, bool block = true);
|
||||
bool timed_down(int num, const boost::posix_time::ptime& ts); // to support timed_write_lock()
|
||||
void up(int num);
|
||||
|
||||
RWLockShmImpl* fPImpl;
|
||||
RWLockShmImpl* fPImpl;
|
||||
};
|
||||
|
||||
} //namespace rwlock
|
||||
} // namespace rwlock
|
||||
|
||||
#undef EXPORT
|
||||
|
||||
|
@ -44,31 +44,31 @@ using namespace boost;
|
||||
|
||||
#ifdef DEBUG
|
||||
using namespace std;
|
||||
#define PRINTSTATE() \
|
||||
cerr << " reading = " << state.reading << endl \
|
||||
<< " writing = " << state.writing << endl \
|
||||
<< " readerswaiting = " << state.readerswaiting << endl \
|
||||
<< " writerswaiting = " << state.writerswaiting << endl;
|
||||
#define PRINTSTATE() \
|
||||
cerr << " reading = " << state.reading << endl \
|
||||
<< " writing = " << state.writing << endl \
|
||||
<< " readerswaiting = " << state.readerswaiting << endl \
|
||||
<< " writerswaiting = " << state.writerswaiting << endl;
|
||||
|
||||
#define CHECKSAFETY() \
|
||||
if (!((state.reading == 0 && (state.writing == 0 || state.writing == 1)) || \
|
||||
(state.reading > 0 && state.writing == 0))) { \
|
||||
cerr << "RWLock_local::" << __func__ << ": safety invariant violation" << endl; \
|
||||
PRINTSTATE(); \
|
||||
throw std::logic_error("RWLock_local: safety invariant violation"); \
|
||||
}
|
||||
#define CHECKSAFETY() \
|
||||
if (!((state.reading == 0 && (state.writing == 0 || state.writing == 1)) || \
|
||||
(state.reading > 0 && state.writing == 0))) \
|
||||
{ \
|
||||
cerr << "RWLock_local::" << __func__ << ": safety invariant violation" << endl; \
|
||||
PRINTSTATE(); \
|
||||
throw std::logic_error("RWLock_local: safety invariant violation"); \
|
||||
}
|
||||
|
||||
#define CHECKLIVENESS() \
|
||||
if (!( \
|
||||
(!(state.readerswaiting > 0 || state.writerswaiting > 0) || \
|
||||
(state.reading > 0 || state.writing > 0)) || \
|
||||
(!(state.reading == 0 && state.writing == 0) || \
|
||||
(state.readerswaiting == 0 && state.writerswaiting == 0)) \
|
||||
)) { \
|
||||
cerr << "RWLock_local::" << __func__ << ": liveness invariant violation" << endl; \
|
||||
PRINTSTATE(); \
|
||||
throw std::logic_error("RWLock_local: liveness invariant violation"); \
|
||||
}
|
||||
#define CHECKLIVENESS() \
|
||||
if (!((!(state.readerswaiting > 0 || state.writerswaiting > 0) || \
|
||||
(state.reading > 0 || state.writing > 0)) || \
|
||||
(!(state.reading == 0 && state.writing == 0) || \
|
||||
(state.readerswaiting == 0 && state.writerswaiting == 0)))) \
|
||||
{ \
|
||||
cerr << "RWLock_local::" << __func__ << ": liveness invariant violation" << endl; \
|
||||
PRINTSTATE(); \
|
||||
throw std::logic_error("RWLock_local: liveness invariant violation"); \
|
||||
}
|
||||
|
||||
#undef CHECKLIVENESS
|
||||
#define CHECKLIVENESS()
|
||||
@ -77,13 +77,12 @@ using namespace std;
|
||||
|
||||
namespace rwlock
|
||||
{
|
||||
|
||||
RWLock_local::RWLock_local()
|
||||
{
|
||||
state.reading = 0;
|
||||
state.readerswaiting = 0;
|
||||
state.writing = 0;
|
||||
state.writerswaiting = 0;
|
||||
state.reading = 0;
|
||||
state.readerswaiting = 0;
|
||||
state.writing = 0;
|
||||
state.writerswaiting = 0;
|
||||
}
|
||||
|
||||
RWLock_local::~RWLock_local()
|
||||
@ -92,147 +91,145 @@ RWLock_local::~RWLock_local()
|
||||
|
||||
void RWLock_local::read_lock()
|
||||
{
|
||||
mutex.lock();
|
||||
mutex.lock();
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
if (state.writerswaiting > 0 || state.writing > 0)
|
||||
{
|
||||
state.readerswaiting++;
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
if (state.writerswaiting > 0 || state.writing > 0)
|
||||
{
|
||||
state.readerswaiting++;
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
while (state.writerswaiting > 0 || state.writing > 0)
|
||||
okToRead.wait(mutex);
|
||||
|
||||
while (state.writerswaiting > 0 || state.writing > 0)
|
||||
okToRead.wait(mutex);
|
||||
state.readerswaiting--;
|
||||
}
|
||||
|
||||
state.readerswaiting--;
|
||||
}
|
||||
|
||||
state.reading++;
|
||||
state.reading++;
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
mutex.unlock();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void RWLock_local::read_unlock()
|
||||
{
|
||||
mutex.lock();
|
||||
mutex.lock();
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
state.reading--;
|
||||
state.reading--;
|
||||
|
||||
if (state.writerswaiting > 0 && state.reading == 0)
|
||||
okToWrite.notify_one();
|
||||
if (state.writerswaiting > 0 && state.reading == 0)
|
||||
okToWrite.notify_one();
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
mutex.unlock();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void RWLock_local::write_lock()
|
||||
{
|
||||
mutex.lock();
|
||||
mutex.lock();
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
if (state.writing > 0 || state.reading > 0)
|
||||
{
|
||||
state.writerswaiting++;
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
while (state.writing > 0 || state.reading > 0)
|
||||
okToWrite.wait(mutex);
|
||||
|
||||
state.writerswaiting--;
|
||||
}
|
||||
|
||||
state.writing++;
|
||||
if (state.writing > 0 || state.reading > 0)
|
||||
{
|
||||
state.writerswaiting++;
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
while (state.writing > 0 || state.reading > 0)
|
||||
okToWrite.wait(mutex);
|
||||
|
||||
state.writerswaiting--;
|
||||
}
|
||||
|
||||
state.writing++;
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
}
|
||||
|
||||
void RWLock_local::write_unlock()
|
||||
{
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
state.writing--;
|
||||
state.writing--;
|
||||
|
||||
if (state.writerswaiting > 0)
|
||||
okToWrite.notify_one();
|
||||
else if (state.readerswaiting > 0)
|
||||
okToRead.notify_all();
|
||||
if (state.writerswaiting > 0)
|
||||
okToWrite.notify_one();
|
||||
else if (state.readerswaiting > 0)
|
||||
okToRead.notify_all();
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
mutex.unlock();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void RWLock_local::upgrade_to_write()
|
||||
{
|
||||
mutex.lock();
|
||||
mutex.lock();
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
state.reading--;
|
||||
|
||||
// try to cut in line
|
||||
if (state.reading == 0)
|
||||
{
|
||||
state.writing++;
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
state.reading--;
|
||||
return;
|
||||
}
|
||||
|
||||
// try to cut in line
|
||||
if (state.reading == 0)
|
||||
{
|
||||
state.writing++;
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// cut & paste from write_lock()
|
||||
if (state.writing > 0 || state.reading > 0)
|
||||
{
|
||||
state.writerswaiting++;
|
||||
// cut & paste from write_lock()
|
||||
if (state.writing > 0 || state.reading > 0)
|
||||
{
|
||||
state.writerswaiting++;
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
while (state.writing > 0 || state.reading > 0)
|
||||
okToWrite.wait(mutex);
|
||||
while (state.writing > 0 || state.reading > 0)
|
||||
okToWrite.wait(mutex);
|
||||
|
||||
state.writerswaiting--;
|
||||
}
|
||||
state.writerswaiting--;
|
||||
}
|
||||
|
||||
state.writing++;
|
||||
state.writing++;
|
||||
}
|
||||
|
||||
/* It's safe (and necessary) to simply convert this writer to a reader without
|
||||
@ -240,88 +237,87 @@ void RWLock_local::upgrade_to_write()
|
||||
void RWLock_local::downgrade_to_read()
|
||||
{
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
|
||||
state.writing--;
|
||||
state.writing--;
|
||||
|
||||
if (state.readerswaiting > 0)
|
||||
okToRead.notify_all();
|
||||
if (state.readerswaiting > 0)
|
||||
okToRead.notify_all();
|
||||
|
||||
state.reading++;
|
||||
state.reading++;
|
||||
|
||||
#ifdef DEBUG
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
CHECKSAFETY();
|
||||
CHECKLIVENESS();
|
||||
#endif
|
||||
mutex.unlock();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
void RWLock_local::lock()
|
||||
{
|
||||
mutex.lock();
|
||||
mutex.lock();
|
||||
}
|
||||
|
||||
void RWLock_local::unlock()
|
||||
{
|
||||
mutex.unlock();
|
||||
mutex.unlock();
|
||||
}
|
||||
|
||||
int RWLock_local::getWriting()
|
||||
{
|
||||
return state.writing;
|
||||
return state.writing;
|
||||
}
|
||||
|
||||
int RWLock_local::getReading()
|
||||
{
|
||||
return state.reading;
|
||||
return state.reading;
|
||||
}
|
||||
|
||||
int RWLock_local::getWritersWaiting()
|
||||
{
|
||||
return state.writerswaiting;
|
||||
return state.writerswaiting;
|
||||
}
|
||||
|
||||
int RWLock_local::getReadersWaiting()
|
||||
{
|
||||
return state.readerswaiting;
|
||||
return state.readerswaiting;
|
||||
}
|
||||
|
||||
ScopedRWLock_local::ScopedRWLock_local(RWLock_local* l, rwlock_mode m)
|
||||
{
|
||||
thelock = l;
|
||||
mode = m;
|
||||
assert(m == R || m == W);
|
||||
locked = false;
|
||||
lock();
|
||||
thelock = l;
|
||||
mode = m;
|
||||
assert(m == R || m == W);
|
||||
locked = false;
|
||||
lock();
|
||||
}
|
||||
|
||||
ScopedRWLock_local::~ScopedRWLock_local()
|
||||
{
|
||||
if (locked)
|
||||
unlock();
|
||||
if (locked)
|
||||
unlock();
|
||||
}
|
||||
|
||||
void ScopedRWLock_local::lock()
|
||||
{
|
||||
if (mode == R)
|
||||
thelock->read_lock();
|
||||
else
|
||||
thelock->write_lock();
|
||||
if (mode == R)
|
||||
thelock->read_lock();
|
||||
else
|
||||
thelock->write_lock();
|
||||
|
||||
locked = true;
|
||||
locked = true;
|
||||
}
|
||||
|
||||
void ScopedRWLock_local::unlock()
|
||||
{
|
||||
if (mode == R)
|
||||
thelock->read_unlock();
|
||||
else
|
||||
thelock->write_unlock();
|
||||
|
||||
locked = false;
|
||||
}
|
||||
if (mode == R)
|
||||
thelock->read_unlock();
|
||||
else
|
||||
thelock->write_unlock();
|
||||
|
||||
locked = false;
|
||||
}
|
||||
|
||||
} // namespace rwlock
|
||||
|
@ -37,7 +37,6 @@
|
||||
|
||||
namespace rwlock
|
||||
{
|
||||
|
||||
/** @brief Implements RW locks for use across threads & processes
|
||||
*
|
||||
* Implements RW locks for use across threads & processes. Every
|
||||
@ -56,144 +55,142 @@ namespace rwlock
|
||||
* eventually deadlock the set of processes that share the same key obviously.
|
||||
*/
|
||||
|
||||
|
||||
class RWLock_local
|
||||
{
|
||||
public:
|
||||
|
||||
class not_excl : public std::exception
|
||||
public:
|
||||
class not_excl : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
return "not_excl";
|
||||
}
|
||||
};
|
||||
return "not_excl";
|
||||
}
|
||||
};
|
||||
|
||||
class wouldblock : public std::exception
|
||||
class wouldblock : public std::exception
|
||||
{
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
public:
|
||||
virtual const char* what() const throw()
|
||||
{
|
||||
return "wouldblock";
|
||||
}
|
||||
};
|
||||
return "wouldblock";
|
||||
}
|
||||
};
|
||||
|
||||
/** @brief Keyed constructor.
|
||||
*
|
||||
* Instantiate an RWLock_local with the given key. All instances that
|
||||
* share a key share the same lock.
|
||||
*
|
||||
* @param key The key
|
||||
* @param excl If true and this is the first instance with the
|
||||
* supplied key, it will return holding the write lock. If true and
|
||||
* this is not the first instance, it will throw not_excl. The intent
|
||||
* is similar to the IPC_EXCL flag in the sem/shm implementations.
|
||||
*/
|
||||
EXPORT RWLock_local();
|
||||
/** @brief Keyed constructor.
|
||||
*
|
||||
* Instantiate an RWLock_local with the given key. All instances that
|
||||
* share a key share the same lock.
|
||||
*
|
||||
* @param key The key
|
||||
* @param excl If true and this is the first instance with the
|
||||
* supplied key, it will return holding the write lock. If true and
|
||||
* this is not the first instance, it will throw not_excl. The intent
|
||||
* is similar to the IPC_EXCL flag in the sem/shm implementations.
|
||||
*/
|
||||
EXPORT RWLock_local();
|
||||
|
||||
EXPORT ~RWLock_local();
|
||||
EXPORT ~RWLock_local();
|
||||
|
||||
/** @brief Grab a read lock
|
||||
*
|
||||
* Grab a read lock. This will block iff writers are waiting or
|
||||
* a writer is active.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void read_lock();
|
||||
/** @brief Grab a read lock
|
||||
*
|
||||
* Grab a read lock. This will block iff writers are waiting or
|
||||
* a writer is active.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void read_lock();
|
||||
|
||||
/** @brief Release a read lock.
|
||||
*
|
||||
* Release a read lock.
|
||||
*/
|
||||
EXPORT void read_unlock();
|
||||
/** @brief Release a read lock.
|
||||
*
|
||||
* Release a read lock.
|
||||
*/
|
||||
EXPORT void read_unlock();
|
||||
|
||||
/** @brief Grab a write lock
|
||||
*
|
||||
* Grab a write lock. This will block while another writer or reader is
|
||||
* active and will have exclusive access on waking.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void write_lock();
|
||||
/** @brief Grab a write lock
|
||||
*
|
||||
* Grab a write lock. This will block while another writer or reader is
|
||||
* active and will have exclusive access on waking.
|
||||
*
|
||||
* @param block (For testing only) If false, will throw
|
||||
* wouldblock instead of blocking
|
||||
*/
|
||||
EXPORT void write_lock();
|
||||
|
||||
/** @brief Release a write lock.
|
||||
*
|
||||
* Release a write lock.
|
||||
*/
|
||||
EXPORT void write_unlock();
|
||||
/** @brief Release a write lock.
|
||||
*
|
||||
* Release a write lock.
|
||||
*/
|
||||
EXPORT void write_unlock();
|
||||
|
||||
/** @brief Upgrade a read lock to a write lock
|
||||
*
|
||||
* Upgrade a read lock to a write lock. It may have to block
|
||||
* if there are other readers currently reading. No guarantees of atomicity.
|
||||
*/
|
||||
EXPORT void upgrade_to_write();
|
||||
/** @brief Upgrade a read lock to a write lock
|
||||
*
|
||||
* Upgrade a read lock to a write lock. It may have to block
|
||||
* if there are other readers currently reading. No guarantees of atomicity.
|
||||
*/
|
||||
EXPORT void upgrade_to_write();
|
||||
|
||||
/** @brief Downgrade a write lock to a read lock
|
||||
*
|
||||
* Downgrade a write lock to a read lock. The conversion happens
|
||||
* atomically.
|
||||
*/
|
||||
EXPORT void downgrade_to_read();
|
||||
/** @brief Downgrade a write lock to a read lock
|
||||
*
|
||||
* Downgrade a write lock to a read lock. The conversion happens
|
||||
* atomically.
|
||||
*/
|
||||
EXPORT void downgrade_to_read();
|
||||
|
||||
/* These are for white box testing only */
|
||||
EXPORT void lock();
|
||||
EXPORT void unlock();
|
||||
EXPORT int getWriting();
|
||||
EXPORT int getReading();
|
||||
EXPORT int getWritersWaiting();
|
||||
EXPORT int getReadersWaiting();
|
||||
|
||||
private:
|
||||
// Not copyable
|
||||
RWLock_local(const RWLock_local& rwl);
|
||||
RWLock_local& operator=(const RWLock_local& rwl);
|
||||
|
||||
/// the layout of the shmseg
|
||||
struct State
|
||||
{
|
||||
int writerswaiting, writing, readerswaiting, reading;
|
||||
} state;
|
||||
boost::mutex mutex;
|
||||
boost::condition okToRead;
|
||||
boost::condition okToWrite;
|
||||
/* These are for white box testing only */
|
||||
EXPORT void lock();
|
||||
EXPORT void unlock();
|
||||
EXPORT int getWriting();
|
||||
EXPORT int getReading();
|
||||
EXPORT int getWritersWaiting();
|
||||
EXPORT int getReadersWaiting();
|
||||
|
||||
private:
|
||||
// Not copyable
|
||||
RWLock_local(const RWLock_local& rwl);
|
||||
RWLock_local& operator=(const RWLock_local& rwl);
|
||||
|
||||
/// the layout of the shmseg
|
||||
struct State
|
||||
{
|
||||
int writerswaiting, writing, readerswaiting, reading;
|
||||
} state;
|
||||
boost::mutex mutex;
|
||||
boost::condition okToRead;
|
||||
boost::condition okToWrite;
|
||||
};
|
||||
|
||||
enum rwlock_mode
|
||||
{
|
||||
R,
|
||||
W
|
||||
R,
|
||||
W
|
||||
};
|
||||
|
||||
class ScopedRWLock_local
|
||||
{
|
||||
public:
|
||||
ScopedRWLock_local(RWLock_local*, rwlock_mode);
|
||||
~ScopedRWLock_local();
|
||||
public:
|
||||
ScopedRWLock_local(RWLock_local*, rwlock_mode);
|
||||
~ScopedRWLock_local();
|
||||
|
||||
void lock();
|
||||
void unlock();
|
||||
void lock();
|
||||
void unlock();
|
||||
|
||||
private:
|
||||
explicit ScopedRWLock_local() {}
|
||||
explicit ScopedRWLock_local(const ScopedRWLock_local&) {}
|
||||
ScopedRWLock_local& operator=(const ScopedRWLock_local&)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
RWLock_local* thelock;
|
||||
rwlock_mode mode;
|
||||
bool locked;
|
||||
private:
|
||||
explicit ScopedRWLock_local()
|
||||
{
|
||||
}
|
||||
explicit ScopedRWLock_local(const ScopedRWLock_local&)
|
||||
{
|
||||
}
|
||||
ScopedRWLock_local& operator=(const ScopedRWLock_local&)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
RWLock_local* thelock;
|
||||
rwlock_mode mode;
|
||||
bool locked;
|
||||
};
|
||||
|
||||
|
||||
#undef EXPORT
|
||||
|
||||
}
|
||||
|
||||
} // namespace rwlock
|
||||
|
@ -46,255 +46,251 @@ int threadStop;
|
||||
|
||||
static void* RWRunner(void* arg)
|
||||
{
|
||||
struct timeval tv;
|
||||
int op, op2, interval;
|
||||
RWLock* rwlock;
|
||||
struct timeval tv;
|
||||
int op, op2, interval;
|
||||
RWLock* rwlock;
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
rwlock = new RWLock(reinterpret_cast<int64_t>(arg));
|
||||
gettimeofday(&tv, NULL);
|
||||
rwlock = new RWLock(reinterpret_cast<int64_t>(arg));
|
||||
|
||||
while (!threadStop)
|
||||
while (!threadStop)
|
||||
{
|
||||
op = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 10;
|
||||
|
||||
if (op < 8) // read
|
||||
{
|
||||
op = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 10;
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->read_lock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
|
||||
if (op < 8) // read
|
||||
{
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->read_lock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
if (op2)
|
||||
{
|
||||
rwlock->upgrade_to_write();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
rwlock->write_unlock();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* For testing the lock recovery code in the BRM workernodes */
|
||||
/*
|
||||
int crash = rand_r((uint32_t *) &tv.tv_usec) % 100;
|
||||
if (crash > 0) // 1% chance of crashing
|
||||
rwlock->read_unlock();
|
||||
*/
|
||||
}
|
||||
}
|
||||
else if (op < 9) // write
|
||||
{
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->write_lock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
if (op2)
|
||||
{
|
||||
rwlock->downgrade_to_read();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
if (op2)
|
||||
{
|
||||
rwlock->upgrade_to_write();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
rwlock->write_unlock();
|
||||
}
|
||||
else
|
||||
{
|
||||
/* For testing the lock recovery code in the BRM workernodes */
|
||||
/*
|
||||
int crash = rand_r((uint32_t *) &tv.tv_usec) % 100;
|
||||
if (crash > 0) // 1% chance of crashing
|
||||
rwlock->read_unlock();
|
||||
}
|
||||
else
|
||||
|
||||
rwlock->write_unlock();
|
||||
}
|
||||
else if (op == 9) // delete
|
||||
{
|
||||
delete rwlock;
|
||||
rwlock = new RWLock(reinterpret_cast<int64_t>(arg));
|
||||
}
|
||||
*/
|
||||
}
|
||||
}
|
||||
else if (op < 9) // write
|
||||
{
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->write_lock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
|
||||
delete rwlock;
|
||||
pthread_exit(0);
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
if (op2)
|
||||
{
|
||||
rwlock->downgrade_to_read();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
rwlock->read_unlock();
|
||||
}
|
||||
else
|
||||
|
||||
rwlock->write_unlock();
|
||||
}
|
||||
else if (op == 9) // delete
|
||||
{
|
||||
delete rwlock;
|
||||
rwlock = new RWLock(reinterpret_cast<int64_t>(arg));
|
||||
}
|
||||
}
|
||||
|
||||
delete rwlock;
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
static void* RWRunner_local(void* arg)
|
||||
{
|
||||
struct timeval tv;
|
||||
int op, op2, interval;
|
||||
RWLock_local* rwlock = reinterpret_cast<RWLock_local*>(arg);
|
||||
struct timeval tv;
|
||||
int op, op2, interval;
|
||||
RWLock_local* rwlock = reinterpret_cast<RWLock_local*>(arg);
|
||||
|
||||
gettimeofday(&tv, NULL);
|
||||
gettimeofday(&tv, NULL);
|
||||
|
||||
while (!threadStop)
|
||||
while (!threadStop)
|
||||
{
|
||||
op = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 10;
|
||||
|
||||
// cout << "doing op " << op << endl;
|
||||
switch (op)
|
||||
{
|
||||
op = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 10;
|
||||
case 0: // read
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
{
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->read_lock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
// cout << "doing op " << op << endl;
|
||||
switch (op)
|
||||
if (op2)
|
||||
{
|
||||
case 0: //read
|
||||
case 1:
|
||||
case 2:
|
||||
case 3:
|
||||
case 4:
|
||||
case 5:
|
||||
case 6:
|
||||
case 7:
|
||||
case 8:
|
||||
{
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->read_lock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
if (op2)
|
||||
{
|
||||
rwlock->upgrade_to_write();
|
||||
// rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
// rwlock->unlock();
|
||||
usleep(interval);
|
||||
rwlock->write_unlock();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 9: //write
|
||||
{
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->write_lock();
|
||||
// rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
// rwlock->unlock();
|
||||
usleep(interval);
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
if (op2)
|
||||
{
|
||||
rwlock->downgrade_to_read();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
rwlock->read_unlock();
|
||||
}
|
||||
else
|
||||
rwlock->write_unlock();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
rwlock->upgrade_to_write();
|
||||
// rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
// rwlock->unlock();
|
||||
usleep(interval);
|
||||
rwlock->write_unlock();
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
case 9: // write
|
||||
{
|
||||
interval = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 100000;
|
||||
rwlock->write_lock();
|
||||
// rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
// rwlock->unlock();
|
||||
usleep(interval);
|
||||
op2 = rand_r(reinterpret_cast<uint32_t*>(&tv.tv_usec)) % 2;
|
||||
|
||||
if (op2)
|
||||
{
|
||||
rwlock->downgrade_to_read();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() > 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
rwlock->unlock();
|
||||
usleep(interval);
|
||||
rwlock->read_unlock();
|
||||
}
|
||||
else
|
||||
rwlock->write_unlock();
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
default: break;
|
||||
}
|
||||
}
|
||||
|
||||
pthread_exit(0);
|
||||
pthread_exit(0);
|
||||
}
|
||||
|
||||
|
||||
class RWLockTest : public CppUnit::TestFixture
|
||||
{
|
||||
CPPUNIT_TEST_SUITE(RWLockTest);
|
||||
|
||||
CPPUNIT_TEST_SUITE(RWLockTest);
|
||||
CPPUNIT_TEST(LongRWTest_1);
|
||||
// CPPUNIT_TEST(LongRWLocalTest_1);
|
||||
|
||||
CPPUNIT_TEST(LongRWTest_1);
|
||||
//CPPUNIT_TEST(LongRWLocalTest_1);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
private:
|
||||
public:
|
||||
void LongRWTest_1()
|
||||
{
|
||||
int key = 0x20000; // the extentmap key
|
||||
|
||||
private:
|
||||
public:
|
||||
void LongRWTest_1()
|
||||
const int threadCount = 30;
|
||||
int i;
|
||||
pthread_t threads[threadCount];
|
||||
|
||||
cerr << endl
|
||||
<< "Multithreaded RWLock test. "
|
||||
"This runs for 60 minutes."
|
||||
<< endl;
|
||||
|
||||
threadStop = 0;
|
||||
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
int key = 0x20000; // the extentmap key
|
||||
|
||||
const int threadCount = 30;
|
||||
int i;
|
||||
pthread_t threads[threadCount];
|
||||
|
||||
cerr << endl << "Multithreaded RWLock test. "
|
||||
"This runs for 60 minutes." << endl;
|
||||
|
||||
threadStop = 0;
|
||||
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
if (pthread_create(&threads[i], NULL, RWRunner,
|
||||
reinterpret_cast<void*>(key)) < 0)
|
||||
throw logic_error("Error creating threads for the ipc test");
|
||||
}
|
||||
|
||||
sleep(3600);
|
||||
threadStop = 1;
|
||||
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
cerr << "Waiting for thread #" << i << endl;
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
if (pthread_create(&threads[i], NULL, RWRunner, reinterpret_cast<void*>(key)) < 0)
|
||||
throw logic_error("Error creating threads for the ipc test");
|
||||
}
|
||||
|
||||
void LongRWLocalTest_1()
|
||||
sleep(3600);
|
||||
threadStop = 1;
|
||||
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
const int threadCount = 40;
|
||||
int i;
|
||||
pthread_t threads[threadCount];
|
||||
RWLock_local rwlock;
|
||||
cerr << "Waiting for thread #" << i << endl;
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
}
|
||||
|
||||
cerr << endl << "Multithreaded RWLock_local test. "
|
||||
"This runs for 30-60 seconds." << endl;
|
||||
void LongRWLocalTest_1()
|
||||
{
|
||||
const int threadCount = 40;
|
||||
int i;
|
||||
pthread_t threads[threadCount];
|
||||
RWLock_local rwlock;
|
||||
|
||||
threadStop = 0;
|
||||
cerr << endl
|
||||
<< "Multithreaded RWLock_local test. "
|
||||
"This runs for 30-60 seconds."
|
||||
<< endl;
|
||||
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
if (pthread_create(&threads[i], NULL, RWRunner_local,
|
||||
reinterpret_cast<void*>(&rwlock)) < 0)
|
||||
throw logic_error("Error creating threads for the local test");
|
||||
}
|
||||
threadStop = 0;
|
||||
|
||||
sleep(30);
|
||||
threadStop = 1;
|
||||
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
cerr << "Waiting for thread #" << i << endl;
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
if (pthread_create(&threads[i], NULL, RWRunner_local, reinterpret_cast<void*>(&rwlock)) < 0)
|
||||
throw logic_error("Error creating threads for the local test");
|
||||
}
|
||||
|
||||
sleep(30);
|
||||
threadStop = 1;
|
||||
|
||||
for (i = 0; i < threadCount; i++)
|
||||
{
|
||||
cerr << "Waiting for thread #" << i << endl;
|
||||
pthread_join(threads[i], NULL);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( RWLockTest );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(RWLockTest);
|
||||
|
||||
#include <cppunit/extensions/TestFactoryRegistry.h>
|
||||
#include <cppunit/ui/text/TestRunner.h>
|
||||
|
||||
int main( int argc, char** argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
CppUnit::TextUi::TestRunner runner;
|
||||
CppUnit::TestFactoryRegistry& registry = CppUnit::TestFactoryRegistry::getRegistry();
|
||||
runner.addTest( registry.makeTest() );
|
||||
bool wasSuccessful = runner.run( "", false );
|
||||
return (wasSuccessful ? 0 : 1);
|
||||
CppUnit::TextUi::TestRunner runner;
|
||||
CppUnit::TestFactoryRegistry& registry = CppUnit::TestFactoryRegistry::getRegistry();
|
||||
runner.addTest(registry.makeTest());
|
||||
bool wasSuccessful = runner.run("", false);
|
||||
return (wasSuccessful ? 0 : 1);
|
||||
}
|
||||
|
||||
|
||||
|
@ -40,143 +40,138 @@ using namespace std;
|
||||
|
||||
class RWLockTest : public CppUnit::TestFixture
|
||||
{
|
||||
CPPUNIT_TEST_SUITE(RWLockTest);
|
||||
|
||||
CPPUNIT_TEST_SUITE(RWLockTest);
|
||||
CPPUNIT_TEST(RWTest_1);
|
||||
|
||||
CPPUNIT_TEST(RWTest_1);
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
|
||||
CPPUNIT_TEST_SUITE_END();
|
||||
private:
|
||||
public:
|
||||
void RWTest_1()
|
||||
{
|
||||
RWLock* rwlock;
|
||||
int caughtException = 0;
|
||||
int key;
|
||||
|
||||
private:
|
||||
public:
|
||||
void RWTest_1()
|
||||
srand(time(NULL));
|
||||
key = rand();
|
||||
|
||||
rwlock = new RWLock(key);
|
||||
rwlock->read_lock(false);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->read_lock(false);
|
||||
rwlock->read_lock(false);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 3);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
|
||||
try
|
||||
{
|
||||
RWLock* rwlock;
|
||||
int caughtException = 0;
|
||||
int key;
|
||||
|
||||
srand(time(NULL));
|
||||
key = rand();
|
||||
|
||||
rwlock = new RWLock(key);
|
||||
rwlock->read_lock(false);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->read_lock(false);
|
||||
rwlock->read_lock(false);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 3);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
|
||||
try
|
||||
{
|
||||
rwlock->write_lock(false);
|
||||
}
|
||||
catch (RWLock::wouldblock& e)
|
||||
{
|
||||
caughtException++;
|
||||
// cerr << endl << "Caught expected exception: " << e.what() << endl;
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(caughtException == 1);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 3);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->read_unlock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 2);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->read_unlock();
|
||||
rwlock->read_unlock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->write_lock(false);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
|
||||
try
|
||||
{
|
||||
rwlock->write_lock(false);
|
||||
}
|
||||
catch (RWLock::wouldblock& e)
|
||||
{
|
||||
caughtException++;
|
||||
// cerr << endl << "Caught expected exception: " << e.what() << endl;
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(caughtException == 2);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
|
||||
try
|
||||
{
|
||||
rwlock->read_lock(false);
|
||||
}
|
||||
catch (RWLock::wouldblock& e)
|
||||
{
|
||||
caughtException++;
|
||||
// cerr << endl << "Caught expected exception: " << e.what() << endl;
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(caughtException == 3);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
delete rwlock;
|
||||
rwlock = new RWLock(key);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->write_unlock();
|
||||
delete rwlock;
|
||||
rwlock->write_lock(false);
|
||||
}
|
||||
catch (RWLock::wouldblock& e)
|
||||
{
|
||||
caughtException++;
|
||||
// cerr << endl << "Caught expected exception: " << e.what() << endl;
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(caughtException == 1);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 3);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->read_unlock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 2);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->read_unlock();
|
||||
rwlock->read_unlock();
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->write_lock(false);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
|
||||
try
|
||||
{
|
||||
rwlock->write_lock(false);
|
||||
}
|
||||
catch (RWLock::wouldblock& e)
|
||||
{
|
||||
caughtException++;
|
||||
// cerr << endl << "Caught expected exception: " << e.what() << endl;
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(caughtException == 2);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
|
||||
try
|
||||
{
|
||||
rwlock->read_lock(false);
|
||||
}
|
||||
catch (RWLock::wouldblock& e)
|
||||
{
|
||||
caughtException++;
|
||||
// cerr << endl << "Caught expected exception: " << e.what() << endl;
|
||||
}
|
||||
|
||||
CPPUNIT_ASSERT(caughtException == 3);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
delete rwlock;
|
||||
rwlock = new RWLock(key);
|
||||
rwlock->lock();
|
||||
CPPUNIT_ASSERT(rwlock->getReading() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWriting() == 1);
|
||||
CPPUNIT_ASSERT(rwlock->getReadersWaiting() == 0);
|
||||
CPPUNIT_ASSERT(rwlock->getWritersWaiting() == 0);
|
||||
rwlock->unlock();
|
||||
rwlock->write_unlock();
|
||||
delete rwlock;
|
||||
}
|
||||
};
|
||||
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION( RWLockTest );
|
||||
CPPUNIT_TEST_SUITE_REGISTRATION(RWLockTest);
|
||||
|
||||
#include <cppunit/extensions/TestFactoryRegistry.h>
|
||||
#include <cppunit/ui/text/TestRunner.h>
|
||||
|
||||
int main( int argc, char** argv)
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
CppUnit::TextUi::TestRunner runner;
|
||||
CppUnit::TestFactoryRegistry& registry = CppUnit::TestFactoryRegistry::getRegistry();
|
||||
runner.addTest( registry.makeTest() );
|
||||
bool wasSuccessful = runner.run( "", false );
|
||||
return (wasSuccessful ? 0 : 1);
|
||||
CppUnit::TextUi::TestRunner runner;
|
||||
CppUnit::TestFactoryRegistry& registry = CppUnit::TestFactoryRegistry::getRegistry();
|
||||
runner.addTest(registry.makeTest());
|
||||
bool wasSuccessful = runner.run("", false);
|
||||
return (wasSuccessful ? 0 : 1);
|
||||
}
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user