Panda3D
|
00001 // Filename: conditionVarPosixImpl.cxx 00002 // Created by: drose (10Feb06) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "selectThreadImpl.h" 00016 00017 #ifdef HAVE_POSIX_THREADS 00018 00019 #include "conditionVarPosixImpl.h" 00020 #include <sys/time.h> 00021 00022 //////////////////////////////////////////////////////////////////// 00023 // Function: ConditionVarPosixImpl::wait 00024 // Access: Public 00025 // Description: 00026 //////////////////////////////////////////////////////////////////// 00027 void ConditionVarPosixImpl:: 00028 wait(double timeout) { 00029 //TAU_PROFILE("ConditionVarPosixImpl::wait()", " ", TAU_USER); 00030 00031 struct timeval now; 00032 gettimeofday(&now, NULL); 00033 00034 // Convert from timeval to timespec 00035 struct timespec ts; 00036 ts.tv_sec = now.tv_sec; 00037 ts.tv_nsec = now.tv_usec * 1000; 00038 00039 int seconds = (int)floor(timeout); 00040 ts.tv_sec += seconds; 00041 ts.tv_nsec += (int)((timeout - seconds) * 1000000.0); 00042 if (ts.tv_nsec > 1000000) { 00043 ts.tv_nsec -= 1000000; 00044 ++ts.tv_sec; 00045 } 00046 00047 int result = pthread_cond_timedwait(&_cvar, &_mutex._lock, &ts); 00048 #ifndef NDEBUG 00049 if (result != 0 && result != ETIMEDOUT) { 00050 pipeline_cat.error() 00051 << "Unexpected error " << result << " from pthread_cond_timedwait()\n"; 00052 } 00053 #endif 00054 } 00055 00056 #endif // HAVE_POSIX_THREADS