Time wait in us timeout < 0 would cause ETIMEOUT and return false immediately
| 130 | // Time wait in us |
| 131 | // timeout < 0 would cause ETIMEOUT and return false immediately |
| 132 | bool TimeWaitInUs(int64_t timeout, const char *msg = NULL) { |
| 133 | // ref: |
| 134 | // http://www.qnx.com/developers/docs/6.5.0SP1.update/com.qnx.doc.neutrino_lib_ref/p/pthread_cond_timedwait.html |
| 135 | struct timespec ts; |
| 136 | clock_gettime(CLOCK_MONOTONIC, &ts); |
| 137 | int64_t nsec = timeout * 1000 + ts.tv_nsec; |
| 138 | |
| 139 | assert(nsec > 0); |
| 140 | |
| 141 | ts.tv_sec += nsec / 1000000000; |
| 142 | ts.tv_nsec = nsec % 1000000000; |
| 143 | |
| 144 | int64_t msg_threshold = mu_->msg_threshold_; |
| 145 | mu_->BeforeUnlock(); |
| 146 | int ret = pthread_cond_timedwait(&cond_, &mu_->mu_, &ts); |
| 147 | mu_->AfterLock(msg, msg_threshold); |
| 148 | return (ret == 0); |
| 149 | } |
| 150 | // Time wait in ms |
| 151 | // timeout < 0 would cause ETIMEOUT and return false immediately |
| 152 | bool TimeWait(int timeout, const char *msg = NULL) { return TimeWaitInUs(timeout * 1000LL, msg); } |
no test coverage detected