Add recursive mutex support to BotMutex
authorclinton <clinton@unknownlamer.org>
Thu, 13 Nov 2008 22:16:02 +0000 (22:16 +0000)
committerclinton <clinton@unknownlamer.org>
Thu, 13 Nov 2008 22:16:02 +0000 (22:16 +0000)
source/BotThreading.C
source/BotThreading.H

index d4e6ea0..75812b3 100644 (file)
 #include <pthread.h>
 #include <iostream>
 
-BotMutex::BotMutex ()
+BotMutex::BotMutex (bool recursive)
 {
   pthread_t self = pthread_self ();
   std::cerr << "Mutex Init..."
            << " Mutex: " << &mutex
            << " Thread: " << &self
            << std::endl;
-  pthread_mutex_init (&mutex, 0);
+  pthread_mutexattr_init (&attrs);
+
+  if (recursive)
+    pthread_mutexattr_settype (&attrs, PTHREAD_MUTEX_RECURSIVE);
+
+  pthread_mutex_init (&mutex, &attrs);
+
 }
 
 BotMutex::~BotMutex ()
@@ -41,6 +47,7 @@ BotMutex::~BotMutex ()
            << " Thread: " << &self
            << std::endl;
   pthread_mutex_destroy (&mutex);
+  pthread_mutexattr_destroy (&attrs);
 }
 
 void BotMutex::lock ()
index f961816..47efb07 100644 (file)
@@ -38,10 +38,11 @@ class BotMutex
 {
 #ifdef MULTITHREAD
   pthread_mutex_t mutex;
+  pthread_mutexattr_t attrs;
 #endif
 
 public:
-  BotMutex ();
+  BotMutex (bool recursive = false);
   ~BotMutex ();
 
   void lock ();