summaryrefslogtreecommitdiff
path: root/src/os_win/os_flock.c
blob: d551480ba4d14d5e94434078307e87cb41400bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*-
 * Copyright (c) 2014-2015 MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

#include "wt_internal.h"

/*
 * __wt_bytelock --
 *	Lock/unlock a byte in a file.
 */
int
__wt_bytelock(WT_FH *fhp, wt_off_t byte, int lock)
{
	WT_DECL_RET;

	/*
	 * WiredTiger requires this function be able to acquire locks past
	 * the end of file.
	 *
	 * Note we're using fcntl(2) locking: all fcntl locks associated with a
	 * file for a given process are removed when any file descriptor for the
	 * file is closed by the process, even if a lock was never requested for
	 * that file descriptor.
	 *
	 * http://msdn.microsoft.com/
	 *    en-us/library/windows/desktop/aa365202%28v=vs.85%29.aspx
	 *
	 * You can lock bytes that are beyond the end of the current file.
	 * This is useful to coordinate adding records to the end of a file.
	 */
	if (lock) {
		ret = LockFile(fhp->filehandle, UINT32_MAX & byte,
		    UINT32_MAX & (byte >> 32), 1, 0);
	} else {
		ret = UnlockFile(fhp->filehandle, UINT32_MAX & byte,
		    UINT32_MAX & (byte >> 32), 1, 0);
	}

	if (ret == FALSE)
		WT_RET_MSG(NULL, __wt_errno(), "%s: LockFile", fhp->name);

	return (0);
}