Commit 7087d753 authored by Davis King's avatar Davis King

Switched the windows implementation of dlib::mutex to use a CRITICAL_SECTION instead

of a "mutex" (i.e. the thing made by CreateMutex()) since a critical section does the
same thing but faster.
parent 05f35378
...@@ -38,34 +38,26 @@ namespace dlib ...@@ -38,34 +38,26 @@ namespace dlib
class mutex class mutex
{ {
// give signaler access to hMutex
friend class signaler;
public: public:
mutex ( mutex (
) : )
hMutex(CreateMutex(NULL,FALSE,NULL))
{
if (hMutex == NULL)
{ {
throw dlib::thread_error(ECREATE_MUTEX, InitializeCriticalSection(&cs);
"in function mutex::mutex() an error occurred making the mutex"
);
}
} }
~mutex ( ~mutex (
) { CloseHandle(hMutex); } ) { DeleteCriticalSection(&cs); }
void lock ( void lock (
) const { WaitForSingleObject (hMutex,INFINITE); } ) const { EnterCriticalSection(&cs); }
void unlock ( void unlock (
) const { ReleaseMutex(hMutex); } ) const { LeaveCriticalSection(&cs); }
private: private:
mutable HANDLE hMutex; mutable CRITICAL_SECTION cs;
// restricted functions // restricted functions
mutex(mutex&); // copy constructor mutex(mutex&); // copy constructor
...@@ -87,22 +79,16 @@ namespace dlib ...@@ -87,22 +79,16 @@ namespace dlib
) : ) :
hSemaphore(CreateSemaphore (NULL, 0, 100000000, NULL)), hSemaphore(CreateSemaphore (NULL, 0, 100000000, NULL)),
waiters(0), waiters(0),
hWaitersMutex(CreateMutex(NULL,FALSE,NULL)),
hCountSema(CreateSemaphore (NULL,0,100000000,NULL)), hCountSema(CreateSemaphore (NULL,0,100000000,NULL)),
m(associated_mutex) m(associated_mutex)
{ {
if (hSemaphore == NULL || hWaitersMutex == NULL || hCountSema == NULL) if (hSemaphore == NULL || hCountSema == NULL)
{ {
if (hSemaphore != NULL) if (hSemaphore != NULL)
{ {
CloseHandle(hSemaphore); CloseHandle(hSemaphore);
} }
if (hWaitersMutex != NULL)
{
CloseHandle(hWaitersMutex);
}
if (hCountSema != NULL) if (hCountSema != NULL)
{ {
CloseHandle(hCountSema); CloseHandle(hCountSema);
...@@ -115,20 +101,20 @@ namespace dlib ...@@ -115,20 +101,20 @@ namespace dlib
} }
~signaler ( ~signaler (
) { CloseHandle(hSemaphore); CloseHandle(hWaitersMutex); CloseHandle(hCountSema);} ) { CloseHandle(hSemaphore); CloseHandle(hCountSema);}
void wait ( void wait (
) const ) const
{ {
// get a lock on the mutex for the waiters variable // get a lock on the mutex for the waiters variable
WaitForSingleObject (hWaitersMutex,INFINITE); waiters_mutex.lock();
// mark that one more thread will be waiting on this signaler // mark that one more thread will be waiting on this signaler
++waiters; ++waiters;
// release the mutex for waiters // release the mutex for waiters
ReleaseMutex(hWaitersMutex); waiters_mutex.unlock();
// release the assocaited mutex // release the associated mutex
ReleaseMutex(m.hMutex); m.unlock();
// wait for the semaphore to be signaled // wait for the semaphore to be signaled
WaitForSingleObject (hSemaphore,INFINITE); WaitForSingleObject (hSemaphore,INFINITE);
...@@ -137,7 +123,7 @@ namespace dlib ...@@ -137,7 +123,7 @@ namespace dlib
ReleaseSemaphore(hCountSema,(LONG)1,NULL); ReleaseSemaphore(hCountSema,(LONG)1,NULL);
// relock the associated mutex // relock the associated mutex
WaitForSingleObject (m.hMutex,INFINITE); m.lock();
} }
bool wait_or_timeout ( bool wait_or_timeout (
...@@ -145,14 +131,14 @@ namespace dlib ...@@ -145,14 +131,14 @@ namespace dlib
) const ) const
{ {
// get a lock on the mutex for the waiters variable // get a lock on the mutex for the waiters variable
WaitForSingleObject (hWaitersMutex,INFINITE); waiters_mutex.lock();
// mark that one more thread will be waiting on this signaler // mark that one more thread will be waiting on this signaler
++waiters; ++waiters;
// release the mutex for waiters // release the mutex for waiters
ReleaseMutex(hWaitersMutex); waiters_mutex.unlock();
// release the assocaited mutex // release the associated mutex
ReleaseMutex(m.hMutex); m.unlock();
bool value; bool value;
...@@ -165,12 +151,12 @@ namespace dlib ...@@ -165,12 +151,12 @@ namespace dlib
value = false; value = false;
// get a lock on the mutex for the waiters variable // get a lock on the mutex for the waiters variable
WaitForSingleObject (hWaitersMutex,INFINITE); waiters_mutex.lock();
// mark that one less thread will be waiting on this signaler. // mark that one less thread will be waiting on this signaler.
if (waiters != 0) if (waiters != 0)
--waiters; --waiters;
// release the mutex for waiters // release the mutex for waiters
ReleaseMutex(hWaitersMutex); waiters_mutex.unlock();
} }
else else
{ {
...@@ -181,7 +167,7 @@ namespace dlib ...@@ -181,7 +167,7 @@ namespace dlib
ReleaseSemaphore(hCountSema,(LONG)1,NULL); ReleaseSemaphore(hCountSema,(LONG)1,NULL);
// relock the associated mutex // relock the associated mutex
WaitForSingleObject (m.hMutex,INFINITE); m.lock();
return value; return value;
} }
...@@ -190,7 +176,7 @@ namespace dlib ...@@ -190,7 +176,7 @@ namespace dlib
) const ) const
{ {
// get a lock on the mutex for the waiters variable // get a lock on the mutex for the waiters variable
WaitForSingleObject (hWaitersMutex,INFINITE); waiters_mutex.lock();
if (waiters > 0) if (waiters > 0)
{ {
...@@ -203,14 +189,14 @@ namespace dlib ...@@ -203,14 +189,14 @@ namespace dlib
} }
// release the mutex for waiters // release the mutex for waiters
ReleaseMutex(hWaitersMutex); waiters_mutex.unlock();
} }
void broadcast ( void broadcast (
) const ) const
{ {
// get a lock on the mutex for the waiters variable // get a lock on the mutex for the waiters variable
WaitForSingleObject (hWaitersMutex,INFINITE); waiters_mutex.lock();
if (waiters > 0) if (waiters > 0)
{ {
...@@ -227,7 +213,7 @@ namespace dlib ...@@ -227,7 +213,7 @@ namespace dlib
} }
// release the mutex for waiters // release the mutex for waiters
ReleaseMutex(hWaitersMutex); waiters_mutex.unlock();
} }
const mutex& get_mutex ( const mutex& get_mutex (
...@@ -238,7 +224,7 @@ namespace dlib ...@@ -238,7 +224,7 @@ namespace dlib
mutable HANDLE hSemaphore; mutable HANDLE hSemaphore;
mutable unsigned long waiters; mutable unsigned long waiters;
mutable HANDLE hWaitersMutex; mutex waiters_mutex;
mutable HANDLE hCountSema; mutable HANDLE hCountSema;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment