From: H.G. Muller Date: Mon, 2 May 2011 11:38:14 +0000 (+0200) Subject: Implement flock in WinBoard X-Git-Url: http://winboard.nl/cgi-bin?a=commitdiff_plain;h=e79a7d4e07632ae3a428f4363effffbbb52c7a1d;p=xboard.git 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. --- 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; +}