Panda3D
 All Classes Functions Variables Enumerations
conditionVarPosixImpl.cxx
1 // Filename: conditionVarPosixImpl.cxx
2 // Created by: drose (10Feb06)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "selectThreadImpl.h"
16 
17 #ifdef HAVE_POSIX_THREADS
18 
19 #include "conditionVarPosixImpl.h"
20 #include <sys/time.h>
21 
22 ////////////////////////////////////////////////////////////////////
23 // Function: ConditionVarPosixImpl::wait
24 // Access: Public
25 // Description:
26 ////////////////////////////////////////////////////////////////////
27 void ConditionVarPosixImpl::
28 wait(double timeout) {
29  //TAU_PROFILE("ConditionVarPosixImpl::wait()", " ", TAU_USER);
30 
31  struct timeval now;
32  gettimeofday(&now, NULL);
33 
34  // Convert from timeval to timespec
35  struct timespec ts;
36  ts.tv_sec = now.tv_sec;
37  ts.tv_nsec = now.tv_usec * 1000;
38 
39  int seconds = (int)floor(timeout);
40  ts.tv_sec += seconds;
41  ts.tv_nsec += (int)((timeout - seconds) * 1000000.0);
42  if (ts.tv_nsec > 1000000) {
43  ts.tv_nsec -= 1000000;
44  ++ts.tv_sec;
45  }
46 
47  int result = pthread_cond_timedwait(&_cvar, &_mutex._lock, &ts);
48 #ifndef NDEBUG
49  if (result != 0 && result != ETIMEDOUT) {
50  pipeline_cat.error()
51  << "Unexpected error " << result << " from pthread_cond_timedwait()\n";
52  }
53 #endif
54 }
55 
56 #endif // HAVE_POSIX_THREADS