summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/syscalls.cc
diff options
context:
space:
mode:
authorAdam Borowski <kilobyte@angband.pl>2010-12-02 03:26:51 +0100
committerAdam Borowski <kilobyte@angband.pl>2010-12-02 03:26:51 +0100
commit4180ca9b4b34b5f5499836cc400a4b585cf0c521 (patch)
tree35507284cd13dbeb83b60ae1812d23d8127aff88 /crawl-ref/source/syscalls.cc
parenta82b27b5ab384984099007781721599b331071d6 (diff)
downloadcrawl-ref-4180ca9b4b34b5f5499836cc400a4b585cf0c521.tar.gz
crawl-ref-4180ca9b4b34b5f5499836cc400a4b585cf0c521.zip
Implement file locking on Windows.
Diffstat (limited to 'crawl-ref/source/syscalls.cc')
-rw-r--r--crawl-ref/source/syscalls.cc31
1 files changed, 29 insertions, 2 deletions
diff --git a/crawl-ref/source/syscalls.cc b/crawl-ref/source/syscalls.cc
index b507511fb3..cea6cbb9a6 100644
--- a/crawl-ref/source/syscalls.cc
+++ b/crawl-ref/source/syscalls.cc
@@ -14,12 +14,24 @@
#undef rename
#include <io.h>
+#else
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#endif
-bool lock_file(int fd, bool write)
+bool lock_file(int fd, bool write, bool wait)
{
#ifdef TARGET_OS_WINDOWS
- return !!LockFile((HANDLE)_get_osfhandle(fd), 0, 0, -1, -1);
+ OVERLAPPED pos;
+ pos.hEvent = 0;
+ pos.Offset = 0;
+ pos.OffsetHigh = 0;
+ return !!LockFileEx((HANDLE)_get_osfhandle(fd),
+ (write ? LOCKFILE_EXCLUSIVE_LOCK : 0) |
+ (wait ? 0 : LOCKFILE_FAIL_IMMEDIATELY),
+ 0, -1, -1, &pos);
#else
struct flock fl;
fl.l_type = write ? F_WRLCK : F_RDLCK;
@@ -27,6 +39,21 @@ bool lock_file(int fd, bool write)
fl.l_start = 0;
fl.l_len = 0;
+ return !fcntl(fd, wait ? F_SETLKW : F_SETLK, &fl);
+#endif
+}
+
+bool unlock_file(int fd)
+{
+#ifdef TARGET_OS_WINDOWS
+ return !!UnlockFile((HANDLE)_get_osfhandle(fd), 0, 0, -1, -1);
+#else
+ struct flock fl;
+ fl.l_type = F_UNLCK;
+ fl.l_whence = SEEK_SET;
+ fl.l_start = 0;
+ fl.l_len = 0;
+
return !fcntl(fd, F_SETLK, &fl);
#endif
}