r7953 - in /trunk/licq: include/licq/thread/readwritemutex.h src/thread/readwritemutex.cpp src/thread/readwritemutex_debug.cpp

[email protected]
Newsgroups gmane.network.licq.cvs
Message-ID <20101031081855.7286C224B82@thejon>
Author: erijo
Date: Sun Oct 31 17:18:48 2010
New Revision: 7953

Log:
Moved private data from the public interface to the internal class since an implementation of ReadWriteMutex using pthread's rwlock will not need the same set of variables.

Modified:
    trunk/licq/include/licq/thread/readwritemutex.h
    trunk/licq/src/thread/readwritemutex.cpp
    trunk/licq/src/thread/readwritemutex_debug.cpp

Modified: trunk/licq/include/licq/thread/readwritemutex.h
==============================================================================
--- trunk/licq/include/licq/thread/readwritemutex.h (original)
+++ trunk/licq/include/licq/thread/readwritemutex.h Sun Oct 31 17:18:48 2010
@@ -20,8 +20,6 @@
 #ifndef LICQ_READWRITEMUTEX_H
 #define LICQ_READWRITEMUTEX_H
 
-#include "condition.h"
-#include "mutex.h"
 #include "../macro.h"
 
 #include <boost/noncopyable.hpp>
@@ -77,12 +75,6 @@
 
 private:
   LICQ_DECLARE_PRIVATE();
-
-  int myNumReaders;
-  bool myHasWriter;
-
-  Mutex myMutex;
-  Condition myLockFree;
 };
 
 } // namespace Licq

Modified: trunk/licq/src/thread/readwritemutex.cpp
==============================================================================
--- trunk/licq/src/thread/readwritemutex.cpp (original)
+++ trunk/licq/src/thread/readwritemutex.cpp Sun Oct 31 17:18:48 2010
@@ -19,6 +19,8 @@
 
 #include "readwritemutex_debug.h"
 
+#include <licq/thread/condition.h>
+#include <licq/thread/mutex.h>
 #include <licq/thread/mutexlocker.h>
 #include <licq/thread/readwritemutex.h>
 
@@ -34,23 +36,17 @@
 class ReadWriteMutex::Private
 {
 public:
-  Private(ReadWriteMutex* parent) :
-    myParent(parent)
+  Private() :
+    myNumReaders(0),
+    myHasWriter(false)
   {
     // Empty
   }
 
   void setName(const std::string& /*name*/) { /* Empty */ }
 
-  void waitRead()
-  {
-    myParent->myLockFree.wait(myParent->myMutex);
-  }
-
-  void waitWrite()
-  {
-    myParent->myLockFree.wait(myParent->myMutex);    
-  }
+  void waitRead() { myLockFree.wait(myMutex); }
+  void waitWrite() { myLockFree.wait(myMutex); }
 
   void setReader() { /* Empty */ }
   void unsetReader() { /* Empty */ }
@@ -58,16 +54,17 @@
   void setWriter() { /* Empty */ }
   void unsetWriter() { /* Empty */ }
 
-private:
-  ReadWriteMutex* myParent;
+  int myNumReaders;
+  bool myHasWriter;
+
+  Mutex myMutex;
+  Condition myLockFree;
 };
 
 #endif
 
 ReadWriteMutex::ReadWriteMutex() :
-  myPrivate(new Private(this)),
-  myNumReaders(0),
-  myHasWriter(false)
+  myPrivate(new Private)
 {
   // Empty
 }
@@ -79,60 +76,61 @@
 
 void ReadWriteMutex::lockRead()
 {
-  MutexLocker locker(myMutex);
   LICQ_D();
+  MutexLocker locker(d->myMutex);
 
-  while (myHasWriter)
+  while (d->myHasWriter)
     d->waitRead();
 
   d->setReader();
-  ++myNumReaders;
+  d->myNumReaders += 1;
 }
 
 void ReadWriteMutex::unlockRead()
 {
-  MutexLocker locker(myMutex);
   LICQ_D();
+  MutexLocker locker(d->myMutex);
 
-  assert(myNumReaders > 0);
-  if (myNumReaders > 0)
+  assert(d->myNumReaders > 0);
+  if (d->myNumReaders > 0)
   {
     d->unsetReader();
-    if (--myNumReaders == 0)
-      myLockFree.signal();
+    d->myNumReaders -= 1;
+    if (d->myNumReaders == 0)
+      d->myLockFree.signal();
   }
 }
 
 void ReadWriteMutex::lockWrite()
 {
-  MutexLocker locker(myMutex);
   LICQ_D();
+  MutexLocker locker(d->myMutex);
 
-  while (myHasWriter || myNumReaders > 0)
+  while (d->myHasWriter || d->myNumReaders > 0)
     d->waitWrite();
 
   d->setWriter();
-  myHasWriter = true;
+  d->myHasWriter = true;
 }
 
 void ReadWriteMutex::unlockWrite()
 {
-  MutexLocker locker(myMutex);
   LICQ_D();
+  MutexLocker locker(d->myMutex);
 
-  assert(myHasWriter);
-  if (myHasWriter)
+  assert(d->myHasWriter);
+  if (d->myHasWriter)
   {
     d->unsetWriter();
-    myHasWriter = false;
-    myLockFree.broadcast();
+    d->myHasWriter = false;
+    d->myLockFree.broadcast();
   }
 }
 
 void ReadWriteMutex::setName(const std::string& name)
 {
-  MutexLocker locker(myMutex);
   LICQ_D();
+  MutexLocker locker(d->myMutex);
 
   d->setName(name);
 }

