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
|
/*
* Modification History
*
* 2000-December-13 Jason Rohrer
* Created.
*
* 2002-October-18 Jason Rohrer
* Moved common include out of header and into platform-specific cpp files,
* since MemoryTrack uses a mutex lock.
* Changed to use malloc instead of new internally to work with debugMemory.
* Made use of mNativeObjectPointer a bit cleaner.
*/
#include "minorGems/common.h"
#include <minorGems/system/MutexLock.h>
#include <pthread.h>
#include <stdlib.h>
/**
* Linux-specific implementation of the MutexLock class member functions.
*
* May also be compatible with other POSIX-like systems.
*
* To compile:
* g++ -lpthread
*/
MutexLock::MutexLock() {
// allocate a mutex structure on the heap
mNativeObjectPointer = malloc( sizeof( pthread_mutex_t ) );
// get a pointer to the mutex
pthread_mutex_t *mutexPointer =
(pthread_mutex_t *)mNativeObjectPointer;
// init the mutex
pthread_mutex_init( mutexPointer, NULL );
}
MutexLock::~MutexLock() {
// get a pointer to the mutex
pthread_mutex_t *mutexPointer =
(pthread_mutex_t *)mNativeObjectPointer;
// destroy the mutex
pthread_mutex_destroy( mutexPointer );
// de-allocate the mutex structure from the heap
free( mNativeObjectPointer );
}
void MutexLock::lock() {
// get a pointer to the mutex
pthread_mutex_t *mutexPointer =
(pthread_mutex_t *)mNativeObjectPointer;
pthread_mutex_lock( mutexPointer );
}
void MutexLock::unlock() {
// get a pointer to the mutex
pthread_mutex_t *mutexPointer =
(pthread_mutex_t *)mNativeObjectPointer;
pthread_mutex_unlock( mutexPointer );
}
|