Index: /trunk/psLib/src/sys/psMemory.c
===================================================================
--- /trunk/psLib/src/sys/psMemory.c	(revision 6418)
+++ /trunk/psLib/src/sys/psMemory.c	(revision 6419)
@@ -8,6 +8,6 @@
 *  @author Robert Lupton, Princeton University
 *
-*  @version $Revision: 1.67 $ $Name: not supported by cvs2svn $
-*  @date $Date: 2006-02-03 00:11:59 $
+*  @version $Revision: 1.68 $ $Name: not supported by cvs2svn $
+*  @date $Date: 2006-02-10 02:44:46 $
 *
 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -58,4 +58,7 @@
 static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
 
+//private boolean for enabling/disabling thread safety.  Default = enabled.
+static bool safeThreads = true;
+
 static psS32 recycleBins = 13;
 static psS32 recycleBinSize[14] = {
@@ -83,6 +86,7 @@
 {
     psPtr ptr = NULL;
-
-    pthread_mutex_lock(&recycleMemBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&recycleMemBlockListMutex);
+    }
     psS32 level = recycleBins - 1;
 
@@ -97,5 +101,8 @@
         level--;
     }
-    pthread_mutex_unlock(&recycleMemBlockListMutex);
+
+    if (safeThreads) {
+        pthread_mutex_unlock(&recycleMemBlockListMutex);
+    }
 
     return ptr;
@@ -271,7 +278,13 @@
     psMemId id;
 
-    pthread_mutex_lock(&memIdMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&memIdMutex);
+    }
+
     id = memid + 1;
-    pthread_mutex_unlock(&memIdMutex);
+
+    if (safeThreads) {
+        pthread_mutex_unlock(&memIdMutex);
+    }
 
     return id;
@@ -287,7 +300,11 @@
 
     for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) {
-        pthread_mutex_unlock(&memBlockListMutex);
+        if (safeThreads) {
+            pthread_mutex_unlock(&memBlockListMutex);
+        }
         failure = checkMemBlock(iter, __func__);
-        pthread_mutex_lock(&memBlockListMutex);
+        if (safeThreads) {
+            pthread_mutex_lock(&memBlockListMutex);
+        }
         if ( failure ) {
             nbad++;
@@ -297,5 +314,7 @@
             if (abort_on_error) {
                 // release the lock on the memblock list
-                pthread_mutex_unlock(&memBlockListMutex);
+                if (safeThreads) {
+                    pthread_mutex_unlock(&memBlockListMutex);
+                }
                 psAbort(__func__, "Detected memory corruption");
                 return nbad;
@@ -305,5 +324,7 @@
 
     // release the lock on the memblock list
-    pthread_mutex_unlock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_unlock(&memBlockListMutex);
+    }
     return nbad;
 }
@@ -331,5 +352,7 @@
             size = recycleBinSize[level];  // round-up size to next sized bin.
 
-            pthread_mutex_lock(&recycleMemBlockListMutex);
+            if (safeThreads) {
+                pthread_mutex_lock(&recycleMemBlockListMutex);
+            }
 
             if (recycleMemBlockList[level] != NULL) {
@@ -342,5 +365,7 @@
             }
 
-            pthread_mutex_unlock(&recycleMemBlockListMutex);
+            if (safeThreads) {
+                pthread_mutex_unlock(&recycleMemBlockListMutex);
+            }
         }
     }
@@ -359,11 +384,16 @@
         *(psPtr*)&ptr->endblock = P_PS_MEMMAGIC;
         ptr->userMemorySize = size;
-        pthread_mutex_init(&ptr->refCounterMutex, NULL);
+        if (safeThreads) {
+            pthread_mutex_init(&ptr->refCounterMutex, NULL);
+        }
     }
     // increment the memory id safely.
-    pthread_mutex_lock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&memBlockListMutex);
+    }
     *(psMemId* ) & ptr->id = ++memid;
-    pthread_mutex_unlock(&memBlockListMutex);
-
+    if (safeThreads) {
+        pthread_mutex_unlock(&memBlockListMutex);
+    }
     ptr->file = file;
     ptr->freeFunc = NULL;
@@ -376,5 +406,7 @@
 
     // need exclusive access of the memory block list now...
