| 215 | } |
| 216 | |
| 217 | void lock() { |
| 218 | #ifdef STAR_MUTEX_TIMED |
| 219 | timespec ts; |
| 220 | clock_gettime(CLOCK_REALTIME, &ts); |
| 221 | ts.tv_sec += 15; |
| 222 | if (pthread_mutex_timedlock(&mutex, &ts) != 0) { |
| 223 | printStack("RecursiveMutex::lock is TAKING TOO LONG 🎃"); |
| 224 | #else |
| 225 | { |
| 226 | #endif |
| 227 | pthread_mutex_lock(&mutex); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void unlock() { |
| 232 | pthread_mutex_unlock(&mutex); |
| 233 | } |
| 234 | |
| 235 | bool tryLock() { |
| 236 | if (pthread_mutex_trylock(&mutex) == 0) |
| 237 | return true; |
| 238 | else |
| 239 | return false; |
| 240 | } |
| 241 | |
| 242 | pthread_mutex_t mutex; |
| 243 | }; |
| 244 | |
| 245 | void Thread::sleepPrecise(unsigned msecs) { |
| 246 | int64_t now = Time::monotonicMilliseconds(); |
| 247 | int64_t deadline = now + msecs; |
| 248 | |
| 249 | while (deadline - now > 10) { |
| 250 | usleep((deadline - now - 10) * 1000); |
| 251 | now = Time::monotonicMilliseconds(); |
| 252 | } |
| 253 | |
| 254 | while (deadline > now) { |
| 255 | usleep((deadline - now) * 500); |
| 256 | now = Time::monotonicMilliseconds(); |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void Thread::sleep(unsigned msecs) { |
| 261 | usleep(msecs * 1000); |
| 262 | } |
| 263 | |
| 264 | void Thread::yield() { |
| 265 | sched_yield(); |
| 266 | } |
| 267 | |
| 268 | unsigned Thread::numberOfProcessors() { |
| 269 | long nprocs = sysconf(_SC_NPROCESSORS_ONLN); |
| 270 | if (nprocs < 1) |
| 271 | throw StarException(strf("Could not determine number of CPUs online: {}\n", strerror(errno))); |
| 272 | return nprocs; |
| 273 | } |
| 274 |
no test coverage detected