| 117 | } |
| 118 | |
| 119 | bool SyncImpl::wait(uint32_t ms) |
| 120 | { |
| 121 | PxUnixScopeLock lock(getSync(this)->mutex); |
| 122 | int lastSetCounter = getSync(this)->setCounter; |
| 123 | if(!getSync(this)->is_set) |
| 124 | { |
| 125 | if(ms == uint32_t(-1)) |
| 126 | { |
| 127 | // have to loop here and check is_set since pthread_cond_wait can return successfully |
| 128 | // even if it was not signaled by pthread_cond_broadcast (OS efficiency design decision) |
| 129 | int status = 0; |
| 130 | while(!status && !getSync(this)->is_set && (lastSetCounter == getSync(this)->setCounter)) |
| 131 | status = pthread_cond_wait(&getSync(this)->cond, &getSync(this)->mutex); |
| 132 | PX_ASSERT((!status && getSync(this)->is_set) || (lastSetCounter != getSync(this)->setCounter)); |
| 133 | } |
| 134 | else |
| 135 | { |
| 136 | timespec ts; |
| 137 | timeval tp; |
| 138 | gettimeofday(&tp, NULL); |
| 139 | uint32_t sec = ms / 1000; |
| 140 | uint32_t usec = (ms - 1000 * sec) * 1000; |
| 141 | |
| 142 | // sschirm: taking into account that us might accumulate to a second |
| 143 | // otherwise the pthread_cond_timedwait complains on osx. |
| 144 | usec = tp.tv_usec + usec; |
| 145 | uint32_t div_sec = usec / 1000000; |
| 146 | uint32_t rem_usec = usec - div_sec * 1000000; |
| 147 | |
| 148 | ts.tv_sec = tp.tv_sec + sec + div_sec; |
| 149 | ts.tv_nsec = rem_usec * 1000; |
| 150 | |
| 151 | // have to loop here and check is_set since pthread_cond_timedwait can return successfully |
| 152 | // even if it was not signaled by pthread_cond_broadcast (OS efficiency design decision) |
| 153 | int status = 0; |
| 154 | while(!status && !getSync(this)->is_set && (lastSetCounter == getSync(this)->setCounter)) |
| 155 | status = pthread_cond_timedwait(&getSync(this)->cond, &getSync(this)->mutex, &ts); |
| 156 | PX_ASSERT((!status && getSync(this)->is_set) || (status == ETIMEDOUT) || |
| 157 | (lastSetCounter != getSync(this)->setCounter)); |
| 158 | } |
| 159 | } |
| 160 | return getSync(this)->is_set || (lastSetCounter != getSync(this)->setCounter); |
| 161 | } |
| 162 | |
| 163 | } // namespace shdfnd |
| 164 | } // namespace physx |