MCPcopy Create free account
hub / github.com/OpenStarbound/OpenStarbound / lock

Function lock

source/core/StarThread_unix.cpp:217–416  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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
245void 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
260void Thread::sleep(unsigned msecs) {
261 usleep(msecs * 1000);
262}
263
264void Thread::yield() {
265 sched_yield();
266}
267
268unsigned 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

Callers 3

ReadLockerMethod · 0.85
WriteLockerMethod · 0.85
MLockerMethod · 0.85

Calls 1

printStackFunction · 0.70

Tested by

no test coverage detected