-    pthread_mutex_lock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&memBlockListMutex);
+    }
 
     // insert the new block to the front of the memBlock linked-list
@@ -385,5 +417,7 @@
     lastMemBlockAllocated = ptr;
 
-    pthread_mutex_unlock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_unlock(&memBlockListMutex);
+    }
 
     // Did the user ask to be informed about this allocation?
@@ -414,5 +448,7 @@
         }
 
-        pthread_mutex_lock(&memBlockListMutex);
+        if (safeThreads) {
+            pthread_mutex_lock(&memBlockListMutex);
+        }
 
         isBlockLast = (ptr == lastMemBlockAllocated);
@@ -441,5 +477,7 @@
         }
 
-        pthread_mutex_unlock(&memBlockListMutex);
+        if (safeThreads) {
+            pthread_mutex_unlock(&memBlockListMutex);
+        }
 
         // Did the user ask to be informed about this allocation?
@@ -487,5 +525,7 @@
     psMemBlock* topBlock = lastMemBlockAllocated;
 
-    pthread_mutex_lock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&memBlockListMutex);
+    }
 
     for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
@@ -506,5 +546,7 @@
     }
 
-    pthread_mutex_unlock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_unlock(&memBlockListMutex);
+    }
 
     if (nleak == 0 || array == NULL) {
@@ -513,5 +555,7 @@
 
     *array = p_psAlloc(nleak * sizeof(psMemBlock), __FILE__, __LINE__);
-    pthread_mutex_lock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&memBlockListMutex);
+    }
 
     for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) {
@@ -527,5 +571,7 @@
     }
 
-    pthread_mutex_unlock(&memBlockListMutex);
+    if (safeThreads) {
+        pthread_mutex_unlock(&memBlockListMutex);
+    }
 
     return nleak;
@@ -552,7 +598,11 @@
     }
 
-    pthread_mutex_lock(&ptr2->refCounterMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&ptr2->refCounterMutex);
+    }
     refCount = ptr2->refCounter;
-    pthread_mutex_unlock(&ptr2->refCounterMutex);
+    if (safeThreads) {
+        pthread_mutex_unlock(&ptr2->refCounterMutex);
+    }
 
     return refCount;
@@ -576,7 +626,11 @@
     }
 
-    pthread_mutex_lock(&ptr->refCounterMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&ptr->refCounterMutex);
+    }
     ptr->refCounter++;
-    pthread_mutex_unlock(&ptr->refCounterMutex);
+    if (safeThreads) {
+        pthread_mutex_unlock(&ptr->refCounterMutex);
+    }
 
     // Did the user ask to be informed about this allocation?
@@ -588,9 +642,8 @@
 }
 
-psPtr p_psMemSetRefCounter(
-    psPtr vptr,
-    psReferenceCount count,
-    const char *file,
-    psS32 lineno)
+psPtr p_psMemSetRefCounter(psPtr vptr,
+                           psReferenceCount count,
+                           const char *file,
+                           psS32 lineno)
 {
     psMemBlock* ptr;
@@ -610,7 +663,11 @@
     }
 
-    pthread_mutex_lock(&ptr->refCounterMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&ptr->refCounterMutex);
+    }
     ptr->refCounter = count;
-    pthread_mutex_unlock(&ptr->refCounterMutex);
+    if (safeThreads) {
+        pthread_mutex_unlock(&ptr->refCounterMutex);
+    }
 
     if (count < 1) {
@@ -642,12 +699,18 @@
     }
 
-    pthread_mutex_lock(&ptr->refCounterMutex);
+    if (safeThreads) {
+        pthread_mutex_lock(&ptr->refCounterMutex);
+    }
 
     if (ptr->refCounter > 1) {
         ptr->refCounter--;                 // multiple references, just decrement the count.
-        pthread_mutex_unlock(&ptr->refCounterMutex);
+        if (safeThreads) {
+            pthread_mutex_unlock(&ptr->refCounterMutex);
+        }
 
     } else {
-        pthread_mutex_unlock(&ptr->refCounterMutex);
+        if (safeThreads) {
+            pthread_mutex_unlock(&ptr->refCounterMutex);
+        }
 
         if (ptr->freeFunc != NULL) {
@@ -655,5 +718,7 @@
         }
 
-        pthread_mutex_lock(&memBlockListMutex);
+        if (safeThreads) {
+            pthread_mutex_lock(&memBlockListMutex);
+        }
 
         // cut the memBlock out of the memBlock list
@@ -668,5 +733,7 @@
         }
 
