#include <folly/portability/SysFile.h>
#ifdef _WIN32
#include <limits>
#include <folly/portability/Windows.h>
extern "C" int flock(int fd, int operation) {
HANDLE h = (HANDLE)_get_osfhandle(fd);
if (h == INVALID_HANDLE_VALUE) {
return -1;
}
constexpr DWORD kMaxDWORD = std::numeric_limits<DWORD>::max();
if (operation & LOCK_UN) {
if (!UnlockFile(h, 0, 0, kMaxDWORD, kMaxDWORD)) {
return -1;
}
} else {
DWORD flags = DWORD(
(operation & LOCK_NB ? LOCKFILE_FAIL_IMMEDIATELY : 0) |
(operation & LOCK_EX ? LOCKFILE_EXCLUSIVE_LOCK : 0));
OVERLAPPED ov = {};
if (!LockFileEx(h, flags, 0, kMaxDWORD, kMaxDWORD, &ov)) {
return -1;
}
}
return 0;
}
#endif