Fawkes API  Fawkes Development Version
scoped_rwlock.h
00001 
00002 /***************************************************************************
00003  *  scoped_rwlock.h - Scoped read/write lock
00004  *
00005  *  Created: Mon Jan 10 11:42:56 2011
00006  *  Copyright  2006-2010  Tim Niemueller [www.niemueller.de]
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __CORE_THREADING_SCOPED_RWLOCK_H_
00025 #define __CORE_THREADING_SCOPED_RWLOCK_H_
00026 
00027 #include <core/utils/refptr.h>
00028 
00029 namespace fawkes {
00030 
00031 class ReadWriteLock;
00032 
00033 class ScopedRWLock
00034 {
00035  public:
00036   /** What to lock for. */
00037   typedef enum {
00038     LOCK_WRITE, ///< Lock for writing
00039     LOCK_READ   ///< Lock for reading
00040   } LockType;
00041 
00042   ScopedRWLock(RefPtr<ReadWriteLock> rwlock, LockType lock_type = LOCK_WRITE,
00043                bool initially_lock = true);
00044   ScopedRWLock(ReadWriteLock *rwlock, LockType lock_type = LOCK_WRITE,
00045                bool initially_lock = true);
00046   ~ScopedRWLock();
00047 
00048   void relock();
00049   void unlock();
00050 
00051  private:
00052   LockType               __lock_type;
00053   bool                   __locked;
00054   RefPtr<ReadWriteLock>  __refrwlock;
00055   ReadWriteLock         *__rawrwlock;
00056 };
00057 
00058 
00059 } // end namespace fawkes
00060 
00061 #endif