-        pthread_mutex_unlock(&memBlockListMutex);
+        if (safeThreads) {
+            pthread_mutex_unlock(&memBlockListMutex);
+        }
 
         // do we need to recycle?
@@ -683,5 +750,7 @@
             ptr->previousBlock = NULL;
 
-            pthread_mutex_lock(&recycleMemBlockListMutex);
+            if (safeThreads) {
+                pthread_mutex_lock(&recycleMemBlockListMutex);
+            }
             ptr->nextBlock = recycleMemBlockList[level];
             if (recycleMemBlockList[level] != NULL) {
@@ -689,5 +758,7 @@
             }
             recycleMemBlockList[level] = ptr;
-            pthread_mutex_unlock(&recycleMemBlockListMutex);
+            if (safeThreads) {
+                pthread_mutex_unlock(&recycleMemBlockListMutex);
+            }
 
         } else {
@@ -703,5 +774,7 @@
             #else // #ifdef PS_MEM_DEBUG
 
-            pthread_mutex_destroy(&ptr->refCounterMutex);
+            if (safeThreads) {
+                pthread_mutex_destroy(&ptr->refCounterMutex);
+            }
             free(ptr);
             #endif // #else - #ifdef PS_MEM_DEBUG
@@ -731,4 +804,5 @@
 
 }
+
 psFreeFunc psMemGetDeallocator(const psPtr ptr)
 {
@@ -998,4 +1072,13 @@
 }
 
+bool psMemThreadSafety(bool safe)
+{
+    bool out = safeThreads;
+    safeThreads = safe;
+    if ( out != p_psListThreadSafety(safe) ) {
+        psWarning("Thread safety setting in psMemory didn't match that of psList.\n");
+    }
+    return out;
+}
 
 bool p_psMemGetPersistent(psPtr vptr)
Index: /trunk/psLib/src/sys/psMemory.h
===================================================================
--- /trunk/psLib/src/sys/psMemory.h	(revision 6418)
+++ /trunk/psLib/src/sys/psMemory.h	(revision 6419)
@@ -12,6 +12,6 @@
  *  @ingroup MemoryManagement
  *
- *  @version $Revision: 1.51 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-10-01 02:22:13 $
+ *  @version $Revision: 1.52 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-02-10 02:44:46 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -169,5 +169,5 @@
  */
 psFreeFunc psMemGetDeallocator(
-    const psPtr ptr                          ///< the memory block
+    const psPtr ptr                    ///< the memory block
 );
 
@@ -181,4 +181,16 @@
 );
 
