head 1.4; access; symbols; locks; strict; comment @# @; 1.4 date 2002.09.07.01.50.35; author curt; state Exp; branches; next 1.3; 1.3 date 2002.08.31.19.01.50; author curt; state Exp; branches; next 1.2; 1.2 date 2002.08.31.18.53.58; author curt; state Exp; branches; next 1.1; 1.1 date 2002.02.15.17.21.54; author curt; state Exp; branches; next ; desc @@ 1.4 log @Updated doxygen docs. @ text @
Main Page Class Hierarchy Compound List File List Compound Members File Members

# SGThread.cxx

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

@ 1.3 log @Oops, try again. @ text @d60 49 a108 24 00052 bool 00053 SGCondition::wait( SGMutex& mutex, unsigned long ms ) 00054 { 00055 struct timeval now; 00056 ::gettimeofday( &now, 0 ); 00057 00058 // Wait time is now + ms milliseconds 00059 unsigned int sec = ms / 1000; 00060 unsigned int nsec = (ms % 1000) * 1000; 00061 struct timespec abstime; 00062 abstime.tv_sec = now.tv_sec + sec; 00063 abstime.tv_nsec = now.tv_usec*1000 + nsec; 00064 00065 int status = pthread_cond_timedwait( &cond, &mutex.mutex, &abstime ); 00066 if (status == ETIMEDOUT) 00067 { 00068 return false; 00069 } 00070 00071 assert( status == 0 ); 00072 return true; 00073 } 00074