[Avida-cvs] [avida-svn] r695 - development/source/tools

brysonda@myxo.css.msu.edu brysonda at myxo.css.msu.edu
Sat May 20 08:46:31 PDT 2006


Author: brysonda
Date: 2006-05-20 11:46:31 -0400 (Sat, 20 May 2006)
New Revision: 695

Modified:
   development/source/tools/tObjectFactory.h
Log:
Modify tObjectFactory to be thread safe.  It is perhaps overly so, locking on reads as well as writes.  This will, however, ensure that all concurrent accesses to the factory are consistent.  Given that calls to the object factory will be few in the grand scheme of things (generally during initialization), the cost should be minimal.

Modified: development/source/tools/tObjectFactory.h
===================================================================
--- development/source/tools/tObjectFactory.h	2006-05-20 15:18:07 UTC (rev 694)
+++ development/source/tools/tObjectFactory.h	2006-05-20 15:46:31 UTC (rev 695)
@@ -20,8 +20,11 @@
 #include "tList.h"
 #endif
 
+#include <pthread.h>
+
 class cString;
 
+
 template<typename CtorSignature> class tObjectFactory;
 
 namespace nObjectFactory
@@ -59,10 +62,11 @@
   
   tDictionary<CreateObjectFunction> m_create_funcs;
   int m_factory_id;
+  mutable pthread_mutex_t m_mutex;
   
 public:
-  tObjectFactory() : m_factory_id(0) { ; }
-  virtual ~tObjectFactory() { ; }
+  tObjectFactory() : m_factory_id(0) { pthread_mutex_init(&m_mutex, NULL); }
+  virtual ~tObjectFactory() { pthread_mutex_destroy(&m_mutex); }
   
   void SetFactoryId(int id) { m_factory_id = id; }
   int GetFactoryId() { return m_factory_id; }
@@ -70,23 +74,34 @@
   template<typename ClassType> bool Register(const cString& key)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func)) return false;
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
+      return false;
+    }
     
     m_create_funcs.Add(key, &nObjectFactory::createObject<BaseType, ClassType>);
+    pthread_mutex_unlock(&m_mutex);
     return true;
   }
   
   bool Unregister(const cString& key)
   {
-    return (m_create_funcs.Remove(key) != NULL);
+    pthread_mutex_lock(&m_mutex);
+    CreateObjectFunction func = m_create_funcs.Remove(key);
+    pthread_mutex_unlock(&m_mutex);
+    return (func != NULL);
   }
   
   virtual BaseType Create(const cString& key)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func))
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
       return func();
-    
+    }
+    pthread_mutex_unlock(&m_mutex);
     return NULL;
   }
   
@@ -95,15 +110,28 @@
     tList<cString> names;
     tList<CreateObjectFunction> funcs;
     
-    m_create_funcs.AsLists(names, funcs);    
+    pthread_mutex_lock(&m_mutex);
+    
+    m_create_funcs.AsLists(names, funcs);
     objects.Resize(names.GetSize());
     
     tListIterator<cString> names_it(names);
-    for (int i = 0; names_it.Next() != NULL; i++)
-      objects[i] = Create(*names_it.Get());
+    for (int i = 0; names_it.Next() != NULL; i++) {
+      CreateObjectFunction func;
+      m_create_funcs.Find(*names_it.Get(), func);
+      objects[i] = func();
+    }
+
+    pthread_mutex_unlock(&m_mutex);
   }
   
-  bool Supports(const cString& key) const { return m_create_funcs.HasEntry(key); }
+  bool Supports(const cString& key) const
+  {
+    pthread_mutex_lock(&m_mutex);
+    bool supports = m_create_funcs.HasEntry(key);
+    pthread_mutex_unlock(&m_mutex);
+    return supports;
+  }
 };
 
 template<typename BaseType, typename Arg1Type>
@@ -114,10 +142,11 @@
   
   tDictionary<CreateObjectFunction> m_create_funcs;
   int m_factory_id;
+  mutable pthread_mutex_t m_mutex;
   
 public:
-  tObjectFactory() : m_factory_id(0) { ; }
-  virtual ~tObjectFactory() { ; }
+  tObjectFactory() : m_factory_id(0) { pthread_mutex_init(&m_mutex, NULL); }
+  virtual ~tObjectFactory() { pthread_mutex_destroy(&m_mutex); }
   
   void SetFactoryId(int id) { m_factory_id = id; }
   int GetFactoryId() { return m_factory_id; }
@@ -125,27 +154,44 @@
   template<typename ClassType> bool Register(const cString& key)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func)) return false;
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
+      return false;
+    }
     
     m_create_funcs.Add(key, &nObjectFactory::createObject<BaseType, ClassType, Arg1Type>);
+    pthread_mutex_unlock(&m_mutex);
     return true;
   }
   
   bool Unregister(const cString& key)
   {
-    return (m_create_funcs.Remove(key) != NULL);
+    pthread_mutex_lock(&m_mutex);
+    CreateObjectFunction func = m_create_funcs.Remove(key);
+    pthread_mutex_unlock(&m_mutex);
+    return (func != NULL);
   }
   
   virtual BaseType Create(const cString& key, Arg1Type arg1)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func))
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
       return func(arg1);
