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