Version 4.2.0

This commit is contained in:
Gary Scavone
2009-03-24 23:02:14 -04:00
committed by Stephen Sinclair
parent cf06b7598b
commit a6381b9d38
281 changed files with 17152 additions and 12000 deletions

View File

@@ -7,7 +7,7 @@
systems, the pthread library is used. Under Windows,
the Windows thread library is used.
by Perry R. Cook and Gary P. Scavone, 1995 - 2002.
by Perry R. Cook and Gary P. Scavone, 1995 - 2004.
*/
/***************************************************/
@@ -15,66 +15,75 @@
Thread :: Thread()
{
thread = 0;
thread_ = 0;
}
Thread :: ~Thread()
{
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
pthread_cancel(thread);
pthread_join(thread, NULL);
#elif defined(__OS_WINDOWS__)
if ( thread )
TerminateThread((HANDLE)thread, 0);
#endif
}
bool Thread :: start( THREAD_FUNCTION routine, void * ptr )
{
bool result = false;
if ( thread_ ) {
errorString_ << "Thread:: a thread is already running!";
handleError( StkError::WARNING );
return false;
}
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
if ( pthread_create(&thread, NULL, *routine, ptr) == 0 )
result = true;
if ( pthread_create(&thread_, NULL, *routine, ptr) == 0 )
return true;
#elif defined(__OS_WINDOWS__)
unsigned thread_id;
thread = _beginthreadex(NULL, 0, routine, ptr, 0, &thread_id);
if ( thread ) result = true;
thread_ = _beginthreadex(NULL, 0, routine, ptr, 0, &thread_id);
if ( thread_ ) return true;
#endif
return result;
return false;
}
bool Thread :: wait( long milliseconds )
bool Thread :: cancel()
{
bool result = false;
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
pthread_cancel(thread);
pthread_join(thread, NULL);
if ( pthread_cancel(thread_) == 0 ) {
return true;
}
#elif defined(__OS_WINDOWS__)
DWORD timeout, retval;
if ( milliseconds < 0 ) timeout = INFINITE;
else timeout = milliseconds;
retval = WaitForSingleObject( (HANDLE)thread, timeout );
TerminateThread((HANDLE)thread_, 0);
return true;
#endif
return false;
}
bool Thread :: wait()
{
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
if ( pthread_join(thread_, NULL) == 0 ) {
thread_ = 0;
return true;
}
#elif defined(__OS_WINDOWS__)
long retval = WaitForSingleObject( (HANDLE)thread_, INFINITE );
if ( retval == WAIT_OBJECT_0 ) {
CloseHandle( (HANDLE)thread );
thread = 0;
result = true;
CloseHandle( (HANDLE)thread_ );
thread_ = 0;
return true;
}
#endif
return result;
return false;
}
void Thread :: test(void)
void Thread :: testCancel(void)
{
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
@@ -82,57 +91,3 @@ void Thread :: test(void)
#endif
}
Mutex :: Mutex()
{
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
pthread_mutex_init(&mutex, NULL);
#elif defined(__OS_WINDOWS__)
InitializeCriticalSection(&mutex);
#endif
}
Mutex :: ~Mutex()
{
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
pthread_mutex_destroy(&mutex);
#elif defined(__OS_WINDOWS__)
DeleteCriticalSection(&mutex);
#endif
}
void Mutex :: lock()
{
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
pthread_mutex_lock(&mutex);
#elif defined(__OS_WINDOWS__)
EnterCriticalSection(&mutex);
#endif
}
void Mutex :: unlock()
{
#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
pthread_mutex_unlock(&mutex);
#elif defined(__OS_WINDOWS__)
LeaveCriticalSection(&mutex);
#endif
}