Fawkes API  Fawkes Development Version
fawkes::ScopedRWLock Class Reference

Scoped read/write lock. More...

#include <>>

Public Types

enum  LockType { LOCK_WRITE, LOCK_READ }
 What to lock for. More...
 

Public Member Functions

 ScopedRWLock (RefPtr< ReadWriteLock > rwlock, LockType lock_type=LOCK_WRITE, bool initially_lock=true)
 Constructor. More...
 
 ScopedRWLock (ReadWriteLock *rwlock, LockType lock_type=LOCK_WRITE, bool initially_lock=true)
 Constructor. More...
 
 ~ScopedRWLock ()
 Destructor. More...
 
void relock ()
 Lock this rwlock, again. More...
 
void unlock ()
 Unlock the rwlock. More...
 

Detailed Description

Scoped read/write lock.

This class is a convenience class which can help you prevent quite a few headaches. Consider the following code.

void my_function()
{
rwlock->lock_for_write();
for (int i = 0; i < LIMIT; ++i) {
if ( failure ) {
rwlock->unlock();
}
}
switch ( someval ) {
VALA:
rwlock->unlock();
return;
VALB:
do_something();
}
try {
do_function_that_throws_exceptions();
} catch (Exception &e) {
rwlock->unlock();
throw;
}
rwlock->unlock();
}

This is not a complete list of examples but as you see if you have many exit points in a function it becomes more and more work to have correct locking behavior.

This is a lot simpler with the ScopedRWLock. The ScopedRWLock locks the given ReadWriteLock on creation, and unlocks it in the destructor. If you now have a read/write locker on the stack as integral type the destructor is called automagically on function exit and thus the lock is appropriately unlocked. The code would look like this:

void my_function()
{
ScopedRWLock lock(rwlock);
// do anything, no need to call rwlock->lock_*()/unlock() if only has to be
// called on entering and exiting the function.
}
Author
Tim Niemueller

Definition at line 33 of file scoped_rwlock.h.

Member Enumeration Documentation

◆ LockType

What to lock for.

Enumerator
LOCK_WRITE 

Lock for writing.

LOCK_READ 

Lock for reading.

Definition at line 37 of file scoped_rwlock.h.

Constructor & Destructor Documentation

◆ ScopedRWLock() [1/2]

fawkes::ScopedRWLock::ScopedRWLock ( RefPtr< ReadWriteLock rwlock,
ScopedRWLock::LockType  lock_type = LOCK_WRITE,
bool  initially_lock = true 
)

Constructor.

Parameters
rwlockReadWriteLock to lock/unlock appropriately.
initially_locktrue to lock the rwlock in the constructor, false to not lock
lock_typelocking type, lock either for writing or for reading

Definition at line 95 of file scoped_rwlock.cpp.

References LOCK_WRITE.

◆ ScopedRWLock() [2/2]

fawkes::ScopedRWLock::ScopedRWLock ( ReadWriteLock rwlock,
ScopedRWLock::LockType  lock_type = LOCK_WRITE,
bool  initially_lock = true 
)

Constructor.

Parameters
rwlockReadWriteLock to lock/unlock appropriately.
initially_locktrue to lock the rwlock in the constructor, false to not lock
lock_typelocking type, lock either for writing or for reading

Definition at line 118 of file scoped_rwlock.cpp.

References fawkes::ReadWriteLock::lock_for_read(), fawkes::ReadWriteLock::lock_for_write(), and LOCK_WRITE.

◆ ~ScopedRWLock()

fawkes::ScopedRWLock::~ScopedRWLock ( )

Destructor.

Definition at line 135 of file scoped_rwlock.cpp.

References fawkes::ReadWriteLock::unlock().

Member Function Documentation

◆ relock()

void fawkes::ScopedRWLock::relock ( )

Lock this rwlock, again.

Use this if you unlocked the rwlock from the outside.

Definition at line 151 of file scoped_rwlock.cpp.

References fawkes::ReadWriteLock::lock_for_read(), fawkes::ReadWriteLock::lock_for_write(), and LOCK_WRITE.

◆ unlock()

void fawkes::ScopedRWLock::unlock ( )

Unlock the rwlock.

Definition at line 172 of file scoped_rwlock.cpp.

References fawkes::ReadWriteLock::unlock().


The documentation for this class was generated from the following files: