moved flock_timedwait_monotonic() to tools.h

This commit is contained in:
Devaev Maxim
2021-01-20 12:36:37 +03:00
parent e08ac1467f
commit 34e0e4dab4
2 changed files with 18 additions and 19 deletions

View File

@@ -32,6 +32,8 @@
#include <time.h>
#include <assert.h>
#include <sys/file.h>
#define A_CALLOC(_dest, _nmemb) assert((_dest = calloc(_nmemb, sizeof(*(_dest)))))
#define A_REALLOC(_dest, _nmemb) assert((_dest = realloc(_dest, _nmemb * sizeof(*(_dest)))))
@@ -123,3 +125,17 @@ INLINE unsigned get_cores_available(void) {
cores_sysconf = (cores_sysconf < 0 ? 0 : cores_sysconf);
return max_u(min_u(cores_sysconf, 4), 1);
}
INLINE int flock_timedwait_monotonic(int fd, long double timeout) {
long double deadline_ts = get_now_monotonic() + timeout;
int retval = -1;
while (true) {
retval = flock(fd, LOCK_EX | LOCK_NB);
if (retval == 0 || errno != EWOULDBLOCK || get_now_monotonic() > deadline_ts) {
break;
}
usleep(1000);
}
return retval;
}