+/** Activate or Deactivate thread safety and mutex locking in the memory management.
+ *
+ *  psMemThreadSafety shall turn on thread safety in the memory management functions if
+ *  safe is true, and deactivate all mutex locking in the memory management functions if
+ *  safe is false.  The function shall return the previous value of the thread safety.
+ *  Note that the default behaviour of the library shall be for the locking to be performed.
+ *
+ *  @return bool:       The previous value of the thread safety.
+ */
+bool psMemThreadSafety(
+    bool safe                          ///< boolean for turning on/off thread safety
+);
 
 /** Set the memory as persistent so that it is ignored when detecting memory leaks.
Index: /trunk/psLib/src/types/psList.c
===================================================================
--- /trunk/psLib/src/types/psList.c	(revision 6418)
+++ /trunk/psLib/src/types/psList.c	(revision 6419)
@@ -6,6 +6,6 @@
  *  @author Robert Daniel DeSonia, MHPCC
  *
- *  @version $Revision: 1.45 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2005-11-14 22:18:34 $
+ *  @version $Revision: 1.46 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-02-10 02:44:46 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -33,4 +33,7 @@
 static psBool listIteratorRemove(psListIterator* iterator);
 
+//private boolean for enabling/disabling thread safety.  Default = enabled.
+static bool safeThreads = true;
+
 static void listFree(psList* list)
 {
@@ -39,5 +42,8 @@
     }
 
-    pthread_mutex_lock(&list->p_lock);
+    if (safeThreads) {
+        pthread_mutex_lock(&list->p_lock);
+        //        pthread_mutex_lock(&p_lock);
+    }
 
     // remove the associated iterators -- any references are invalid once psList is freed.
@@ -64,8 +70,12 @@
     }
 
-    pthread_mutex_unlock(&list->p_lock);
-
-    pthread_mutex_destroy(&list->p_lock);
-
+    if (safeThreads) {
+        pthread_mutex_unlock(&list->p_lock);
+        pthread_mutex_destroy(&list->p_lock);
+        //        pthread_mutex_unlock(&p_lock);
+        //        pthread_mutex_destroy(&p_lock);
+    } else {
+        safeThreads = true;
+    }
 }
 
@@ -100,6 +110,8 @@
     int index = iterator->index;
 
-    pthread_mutex_lock(&list->p_lock);
-
+    if (safeThreads) {
+        pthread_mutex_lock(&list->p_lock);
+        //        pthread_mutex_lock(&p_lock);
+    }
     if (elem == list->head) {        // head of list?
         list->head = elem->next;
@@ -126,6 +138,8 @@
     list->n--;
 
-    pthread_mutex_unlock(&list->p_lock)
-    ;
+    if (safeThreads) {
+        pthread_mutex_unlock(&list->p_lock);
+        //        pthread_mutex_unlock(&p_lock);
+    }
 
     // OK, delete orphaned list element and its data
@@ -150,5 +164,8 @@
     psListIteratorAlloc(list,PS_LIST_HEAD,true);
 
-    pthread_mutex_init(&(list->p_lock), NULL);
+    if (safeThreads) {
+        pthread_mutex_init(&(list->p_lock), NULL);
+        //        pthread_mutex_init(&p_lock, NULL);
+    }
 
     if (data != NULL) {
@@ -164,5 +181,10 @@
 }
 
-
+bool p_psListThreadSafety(bool safe)
+{
+    bool out = safeThreads;
+    safeThreads = safe;
+    return out;
+}
 
 psListIterator* psListIteratorAlloc(psList* list, long location, bool mutable)
@@ -326,5 +348,8 @@
     psListElem* elem = psAlloc(sizeof(psListElem));
 
-    pthread_mutex_lock(&list->p_lock);
+    if (safeThreads) {
+        pthread_mutex_lock(&list->p_lock);
+        //        pthread_mutex_lock(&p_lock);
+    }
 
     // set the new list element's attributes
@@ -362,5 +387,8 @@
     }
 
-    pthread_mutex_unlock(&list->p_lock);
+    if (safeThreads) {
+        pthread_mutex_unlock(&list->p_lock);
+        //        pthread_mutex_unlock(&p_lock);
+    }
 
     return true;
@@ -399,5 +427,8 @@
     psListElem* elem = psAlloc(sizeof(psListElem));
 
-    pthread_mutex_lock(&list->p_lock);
+    if (safeThreads) {
+        pthread_mutex_lock(&list->p_lock);
+        //        pthread_mutex_lock(&p_lock);
+    }
 
     // set the new list element's attributes
@@ -435,5 +466,8 @@
     }
 
-    pthread_mutex_unlock(&list->p_lock);
+    if (safeThreads) {
+        pthread_mutex_unlock(&list->p_lock);
+        //        pthread_mutex_unlock(&p_lock);
+    }
 
     return true;
Index: /trunk/psLib/src/types/psList.h
===================================================================
--- /trunk/psLib/src/types/psList.h	(revision 6418)
+++ /trunk/psLib/src/types/psList.h	(revision 6419)
@@ -7,6 +7,6 @@
  *  @ingroup LinkedList
  *
- *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-02-07 23:36:15 $
+ *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-02-10 02:44:46 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -94,4 +94,11 @@
 ;
 
+/** Private function used by psMemThreadSafety to activate/deactivate thread safety.
+ *
+ *  @return bool:       The previous value of the thread safety.
+ */
+bool p_psListThreadSafety(
+    bool safe                          ///< current value of thread safety to set
+);
 
 /** Creates a psList linked list object.
