From e79a7d4e07632ae3a428f4363effffbbb52c7a1d Mon Sep 17 00:00:00 2001 From: H.G. Muller Date: Mon, 2 May 2011 13:38:14 +0200 Subject: [PATCH] Implement flock in WinBoard This system function seems unknown in a MinGW compile, so it is implemented with the aid of file byte-range locking, using a lock on the first 1024 bytes of the file as a semaphore. --- backend.c | 3 +++ winboard/winboard.c | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 0 deletions(-) diff --git a/backend.c b/backend.c index d68df79..f6e75c4 100644 --- a/backend.c +++ b/backend.c @@ -57,6 +57,9 @@ #define DoSleep( n ) if( (n) != 0 ) Sleep( (n) ); +int flock(int f, int code); +#define LOCK_EX 2 + #else #define DoSleep( n ) if( (n) >= 0) sleep(n) diff --git a/winboard/winboard.c b/winboard/winboard.c index 3938cf2..4d823eb 100644 --- a/winboard/winboard.c +++ b/winboard/winboard.c @@ -73,6 +73,7 @@ #include #include #include +#include #if __GNUC__ #include @@ -9783,3 +9784,19 @@ SettingsPopUp(ChessProgramState *cps) { // [HGM] wrapper needed because handles must not be passed through back-end EngineOptionsPopup(savedHwnd, cps); } + +int flock(int fid, int code) +{ + HANDLE hFile = (HANDLE) _get_osfhandle(fid); + OVERLAPPED ov; + ov.hEvent = NULL; + ov.Offset = 0; + ov.OffsetHigh = 0; + switch(code) { + case 1: LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, 1024, 0, &ov); break; // LOCK_SH + case 2: LockFileEx(hFile, LOCKFILE_EXCLUSIVE_LOCK, 0, 1024, 0, &ov); break; // LOCK_EX + case 3: UnlockFileEx(hFile, 0, 1024, 0, &ov); break; // LOCK_UN + default: return -1; + } + return 0; +} -- 1.7.0.4