ReadWriteMutex 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef _OPENTHREADS_READWRITEMUTEX_
  14. #define _OPENTHREADS_READWRITEMUTEX_
  15. #include <OpenThreads/Thread>
  16. #include <OpenThreads/ReentrantMutex>
  17. namespace OpenThreads {
  18. class ReadWriteMutex
  19. {
  20. public:
  21. ReadWriteMutex():
  22. _readCount(0) {}
  23. virtual ~ReadWriteMutex() {}
  24. virtual int readLock()
  25. {
  26. OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
  27. int result = 0;
  28. if (_readCount==0)
  29. {
  30. result = _readWriteMutex.lock();
  31. }
  32. ++_readCount;
  33. return result;
  34. }
  35. virtual int readUnlock()
  36. {
  37. OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
  38. int result = 0;
  39. if (_readCount>0)
  40. {
  41. --_readCount;
  42. if (_readCount==0)
  43. {
  44. result = _readWriteMutex.unlock();
  45. }
  46. }
  47. return result;
  48. }
  49. virtual int writeLock()
  50. {
  51. return _readWriteMutex.lock();
  52. }
  53. virtual int writeUnlock()
  54. {
  55. return _readWriteMutex.unlock();
  56. }
  57. protected:
  58. ReadWriteMutex(const ReadWriteMutex&) {}
  59. ReadWriteMutex& operator = (const ReadWriteMutex&) { return *(this); }
  60. #if 0
  61. ReentrantMutex _readWriteMutex;
  62. ReentrantMutex _readCountMutex;
  63. #else
  64. OpenThreads::Mutex _readWriteMutex;
  65. OpenThreads::Mutex _readCountMutex;
  66. #endif
  67. unsigned int _readCount;
  68. };
  69. class ScopedReadLock
  70. {
  71. public:
  72. ScopedReadLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.readLock(); }
  73. ~ScopedReadLock() { _mutex.readUnlock(); }
  74. protected:
  75. ReadWriteMutex& _mutex;
  76. ScopedReadLock& operator = (const ScopedReadLock&) { return *this; }
  77. };
  78. class ScopedWriteLock
  79. {
  80. public:
  81. ScopedWriteLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.writeLock(); }
  82. ~ScopedWriteLock() { _mutex.writeUnlock(); }
  83. protected:
  84. ReadWriteMutex& _mutex;
  85. ScopedWriteLock& operator = (const ScopedWriteLock&) { return *this; }
  86. };
  87. }
  88. #endif