Index: trunk/psLib/src/sys/psMemory.c
===================================================================
--- trunk/psLib/src/sys/psMemory.c	(revision 6314)
+++ 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)
