Index: /branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.c
===================================================================
--- /branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.c	(revision 31575)
+++ /branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.c	(revision 31576)
@@ -40,13 +40,25 @@
 #define P_PS_MEMMAGIC (uint32_t)0xdeadbeef
 
+# define USE_SPINLOCK 1
+
+# if (USE_SPINLOCK) 
+#define MUTEX_LOCK(mutexPtr) \
+if (safeThreads) { \
+    pthread_spin_lock(mutexPtr); \
+}
+#define MUTEX_UNLOCK(mutexPtr) \
+if (safeThreads) { \
+    pthread_spin_unlock(mutexPtr); \
+}
+# else // !USE_SPINLOCK
 #define MUTEX_LOCK(mutexPtr) \
 if (safeThreads) { \
     pthread_mutex_lock(mutexPtr); \
 }
-
 #define MUTEX_UNLOCK(mutexPtr) \
 if (safeThreads) { \
     pthread_mutex_unlock(mutexPtr); \
 }
+# endif // USE_SPINLOCK
 
 // psAbort() calls functions that call psAlloc() so it is *UNSAFE* to use it
@@ -94,5 +106,21 @@
 //
 //
-static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
+
+# if (USE_SPINLOCK)
+static pthread_spinlock_t memBlockListMutex;
+# else
+// static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
+static pthread_mutex_t memBlockListMutex;
+# endif
+
+void psMemInit() {
+
+# if (USE_SPINLOCK) 
+    pthread_spin_init(&memBlockListMutex, 0);
+# else
+    pthread_mutex_init(&memBlockListMutex, NULL);
+# endif
+
+}
 
 /******** thread safety options management ********/
@@ -335,21 +363,10 @@
 
 # if (!HARDLOCKS)
-static int setLock1 = 0;
-static int clearLock1 = 0;
-
-static int setLock2 = 0;
-static int clearLock2 = 0;
-
-static int setLockNext = 0;
-static int clearLockNext = 0;
-
-static int setLockPrev = 0;
-static int clearLockPrev = 0;
+static int setLock = 0;
+static int clearLock = 0;
+static int retryLock = 0;
 # endif
 
 # define BLOCK_SLEEP 100
-
-static int callL1 = 0;
-static int callL2 = 0;
 
 # define MEM_ASSERT(COND,MSG) { if (!(COND)) { fprintf (stderr, MSG); abort(); } }
@@ -373,7 +390,4 @@
 	
     while (true) {
-	// we cannot set the lock on the marker while the lock is held
-	while (lastMemBlockAllocated->inFlight) { usleep (BLOCK_SLEEP); }
-
 	// set a lock, then lock this memblock and its neighbors
 	MUTEX_LOCK(&memBlockListMutex);
@@ -383,4 +397,6 @@
 	    // nope, we need to try again
 	    MUTEX_UNLOCK(&memBlockListMutex);
+	    retryLock ++;
+	    usleep (BLOCK_SLEEP);
 	    continue;
 	}
@@ -388,5 +404,5 @@
 	// the marker is ours!
 	lastMemBlockAllocated->inFlight = true;
-	setLock1 ++;
+	setLock ++;
 	return;
     }
@@ -410,7 +426,4 @@
 	
     while (true) {
-	// we cannot set the lock on the marker while the lock is held
-	while (memBlock->inFlight) { usleep (BLOCK_SLEEP); }
-
 	// set a lock, then lock this memblock and its neighbors
 	MUTEX_LOCK(&memBlockListMutex);
@@ -420,4 +433,6 @@
 	    // nope, we need to try again
 	    MUTEX_UNLOCK(&memBlockListMutex);
+	    usleep (BLOCK_SLEEP);
+	    retryLock ++;
 	    continue;
 	}
@@ -425,5 +440,5 @@
 	// the marker is ours!
 	memBlock->inFlight = true;
-	setLock1 ++;
+	setLock ++;
 	if (keepMutex) return;
 
@@ -455,5 +470,5 @@
     }
     memBlock->inFlight = false;
-    clearLock1 ++;
+    clearLock ++;
     MUTEX_UNLOCK(&memBlockListMutex);
     return;
@@ -475,7 +490,7 @@
 	// we cannot set the lock on the marker while the lock is held
 	// wait until all three markers are clear:
-	while (memBlock->inFlight) { usleep (BLOCK_SLEEP); }
-	while (memBlock->nextBlock && memBlock->nextBlock->inFlight) { usleep (BLOCK_SLEEP); }
-	while (memBlock->previousBlock && memBlock->previousBlock->inFlight) { usleep (BLOCK_SLEEP); }
+	// while (memBlock->inFlight) { usleep (BLOCK_SLEEP); }
+	// while (memBlock->nextBlock && memBlock->nextBlock->inFlight) { usleep (BLOCK_SLEEP); }
+	// while (memBlock->previousBlock && memBlock->previousBlock->inFlight) { usleep (BLOCK_SLEEP); }
 
 	// set a lock, then lock this memblock and its neighbors
@@ -486,4 +501,6 @@
 	    // nope, we need to try again
 	    MUTEX_UNLOCK(&memBlockListMutex);
