1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
/*
* Modification History
*
* 2001-January-27 Jason Rohrer
* Created.
*
* 2001-March-4 Jason Rohrer
* Replaced include of <winbase.h> and <windef.h> with <windows.h>
* to fix compile bugs encountered with newer windows compilers.
*
* 2002-October-18 Jason Rohrer
* Moved common include out of header and into platform-specific cpp files,
* since MemoryTrack uses a mutex lock.
*
* 2002-October-19 Jason Rohrer
* Changed to use malloc instead of new internally to work with debugMemory.
* Made use of mNativeObjectPointer a bit cleaner.
* Fixed a few bugs with new use of mNativeObjectPointer.
*/
#include "minorGems/common.h"
#include "minorGems/system/MutexLock.h"
#include <windows.h>
#include <stdlib.h>
/**
* Win32-specific implementation of the MutexLock class member functions.
*/
MutexLock::MutexLock() {
// allocate a handle on the heap
mNativeObjectPointer = malloc( sizeof( HANDLE ) );
// retrieve handle from the heap
HANDLE *mutexPointer = (HANDLE *)mNativeObjectPointer;
// create the mutex
*mutexPointer = CreateMutex(
(LPSECURITY_ATTRIBUTES) NULL, // no attributes
(BOOL) false, // not initially locked
(LPCTSTR) NULL ); // no name
}
MutexLock::~MutexLock() {
// retrieve handle from the heap
HANDLE *mutexPointer = (HANDLE *)mNativeObjectPointer;
// destroy the mutex
CloseHandle( *mutexPointer );
// de-allocate the mutex structure from the heap
free( mutexPointer );
}
void MutexLock::lock() {
// retrieve handle from the heap
HANDLE *mutexPointer = (HANDLE *)mNativeObjectPointer;
WaitForSingleObject( *mutexPointer, INFINITE );
}
void MutexLock::unlock() {
// retrieve handle from the heap
HANDLE *mutexPointer = (HANDLE *)mNativeObjectPointer;
ReleaseMutex( *mutexPointer );
}
|