SGThread.cxx

00001 #include <simgear/compiler.h>
00002 
00003 #if defined(_MSC_VER) || defined(__MINGW32__)
00004 #  include <time.h>
00005 #else
00006 #  if defined ( sgi ) && !defined( __GNUC__ )
00007      // This works around a bug triggered when using MipsPro 7.4.1
00008      // and (at least) IRIX 6.5.20
00009 #    include <iostream>
00010 #  endif
00011 #  include <sys/time.h>
00012 #endif
00013 #if _MSC_VER >= 1300
00014 #  include "winsock2.h"
00015 #endif
00016 
00017 #include "SGThread.hxx"
00018 
00019 void*
00020 start_handler( void* arg )
00021 {
00022     SGThread* thr = static_cast<SGThread*>(arg);
00023     thr->run();
00024     return 0;
00025 }
00026 
00027 void
00028 SGThread::set_cancel( cancel_t mode )
00029 {
00030     switch (mode)
00031     {
00032     case CANCEL_DISABLE:
00033         pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, 0 );
00034         break;
00035     case CANCEL_DEFERRED:
00036         pthread_setcanceltype( PTHREAD_CANCEL_DEFERRED, 0 );
00037         pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, 0 );
00038         break;
00039     case CANCEL_IMMEDIATE:
00040         pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, 0 );
00041         pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, 0 );
00042         break;
00043     default:
00044         break;
00045     }
00046 }
00047 
00048 bool
00049 SGMutex::trylock()
00050 {
00051     int status = pthread_mutex_lock( &mutex );
00052     if (status == EBUSY)
00053     {
00054         return false;
00055     }
00056     assert( status == 0 );
00057     return true;
00058 }
00059 
00060 #if defined(_MSC_VER) || defined(__MINGW32__)
00061 int gettimeofday(struct timeval* tp, void* tzp) {
00062     LARGE_INTEGER t;
00063 
00064     if(QueryPerformanceCounter(&t)) {
00065         /* hardware supports a performance counter */
00066         LARGE_INTEGER f;
00067         QueryPerformanceFrequency(&f);
00068         tp->tv_sec = t.QuadPart/f.QuadPart;
00069         tp->tv_usec = ((float)t.QuadPart/f.QuadPart*1000*1000)
00070             - (tp->tv_sec*1000*1000);
00071     } else {
00072         /* hardware doesn't support a performance counter, so get the
00073            time in a more traditional way. */
00074         DWORD t;
00075         t = timeGetTime();
00076         tp->tv_sec = t / 1000;
00077         tp->tv_usec = t % 1000;
00078     }
00079 
00080     /* 0 indicates that the call succeeded. */
00081     return 0;
00082 }
00083 #endif
00084 
00085 bool
00086 SGPthreadCond::wait( SGMutex& mutex, unsigned long ms )
00087 {
00088     struct timeval now;
00089     ::gettimeofday( &now, 0 );
00090 
00091     // Wait time is now + ms milliseconds
00092     unsigned int sec = ms / 1000;
00093     unsigned int nsec = (ms % 1000) * 1000;
00094     struct timespec abstime;
00095     abstime.tv_sec = now.tv_sec + sec;
00096     abstime.tv_nsec = now.tv_usec*1000 + nsec;
00097 
00098     int status = pthread_cond_timedwait( &cond, &mutex.mutex, &abstime );
00099     if (status == ETIMEDOUT)
00100     {
00101         return false;
00102     }
00103 
00104     assert( status == 0 );
00105     return true;
00106 }
00107 

Generated on Mon Dec 17 09:30:55 2007 for SimGear by  doxygen 1.5.1