+	    usleep (BLOCK_SLEEP);
+	    retryLock ++;
 	    continue;
 	}
@@ -491,4 +508,6 @@
 	    // nope, we need to try again
 	    MUTEX_UNLOCK(&memBlockListMutex);
+	    usleep (BLOCK_SLEEP);
+	    retryLock ++;
 	    continue;
 	}
@@ -496,4 +515,6 @@
 	    // nope, we need to try again
 	    MUTEX_UNLOCK(&memBlockListMutex);
+	    usleep (BLOCK_SLEEP);
+	    retryLock ++;
 	    continue;
 	}
@@ -501,12 +522,10 @@
 	// the markers are ours!
 	memBlock->inFlight = true;
-	setLock2 ++;
+	setLock ++;
 	if (memBlock->nextBlock) {
 	    memBlock->nextBlock->inFlight = true;
-	    setLockNext ++;
 	}
 	if (memBlock->previousBlock) {
 	    memBlock->previousBlock->inFlight = true;
-	    setLockPrev ++;
 	}
 	if (keepMutex) return;
@@ -530,14 +549,12 @@
     MEM_ASSERT (memBlock->inFlight, "trying to clear an unlocked memBlock\n");
     memBlock->inFlight = false;
-    clearLock2 ++;
+    clearLock ++;
     if (memBlock->nextBlock) {
 	MEM_ASSERT (memBlock->nextBlock->inFlight, "trying to clear an unlocked memBlock\n");
 	memBlock->nextBlock->inFlight = false;
-	clearLockNext ++;
     }
     if (memBlock->previousBlock) {
 	MEM_ASSERT (memBlock->previousBlock->inFlight, "trying to clear an unlocked memBlock\n");
 	memBlock->previousBlock->inFlight = false;
-	clearLockPrev ++;
     }
     MUTEX_UNLOCK(&memBlockListMutex);
@@ -621,5 +638,4 @@
     // lastMemBlockAllocated is always true except the first allocation
     BLOCKLAST_LOCK ();
-    callL1++;
 
     psMemBlock *last1 = lastMemBlockAllocated;
@@ -649,8 +665,4 @@
 
     BLOCK_UNLOCK (memBlock->nextBlock, true);
-    callL1--;
-
-    // if (callL1 > +7) abort();
-    // if (callL1 < -7) abort();
 
     // And return the user the memory that they allocated
@@ -800,6 +812,6 @@
     // set a lock, then lock this memblock and its neighbors.  at the end of this call,
     // the global mutex is released, but the memBlock and its neighbors are protected
-    // BLOCKSET_LOCK (memBlock, false);
-    MUTEX_LOCK(&memBlockListMutex);
+    BLOCKSET_LOCK (memBlock, false);
+    // MUTEX_LOCK(&memBlockListMutex);
 
     psMemBlock *nextBlock = memBlock->nextBlock;
@@ -847,6 +859,6 @@
 
     // all of the list modifications are done; set the lock and clear the per-memBlock locks
-    // BLOCKSET_UNLOCK(memBlock, false);
-    MUTEX_UNLOCK(&memBlockListMutex);
+    BLOCKSET_UNLOCK(memBlock, false);
+    // MUTEX_UNLOCK(&memBlockListMutex);
 
     // XXX these are not actually guaranteed : another thread may already grab them before we get here
@@ -872,4 +884,7 @@
     psS32 j = 0;
     psMemBlock *topBlock = lastMemBlockAllocated;
+
+    // XXX move this elsewhere?
+    fprintf (stderr, "set %d locks, cleared %d locks, retry on %d locks (memID %ld)\n", setLock, clearLock, retryLock, memid);
 
     // make sure that the memblock list is free of corruption before we crawl
@@ -1080,5 +1095,4 @@
     // we need a MUTEX_LOCK here: otherwise, two functions can race on refCounter--;
     BLOCK_LOCK (memBlock, true);
-    callL2++;
     if (memBlock->refCounter > 1) {
         memBlock->refCounter--;
@@ -1089,11 +1103,7 @@
         }
 	BLOCK_UNLOCK(memBlock, true);
-	callL2--;
         return ptr;
     }
     BLOCK_UNLOCK(memBlock, true);
-    callL2--;
-    // if (callL2 > +7) abort();
-    // if (callL2 < -7) abort();
 
     // we can't invoke freeFunc() while we're holding memBlockListMutex as it
Index: /branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.h
===================================================================
--- /branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.h	(revision 31575)
+++ /branches/eam_branches/ipp-20110505/psLib/src/sys/psMemory.h	(revision 31576)
@@ -146,4 +146,5 @@
 #endif // ifdef DOXYGEN
 
+void psMemInit(void);
 
 /** Set the deallocator routine
Index: /branches/eam_branches/ipp-20110505/psphot/src/psphot.c
===================================================================
--- /branches/eam_branches/ipp-20110505/psphot/src/psphot.c	(revision 31575)
+++ /branches/eam_branches/ipp-20110505/psphot/src/psphot.c	(revision 31576)
@@ -4,4 +4,5 @@
 int main (int argc, char **argv) {
 
+    psMemInit();
     psTimerStart ("complete");
     pmErrorRegister();                  // register psModule's error codes/messages