-    
+    }
+    pthread_mutex_unlock(&m_mutex);
     return NULL;
   }
 
-  bool Supports(const cString& key) const { return m_create_funcs.HasEntry(key); }
+  bool Supports(const cString& key) const
+  {
+    pthread_mutex_lock(&m_mutex);
+    bool supports = m_create_funcs.HasEntry(key);
+    pthread_mutex_unlock(&m_mutex);
+    return supports;
+  }
 };
 
 template<typename BaseType, typename Arg1Type, typename Arg2Type>
@@ -156,10 +202,11 @@
   
   tDictionary<CreateObjectFunction> m_create_funcs;
   int m_factory_id;
+  mutable pthread_mutex_t m_mutex;
   
 public:
-    tObjectFactory() : m_factory_id(0) { ; }
-  virtual ~tObjectFactory() { ; }
+    tObjectFactory() : m_factory_id(0) { pthread_mutex_init(&m_mutex, NULL); }
+  virtual ~tObjectFactory() { pthread_mutex_destroy(&m_mutex); }
   
   void SetFactoryId(int id) { m_factory_id = id; }
   int GetFactoryId() { return m_factory_id; }
@@ -167,27 +214,44 @@
   template<typename ClassType> bool Register(const cString& key)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func)) return false;
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
+      return false;
+    }
     
     m_create_funcs.Add(key, &nObjectFactory::createObject<BaseType, ClassType, Arg1Type, Arg2Type>);
+    pthread_mutex_unlock(&m_mutex);
     return true;
   }
   
   bool Unregister(const cString& key)
   {
-    return (m_create_funcs.Remove(key) != NULL);
+    pthread_mutex_lock(&m_mutex);
+    CreateObjectFunction func = m_create_funcs.Remove(key);
+    pthread_mutex_unlock(&m_mutex);
+    return (func != NULL);
   }
   
   virtual BaseType Create(const cString& key, Arg1Type arg1, Arg2Type arg2)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func))
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
       return func(arg1, arg2);
-    
+    }
+    pthread_mutex_unlock(&m_mutex);
     return NULL;
   }
 
-  bool Supports(const cString& key) const { return m_create_funcs.HasEntry(key); }
+  bool Supports(const cString& key) const
+  {
+    pthread_mutex_lock(&m_mutex);
+    bool supports = m_create_funcs.HasEntry(key);
+    pthread_mutex_unlock(&m_mutex);
+    return supports;
+  }
 };
 
 template<typename BaseType, typename Arg1Type, typename Arg2Type, typename Arg3Type>
@@ -198,10 +262,11 @@
   
   tDictionary<CreateObjectFunction> m_create_funcs;
   int m_factory_id;
+  mutable pthread_mutex_t m_mutex;
   
 public:
-    tObjectFactory() : m_factory_id(0) { ; }
-  virtual ~tObjectFactory() { ; }
+  tObjectFactory() : m_factory_id(0) { pthread_mutex_init(&m_mutex, NULL); }
+  virtual ~tObjectFactory() { pthread_mutex_destroy(&m_mutex); }
   
   void SetFactoryId(int id) { m_factory_id = id; }
   int GetFactoryId() { return m_factory_id; }
@@ -209,27 +274,44 @@
   template<typename ClassType> bool Register(const cString& key)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func)) return false;
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
+      return false;
+    }
     
     m_create_funcs.Add(key, &nObjectFactory::createObject<BaseType, ClassType, Arg1Type, Arg2Type, Arg3Type>);
+    pthread_mutex_unlock(&m_mutex);
     return true;
   }
   
   bool Unregister(const cString& key)
   {
-    return (m_create_funcs.Remove(key) != NULL);
+    pthread_mutex_lock(&m_mutex);
+    CreateObjectFunction func = m_create_funcs.Remove(key);
+    pthread_mutex_unlock(&m_mutex);
+    return (func != NULL);
   }
   
   virtual BaseType Create(const cString& key, Arg1Type arg1, Arg2Type arg2, Arg3Type arg3)
   {
     CreateObjectFunction func;
-    if (m_create_funcs.Find(key, func))
+    pthread_mutex_lock(&m_mutex);
+    if (m_create_funcs.Find(key, func)) {
+      pthread_mutex_unlock(&m_mutex);
       return func(arg1, arg2, arg3);
-    
+    }
+    pthread_mutex_unlock(&m_mutex);
     return NULL;
   }
 
-  bool Supports(const cString& key) const { return m_create_funcs.HasEntry(key); }
+  bool Supports(const cString& key) const
+  {
+    pthread_mutex_lock(&m_mutex);
+    bool supports = m_create_funcs.HasEntry(key);
+    pthread_mutex_unlock(&m_mutex);
+    return supports;
+  }
 };
 
 #endif




More information about the Avida-cvs mailing list