| 86 | } |
| 87 | |
| 88 | bool thread_cond::timed_wait(long long microseconds, bool locked) |
| 89 | { |
| 90 | struct timeval tv; |
| 91 | gettimeofday(&tv, NULL); |
| 92 | |
| 93 | long long ns = (tv.tv_usec + microseconds % SEC_TO_US) * US_TO_NS; |
| 94 | // Sanity check. |
| 95 | if (ns < 0) { |
| 96 | ns = 0; |
| 97 | } |
| 98 | |
| 99 | struct timespec ts; |
| 100 | ts.tv_sec = (long) (tv.tv_sec + microseconds / SEC_TO_US); |
| 101 | ts.tv_sec += (long) (ns / SEC_TO_NS); |
| 102 | ts.tv_nsec = (long) (ns % SEC_TO_NS); |
| 103 | |
| 104 | bool locked_internal; |
| 105 | if (mutex_internal_ || !locked) { |
| 106 | if (!mutex_->lock()) { |
| 107 | logger_error("lock error=%s", last_serror()); |
| 108 | return false; |
| 109 | } |
| 110 | locked_internal = true; |
| 111 | } else { |
| 112 | locked_internal = false; |
| 113 | } |
| 114 | |
| 115 | int ret = acl_pthread_cond_timedwait(cond_, mutex_->get_mutex(), &ts); |
| 116 | if (ret) { |
| 117 | #ifdef ACL_UNIX |
| 118 | acl_set_error(ret); |
| 119 | #endif |
| 120 | if (ret != ACL_ETIMEDOUT) { |
| 121 | logger_error("pthread_cond_timedwait error=%s", |
| 122 | last_serror()); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | if (locked_internal && !mutex_->unlock()) { |
| 127 | logger_error("mutex unlock error=%s", last_serror()); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | return ret == 0; |
| 132 | } |
| 133 | |
| 134 | thread_mutex& thread_cond::get_mutex() const |
| 135 | { |
nothing calls this directly
no test coverage detected