Modified: trunk/licq/src/thread/readwritemutex_debug.cpp
==============================================================================
--- trunk/licq/src/thread/readwritemutex_debug.cpp (original)
+++ trunk/licq/src/thread/readwritemutex_debug.cpp Sun Oct 31 17:18:48 2010
@@ -28,8 +28,9 @@
 class ReadWriteMutex::Private
 {
 public:
-  Private(ReadWriteMutex* parent) :
-    myParent(parent),
+  Private() :
+    myNumReaders(0),
+    myHasWriter(false),
     myName("no name")
   {
     // Empty
@@ -37,8 +38,8 @@
 
   ~Private()
   {
-    assert(myParent->myNumReaders == 0);
-    assert(myParent->myHasWriter == false);
+    assert(myNumReaders == 0);
+    assert(!myHasWriter);
   }
 
   void setName(const std::string& name) { myName = name; }
@@ -55,11 +56,16 @@
   void setReader();
   void unsetReader();
 
+  int myNumReaders;
+  bool myHasWriter;
+
+  Mutex myMutex;
+  Condition myLockFree;
+
 private:
   static const int RW_MUTEX_MAX_READERS = 20;
   static const unsigned int WAIT_TIMEOUT = 30 * 1000;
 
-  ReadWriteMutex* myParent;
   pthread_t myWriterThread;
   pthread_t myReaderThreads[RW_MUTEX_MAX_READERS];
   std::string myName;
@@ -71,15 +77,15 @@
             "on '%s'\n", (void*)::pthread_self(),
             (writing ? "write" : "read"), myName.c_str());
 
-  if (myParent->myHasWriter)
+  if (myHasWriter)
     ::fprintf(file, "Thread %p holds the write lock\n", (void*)myWriterThread);
   else
     ::fprintf(file, "No thread holds the write lock\n");
 
-  if (myParent->myNumReaders > 0)
+  if (myNumReaders > 0)
   {
     ::fprintf(file, "These threads hold the read lock:");
-    for (int i = 0; i < myParent->myNumReaders; ++i)
+    for (int i = 0; i < myNumReaders; ++i)
       ::fprintf(file, " %p", (void*)myReaderThreads[i]);
     ::fprintf(file, "\n");
   }
@@ -89,7 +95,7 @@
 
 void ReadWriteMutex::Private::debugWait(bool writing)
 {
-  if (!myParent->myLockFree.wait(myParent->myMutex, WAIT_TIMEOUT))
+  if (!myLockFree.wait(myMutex, WAIT_TIMEOUT))
   {
     // Print to stderr
     printUsers(stderr, writing);
@@ -123,7 +129,7 @@
   int i;
 
   // Find this thread
-  for (i = 0; i < myParent->myNumReaders; ++i)
+  for (i = 0; i < myNumReaders; ++i)
   {
     if (::pthread_equal(::pthread_self(), myReaderThreads[i]))
       break;
@@ -132,10 +138,10 @@
   // Make sure that this thread doesn't have a read lock already.
   // Comment the assert if a thread should be allowed to have multiple
   // read locks.
-  assert(i == myParent->myNumReaders);
+  assert(i == myNumReaders);
 
-  assert(myParent->myNumReaders < RW_MUTEX_MAX_READERS);
-  myReaderThreads[myParent->myNumReaders] = ::pthread_self();
+  assert(myNumReaders < RW_MUTEX_MAX_READERS);
+  myReaderThreads[myNumReaders] = ::pthread_self();
 }
 
 void ReadWriteMutex::Private::unsetReader()
@@ -143,16 +149,16 @@
   int i;
 
   // Find this thread
-  for (i = 0; i < myParent->myNumReaders; ++i)
+  for (i = 0; i < myNumReaders; ++i)
   {
     if (::pthread_equal(::pthread_self(), myReaderThreads[i]))
       break;
   }
 
   // Make sure that this thread has a read lock
-  assert(i < myParent->myNumReaders);
+  assert(i < myNumReaders);
 
   // Remove the reference to this thread
-  for (; i < myParent->myNumReaders - 1; ++i)
+  for (; i < myNumReaders - 1; ++i)
     myReaderThreads[i] = myReaderThreads[i + 1];
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.