|
113 | 113 | #endif
|
114 | 114 |
|
115 | 115 |
|
116 |
| -#define MICROSECONDS_TO_TIMESPEC(microseconds, ts) \ |
117 |
| -do { \ |
118 |
| - struct timeval tv; \ |
119 |
| - gettimeofday(&tv, NULL); \ |
120 |
| - tv.tv_usec += microseconds % 1000000; \ |
121 |
| - tv.tv_sec += microseconds / 1000000; \ |
122 |
| - tv.tv_sec += tv.tv_usec / 1000000; \ |
123 |
| - tv.tv_usec %= 1000000; \ |
124 |
| - ts.tv_sec = tv.tv_sec; \ |
125 |
| - ts.tv_nsec = tv.tv_usec * 1000; \ |
126 |
| -} while(0) |
127 |
| - |
128 |
| - |
129 | 116 | /*
|
130 | 117 | * pthread_cond support
|
131 | 118 | */
|
@@ -156,23 +143,23 @@ _PyThread_cond_init(PyCOND_T *cond)
|
156 | 143 | return pthread_cond_init(cond, condattr_monotonic);
|
157 | 144 | }
|
158 | 145 |
|
| 146 | + |
159 | 147 | void
|
160 | 148 | _PyThread_cond_after(long long us, struct timespec *abs)
|
161 | 149 | {
|
| 150 | + _PyTime_t timeout = _PyTime_FromMicrosecondsClamp(us); |
| 151 | + _PyTime_t t; |
162 | 152 | #ifdef CONDATTR_MONOTONIC
|
163 | 153 | if (condattr_monotonic) {
|
164 |
| - clock_gettime(CLOCK_MONOTONIC, abs); |
165 |
| - abs->tv_sec += us / 1000000; |
166 |
| - abs->tv_nsec += (us % 1000000) * 1000; |
167 |
| - abs->tv_sec += abs->tv_nsec / 1000000000; |
168 |
| - abs->tv_nsec %= 1000000000; |
169 |
| - return; |
| 154 | + t = _PyTime_GetMonotonicClock(); |
170 | 155 | }
|
| 156 | + else |
171 | 157 | #endif
|
172 |
| - |
173 |
| - struct timespec ts; |
174 |
| - MICROSECONDS_TO_TIMESPEC(us, ts); |
175 |
| - *abs = ts; |
| 158 | + { |
| 159 | + t = _PyTime_GetSystemClock(); |
| 160 | + } |
| 161 | + t = _PyTime_Add(t, timeout); |
| 162 | + _PyTime_AsTimespec_clamp(t, abs); |
176 | 163 | }
|
177 | 164 |
|
178 | 165 |
|
@@ -639,17 +626,17 @@ PyThread_acquire_lock_timed(PyThread_type_lock lock, PY_TIMEOUT_T microseconds,
|
639 | 626 | goto unlock;
|
640 | 627 | }
|
641 | 628 |
|
642 |
| - struct timespec abs; |
| 629 | + struct timespec abs_timeout; |
643 | 630 | if (microseconds > 0) {
|
644 |
| - _PyThread_cond_after(microseconds, &abs); |
| 631 | + _PyThread_cond_after(microseconds, &abs_timeout); |
645 | 632 | }
|
646 | 633 | // Continue trying until we get the lock
|
647 | 634 |
|
648 | 635 | // mut must be locked by me -- part of the condition protocol
|
649 | 636 | while (1) {
|
650 | 637 | if (microseconds > 0) {
|
651 | 638 | status = pthread_cond_timedwait(&thelock->lock_released,
|
652 |
| - &thelock->mut, &abs); |
| 639 | + &thelock->mut, &abs_timeout); |
653 | 640 | if (status == 1) {
|
654 | 641 | break;
|
655 | 642 | }
|
|
0 commit comments