00001 /* --------------------------------------------------------------------------- 00002 Phission : 00003 Realtime Vision Processing System 00004 00005 Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu) 00006 University of Massachusetts at Lowell, 00007 Laboratory for Artificial Intelligence and Robotics 00008 00009 This file is part of Phission. 00010 00011 Phission is free software; you can redistribute it and/or modify 00012 it under the terms of the GNU Lesser General Public License as published by 00013 the Free Software Foundation; either version 2 of the License, or 00014 (at your option) any later version. 00015 00016 Phission 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 Lesser General Public License for more details. 00020 00021 You should have received a copy of the GNU Lesser General Public License 00022 along with Phission; if not, write to the Free Software 00023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00024 00025 --------------------------------------------------------------------------- */ 00026 #ifndef _PHRWLOCK_H 00027 #define _PHRWLOCK_H 00028 00029 #include <phStandard.h> 00030 #include <phStdint.h> 00031 #include <phMutex.h> 00032 00038 /* ------------------------------------------------------------------------- */ 00039 /* Read Write Macros for the phRWLock class */ 00040 /* ------------------------------------------------------------------------- */ 00041 /* These operate on any read/write lock */ 00042 /* ------------------------------------------------------------------------- */ 00048 #define phRWLOCK_READLOCK(rw,l) \ 00049 rc = rw.readLock(); \ 00050 phCHECK_RC(rc,NULL,"rw.readLock()"); \ 00051 ++l; \ 00052 00053 00056 #define phRWLOCK_READLOCK_LOOSE(rw,l) \ 00057 rc = rw.readLock(); \ 00058 phPRINT_RC(rc,NULL,"rw.readLock()"); \ 00059 if (rc == phSUCCESS) ++l; \ 00060 00061 00064 #define phRWLOCK_WRITELOCK(rw,l) \ 00065 rc = rw.writeLock(); \ 00066 phCHECK_RC(rc,NULL,"rw.writeLock()"); \ 00067 ++l; \ 00068 00069 00072 #define phRWLOCK_WRITELOCK_LOOSE(rw,l) \ 00073 rc = rw.writeLock(); \ 00074 phPRINT_RC(rc,NULL,"rw.writeLock()"); \ 00075 if (rc == phSUCCESS) ++l; \ 00076 00077 00080 #define phRWLOCK_UNLOCK(rw,l) \ 00081 if (l > 0) \ 00082 { \ 00083 rc = rw.rwUnlock(); \ 00084 --l; \ 00085 phCHECK_RC(rc,NULL,"rw.rwUnlock()"); \ 00086 } 00087 00091 #define phRWLOCK_UNLOCK_LOOSE(rw,l) \ 00092 if (l > 0) \ 00093 { \ 00094 rc = rw.rwUnlock(); \ 00095 phPRINT_RC(rc,NULL,"rw.rwUnlock()"); \ 00096 if (rc == phSUCCESS) --l; \ 00097 } 00098 00102 #define phRWLOCK_UNLOCK_ERROR(rw,l) \ 00103 if (l > 0) \ 00104 { \ 00105 do { \ 00106 rc = rw.rwUnlock(); \ 00107 phPRINT_RC(rc,NULL,"rw.rwUnlock()"); \ 00108 if (rc == phSUCCESS) --l; \ 00109 } while ((l > 0) && (rc == phSUCCESS)); \ 00110 } 00111 00112 /* ------------------------------------------------------------------------- */ 00117 #define phRWLock_OWNED 1 00118 00127 #define phRWLock_NOTLOCKED 1 00128 /* ------------------------------------------------------------------------- */ 00134 typedef struct ph_rwlock_info_t *ph_rwlock_info; 00135 00136 /* ------------------------------------------------------------------------- */ 00188 PHAPI(class) phRWLock : public phObject 00189 { 00190 private: 00191 phMutex m_mutex; 00192 00198 ph_rwlock_info m_info; 00199 00204 uint32_t m_readers; 00205 00210 uintmax_t m_writer; 00217 uint32_t m_writer_nlocks; 00218 00222 void init_lock(); 00223 00224 public: 00228 phRWLock(); 00229 ~phRWLock(); 00230 00246 int readLock (); 00268 int tryReadLock (); 00278 int writeLock (); 00300 int tryWriteLock(); 00311 int rwUnlock (); 00312 }; 00313 /* end \addtogroup ThreadSync */ 00315 00316 #endif /* _PHRWLOCK_H */
Copyright (C) 2002 - 2007 |
Philip D.S. Thoren ( pthoren@users.sourceforge.net ) University Of Massachusetts at Lowell Robotics Lab |