|
From: <axl...@us...> - 2009-05-14 10:54:01
|
Revision: 241
http://hgengine.svn.sourceforge.net/hgengine/?rev=241&view=rev
Author: axlecrusher
Date: 2009-05-14 10:53:50 +0000 (Thu, 14 May 2009)
Log Message:
-----------
just use the semaphore to hold the thread id
Modified Paths:
--------------
Mercury2/src/MSemaphore.cpp
Mercury2/src/MSemaphore.h
Modified: Mercury2/src/MSemaphore.cpp
===================================================================
--- Mercury2/src/MSemaphore.cpp 2009-05-14 01:47:52 UTC (rev 240)
+++ Mercury2/src/MSemaphore.cpp 2009-05-14 10:53:50 UTC (rev 241)
@@ -1,9 +1,12 @@
#include <MSemaphore.h>
#include <MercuryThreads.h>
+#include <stdio.h>
+
MSemaphore::MSemaphore()
- :m_counter(0)
+ :m_lockCount(0), m_semaphore(0)
{
+ printf("cs %d\n", m_semaphore);
}
#ifndef WIN32
@@ -13,24 +16,19 @@
unsigned long MSemaphore::ReadAndClear()
{
- return __sync_fetch_and_and(&m_counter, 0);
+ return __sync_fetch_and_and(&m_semaphore, 0);
}
unsigned long MSemaphore::Decrement()
{
- return __sync_sub_and_fetch(&m_counter, 1);
+ return __sync_sub_and_fetch(&m_semaphore, 1);
}
unsigned long MSemaphore::Increment()
{
- return __sync_add_and_fetch(&m_counter, 1);
+ return __sync_add_and_fetch(&m_semaphore, 1);
}
-void MSemaphore::WaitAndSet(unsigned long value, unsigned long newVal)
-{
- while( (unsigned long)__sync_val_compare_and_swap(&m_counter, value, newVal) != value );
-}
-
#else
FORCEINLINE
@@ -75,51 +73,51 @@
unsigned long MSemaphore::ReadAndClear()
{
- return MyInterlockedAnd(&m_counter, 0);
+ return MyInterlockedAnd(&m_semaphore, 0);
}
unsigned long MSemaphore::Decrement()
{
- return InterlockedDecrement(&m_counter);
+ return InterlockedDecrement(&m_semaphore);
}
unsigned long MSemaphore::Increment()
{
- return InterlockedIncrement(&m_counter);
+ return InterlockedIncrement(&m_semaphore);
}
-void MSemaphore::WaitAndSet(unsigned long value, unsigned long newVal)
-{
- while ( InterlockedCompareExchange(Destination, newVal, value) != value );
-}
-
#endif
unsigned long MSemaphore::Read()
{
- return SYNC_OR_AND_FETCH(&m_counter, 0);
+ return SYNC_OR_AND_FETCH(&m_semaphore, 0);
}
+void MSemaphore::WaitAndSet(unsigned long value, unsigned long newVal)
+{
+ while( COMPARE_AND_SWAP(&m_semaphore, value, newVal) != value );
+}
+
void MSemaphore::Wait()
{
uint32_t thread = MercuryThread::Current();
- if ( COMPARE_AND_SWAP(&m_thread, 0, thread) == thread) //recursive lock
+ if ( SYNC_OR_AND_FETCH(&m_semaphore, 0) == thread) //recursive lock
{
++m_lockCount;
return;
}
- WaitAndSet(0,1);
+
+ WaitAndSet(0,thread); //new lock
++m_lockCount;
}
void MSemaphore::UnLock()
{
uint32_t thread = MercuryThread::Current();
- if ( SYNC_OR_AND_FETCH(&m_thread, 0) == thread) //unlock given from correct thread
+ if ( SYNC_OR_AND_FETCH(&m_semaphore, 0) == thread) //unlock given from correct thread
{
--m_lockCount;
- if (m_lockCount == 0) WaitAndSet(1,0);
- SYNC_AND_AND_FETCH(&m_thread, 0 );
+ if (m_lockCount == 0) SYNC_AND_AND_FETCH(&m_semaphore, 0 );
}
}
Modified: Mercury2/src/MSemaphore.h
===================================================================
--- Mercury2/src/MSemaphore.h 2009-05-14 01:47:52 UTC (rev 240)
+++ Mercury2/src/MSemaphore.h 2009-05-14 10:53:50 UTC (rev 241)
@@ -22,14 +22,12 @@
void UnLock();
private:
- //what exactly needs to be volatile
+ //what exactly needs to be volatile?
uint32_t m_lockCount;
#ifndef WIN32
- uint32_t m_counter;
- uint32_t m_thread;
+ uint32_t m_semaphore;
#else
volatile LONG m_counter; //align to 32bit boundary
- volatile LONG m_thread;
#endif
};
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|