Initial source commit
This commit is contained in:
commit
f1384c11ee
335 changed files with 52715 additions and 0 deletions
94
minorGems/system/win32/BinarySemaphoreWin32.cpp
Normal file
94
minorGems/system/win32/BinarySemaphoreWin32.cpp
Normal file
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* 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.
|
||||
*
|
||||
* 2003-August-26 Jason Rohrer
|
||||
* Added support for timeouts on wait.
|
||||
*/
|
||||
|
||||
#include "minorGems/system/BinarySemaphore.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
/**
|
||||
* Win32-specific implementation of the BinarySemaphore class member functions.
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* Native object pointer A is the semaphore handle.
|
||||
* Pointer B is not used.
|
||||
*/
|
||||
|
||||
|
||||
BinarySemaphore::BinarySemaphore() :
|
||||
mSemaphoreValue( 0 ) {
|
||||
|
||||
// allocate a handle on the heap
|
||||
mNativeObjectPointerA = (void *)( new HANDLE[1] );
|
||||
|
||||
// retrieve handle from the heap
|
||||
HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;
|
||||
|
||||
|
||||
semaphorePointer[0] = CreateSemaphore(
|
||||
(LPSECURITY_ATTRIBUTES) NULL, // no attributes
|
||||
0, // initial count
|
||||
1, // maximum count
|
||||
(LPCTSTR) NULL ); // no name
|
||||
|
||||
}
|
||||
|
||||
BinarySemaphore::~BinarySemaphore() {
|
||||
|
||||
// retrieve handle from the heap
|
||||
HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;
|
||||
|
||||
// destroy the semaphore
|
||||
CloseHandle( semaphorePointer[0] );
|
||||
|
||||
// de-allocate the handle from the heap
|
||||
delete [] semaphorePointer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int BinarySemaphore::wait( int inTimeoutInMilliseconds ) {
|
||||
|
||||
// retrieve handle from the heap
|
||||
HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;
|
||||
|
||||
if( inTimeoutInMilliseconds == -1 ) {
|
||||
WaitForSingleObject( semaphorePointer[0], INFINITE );
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
// timeout
|
||||
int result = WaitForSingleObject( semaphorePointer[0],
|
||||
inTimeoutInMilliseconds );
|
||||
|
||||
if( result == WAIT_TIMEOUT ) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void BinarySemaphore::signal() {
|
||||
|
||||
// retrieve handle from the heap
|
||||
HANDLE *semaphorePointer = (HANDLE *)mNativeObjectPointerA;
|
||||
|
||||
ReleaseSemaphore( semaphorePointer[0], 1, (LPLONG) NULL );
|
||||
}
|
29
minorGems/system/win32/LauncherWin32.cpp
Normal file
29
minorGems/system/win32/LauncherWin32.cpp
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2003-January-10 Jason Rohrer
|
||||
* Created.
|
||||
*
|
||||
* 2003-March-24 Jason Rohrer
|
||||
* Fixed a syntax typo.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#include "minorGems/system/Launcher.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <process.h>
|
||||
|
||||
|
||||
|
||||
void Launcher::launchCommand( char *inCommandName,
|
||||
char **inArguments ) {
|
||||
|
||||
_spawnvp( _P_NOWAIT,
|
||||
inCommandName,
|
||||
inArguments );
|
||||
}
|
||||
|
||||
|
||||
|
81
minorGems/system/win32/MutexLockWin32.cpp
Normal file
81
minorGems/system/win32/MutexLockWin32.cpp
Normal file
|
@ -0,0 +1,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 );
|
||||
}
|
||||
|
108
minorGems/system/win32/ThreadWin32.cpp
Normal file
108
minorGems/system/win32/ThreadWin32.cpp
Normal file
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
* 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.
|
||||
*
|
||||
* 2004-March-31 Jason Rohrer
|
||||
* Added missing call to CloseHandle in destructor.
|
||||
* Added support for detatched mode.
|
||||
*
|
||||
* 2004-April-1 Jason Rohrer
|
||||
* Fixed a bug in CloseHandle call pointed out by Mycroftxxx.
|
||||
*
|
||||
* 2005-January-22 Jason Rohrer
|
||||
* Added a static sleep function.
|
||||
*/
|
||||
|
||||
#include "minorGems/system/Thread.h"
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
|
||||
/**
|
||||
* Win32-specific implementation of the Thread class member functions.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
// prototype
|
||||
/**
|
||||
* A wrapper for the run method, since windows thread (perhaps) won't take
|
||||
* a class's member function. Takes a pointer to the Thread to run,
|
||||
* cast as a void*;
|
||||
*/
|
||||
DWORD WINAPI win32ThreadFunction( void * );
|
||||
|
||||
|
||||
|
||||
Thread::Thread() {
|
||||
// allocate a handle on the heap
|
||||
mNativeObjectPointer = (void *)( new HANDLE[1] );
|
||||
}
|
||||
|
||||
|
||||
|
||||
Thread::~Thread() {
|
||||
// get a pointer to the allocated handle
|
||||
HANDLE *threadPointer = (HANDLE *)mNativeObjectPointer;
|
||||
|
||||
// close the handle to ensure that the thread resources are freed
|
||||
CloseHandle( threadPointer[0] );
|
||||
|
||||
// de-allocate the thread handle from the heap
|
||||
delete [] threadPointer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Thread::start( char inDetach ) {
|
||||
|
||||
mIsDetached = inDetach;
|
||||
|
||||
// get a pointer to the allocated handle
|
||||
HANDLE *threadPointer = (HANDLE *)mNativeObjectPointer;
|
||||
|
||||
DWORD threadID;
|
||||
|
||||
threadPointer[0] = CreateThread(
|
||||
(LPSECURITY_ATTRIBUTES)NULL, // no attributes
|
||||
(DWORD)0, // default stack size
|
||||
win32ThreadFunction, // function
|
||||
(LPVOID)this, // function arg
|
||||
(DWORD)0, // no creation flags (start thread immediately)
|
||||
&threadID );
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Thread::join() {
|
||||
|
||||
HANDLE *threadPointer = (HANDLE *)mNativeObjectPointer;
|
||||
|
||||
WaitForSingleObject( threadPointer[0], INFINITE );
|
||||
}
|
||||
|
||||
|
||||
void Thread::staticSleep( unsigned long inTimeInMilliseconds ) {
|
||||
Sleep( inTimeInMilliseconds );
|
||||
}
|
||||
|
||||
|
||||
// takes a pointer to a Thread object as the data value
|
||||
DWORD WINAPI win32ThreadFunction( void *inPtrToThread ) {
|
||||
Thread *threadToRun = (Thread *)inPtrToThread;
|
||||
threadToRun->run();
|
||||
|
||||
|
||||
if( threadToRun->isDetatched() ) {
|
||||
// thread detached, so we must destroy it
|
||||
delete threadToRun;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
77
minorGems/system/win32/TimeWin32.cpp
Normal file
77
minorGems/system/win32/TimeWin32.cpp
Normal file
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Modification History
|
||||
*
|
||||
* 2001-November-7 Jason Rohrer
|
||||
* Created.
|
||||
*
|
||||
* 2002-April-11 Jason Rohrer
|
||||
* Added missing include, and fixed a bug.
|
||||
*
|
||||
* 2004-January-29 Jason Rohrer
|
||||
* Fixed so that 0-point of time is the same as on other platforms.
|
||||
*
|
||||
* 2004-October-14 Jason Rohrer
|
||||
* Fixed bug in second/millisecond callibration.
|
||||
* Fixed bug in win32 time to ANSI time translation.
|
||||
* Fixed daylight savings time bug.
|
||||
*/
|
||||
|
||||
|
||||
#include "minorGems/system/Time.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <winbase.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Windows implementation of Time.h.
|
||||
*
|
||||
* The 0-point should match the ANSI standard.
|
||||
*/
|
||||
|
||||
|
||||
|
||||
void Time::getCurrentTime( unsigned long *outSeconds,
|
||||
unsigned long *outMilliseconds ) {
|
||||
// convert from win32 broken-down time (which has msec resolution)
|
||||
// to an ANSI time struct and then convert to an absolute time in
|
||||
// seconds
|
||||
// This procedure ensures that the 0-point matches the ANSI standard.
|
||||
|
||||
// note:
|
||||
// we cannot simply call ANSI time() to get the seconds and then rely
|
||||
// on GetLocalTime to get the milliseconds, since the seconds value
|
||||
// used by GetLocalTime is (strangely enough) not calibrated to the seconds
|
||||
// value of time().
|
||||
// In other words, it is possible for the time() seconds to advance
|
||||
// at a different clock cycle than the GetLocalTime seconds.
|
||||
|
||||
// get time using a win32 call
|
||||
SYSTEMTIME win32TimeStruct;
|
||||
GetLocalTime( &win32TimeStruct );
|
||||
|
||||
// convert this win32 structure to the ANSI standard structure
|
||||
struct tm ansiTimeStruct;
|
||||
|
||||
ansiTimeStruct.tm_sec = win32TimeStruct.wSecond;
|
||||
ansiTimeStruct.tm_min = win32TimeStruct.wMinute;
|
||||
ansiTimeStruct.tm_hour = win32TimeStruct.wHour;
|
||||
ansiTimeStruct.tm_mday = win32TimeStruct.wDay;
|
||||
// ANSI time struct has month in range [0..11]
|
||||
ansiTimeStruct.tm_mon = win32TimeStruct.wMonth - 1;
|
||||
// ANSI time struct has year that is an offset from 1900
|
||||
ansiTimeStruct.tm_year = win32TimeStruct.wYear - 1900;
|
||||
// unknown daylight savings time (dst) status
|
||||
// if we fail to init this value, we can get inconsistent results
|
||||
ansiTimeStruct.tm_isdst = -1;
|
||||
|
||||
unsigned long secondsSinceEpoch = mktime( &ansiTimeStruct );
|
||||
|
||||
*outSeconds = secondsSinceEpoch;
|
||||
*outMilliseconds = (unsigned long)( win32TimeStruct.wMilliseconds );
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue