ReactOS 0.4.16-dev-889-g9563c07
locking.cpp
Go to the documentation of this file.
1//
2// locking.cpp
3//
4// Copyright (c) Microsoft Corporation. All rights reserved.
5//
6// Defines _locking(), which locks and unlocks regions of a file.
7//
9#include <sys/locking.h>
10
11
12
13static int __cdecl locking_nolock(int const fh, int const locking_mode, long const number_of_bytes) throw()
14{
15 __int64 const lock_offset = _lseeki64_nolock(fh, 0L, SEEK_CUR);
16 if (lock_offset == -1)
17 return -1;
18
19 OVERLAPPED overlapped = { 0 };
20 overlapped.Offset = static_cast<DWORD>(lock_offset);
21 overlapped.OffsetHigh = static_cast<DWORD>((lock_offset >> 32) & 0xffffffff);
22
23 // Set the retry count, based on the mode:
24 bool const allow_retry = locking_mode == _LK_LOCK || locking_mode == _LK_RLCK;
25 int const retry_count = allow_retry ? 10 : 1;
26
27 // Ask the OS to lock the file either until the request succeeds or the
28 // retry count is reached, whichever comes first. Note that the only error
29 // possible is a locking violation, since an invalid handle would have
30 // already failed above.
31 bool succeeded = false;
32 for (int i = 0; i != retry_count; ++i)
33 {
34 if (locking_mode == _LK_UNLCK)
35 {
36 succeeded = UnlockFileEx(
37 reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
38 0,
39 number_of_bytes,
40 0,
41 &overlapped) == TRUE;
42 }
43 else
44 {
45 // Ensure exclusive lock access, and return immediately if lock
46 // acquisition fails:
47 succeeded = LockFileEx(
48 reinterpret_cast<HANDLE>(_get_osfhandle(fh)),
50 0,
51 number_of_bytes,
52 0,
53 &overlapped) == TRUE;
54 }
55
56 if (succeeded)
57 {
58 break;
59 }
60
61 // Doesnt sleep on the last try
62 if (i != retry_count - 1)
63 {
64 Sleep(1000);
65 }
66 }
67
68 // If an OS error occurred (e.g., if the file was already locked), return
69 // EDEADLOCK if this was ablocking call; otherwise map the error noramlly:
70 if (!succeeded)
71 {
73 if (locking_mode == _LK_LOCK || locking_mode == _LK_RLCK)
74 {
76 }
77
78 return -1;
79 }
80
81 return 0;
82}
83
84
85
86// Locks or unlocks the requested number of bytes in the specified file.
87//
88// Note that this function acquires the lock for the specified file and holds
89// this lock for the entire duration of the call, even during the one second
90// delays between calls into the operating system. This is to prevent other
91// threads from changing the file during the call.
92//
93// Returns 0 on success; returns -1 and sets errno on failure.
94extern "C" int __cdecl _locking(int const fh, int const locking_mode, long const number_of_bytes)
95{
97 _VALIDATE_CLEAR_OSSERR_RETURN(fh >= 0 && (unsigned)fh < (unsigned)_nhandle, EBADF, -1);
99 _VALIDATE_CLEAR_OSSERR_RETURN(number_of_bytes >= 0, EINVAL, -1);
100
102 int result = -1;
103 __try
104 {
105 if ((_osfile(fh) & FOPEN) == 0)
106 {
107 errno = EBADF;
108 _doserrno = 0;
109 _ASSERTE(("Invalid file descriptor. File possibly closed by a different thread",0));
110 __leave;
111 }
112
113 result = locking_nolock(fh, locking_mode, number_of_bytes);
114 }
116 {
118 }
120 return result;
121}
#define EINVAL
Definition: acclib.h:90
#define EBADF
Definition: acclib.h:82
#define __cdecl
Definition: accygwin.h:79
#define __int64
Definition: basetyps.h:16
#define _CHECK_FH_CLEAR_OSSERR_RETURN(handle, errorcode, retexpr)
void __cdecl __acrt_errno_map_os_error(unsigned long)
Definition: errno.cpp:91
int _nhandle
Definition: ioinit.cpp:34
_Check_return_opt_ __int64 __cdecl _lseeki64_nolock(_In_ int _FileHandle, _In_ __int64 _Offset, _In_ int _Origin)
void __cdecl __acrt_lowio_lock_fh(_In_ int _FileHandle)
void __cdecl __acrt_lowio_unlock_fh(_In_ int _FileHandle)
#define _ASSERTE(expr)
Definition: crtdbg.h:114
#define _LK_LOCK
Definition: locking.h:16
#define _LK_UNLCK
Definition: locking.h:15
#define _LK_RLCK
Definition: locking.h:18
#define TRUE
Definition: types.h:120
BOOL WINAPI UnlockFileEx(IN HANDLE hFile, IN DWORD dwReserved, IN DWORD nNumberOfBytesToUnLockLow, IN DWORD nNumberOfBytesToUnLockHigh, IN LPOVERLAPPED lpOverlapped)
Definition: lock.c:183
BOOL WINAPI LockFileEx(IN HANDLE hFile, IN DWORD dwFlags, IN DWORD dwReserved, IN DWORD nNumberOfBytesToLockLow, IN DWORD nNumberOfBytesToLockHigh, IN LPOVERLAPPED lpOverlapped)
Definition: lock.c:82
#define EDEADLOCK
Definition: errno.h:60
unsigned long DWORD
Definition: ntddk_ex.h:95
#define FOPEN
GLuint64EXT * result
Definition: glext.h:11304
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define _doserrno
Definition: stdlib.h:131
#define _VALIDATE_CLEAR_OSSERR_RETURN(expr, errorcode, retexpr)
int __cdecl _locking(int const fh, int const locking_mode, long const number_of_bytes)
Definition: locking.cpp:94
static int __cdecl locking_nolock(int const fh, int const locking_mode, long const number_of_bytes)
Definition: locking.cpp:13
#define SEEK_CUR
Definition: util.h:63
#define L(x)
Definition: ntvdm.h:50
#define __try
Definition: pseh2_64.h:188
#define __leave
Definition: pseh2_64.h:192
#define __endtry
Definition: pseh2_64.h:191
#define __finally
Definition: pseh2_64.h:190
#define errno
Definition: errno.h:18
_CRTIMP intptr_t __cdecl _get_osfhandle(_In_ int _FileHandle)
#define _osfile(i)
Definition: internal.h:72
namespace GUID const ADDRINFOEXW ADDRINFOEXW struct timeval OVERLAPPED * overlapped
Definition: sock.c:81
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790
#define LOCKFILE_FAIL_IMMEDIATELY
Definition: winbase.h:411
DWORD WINAPI GetLastError(void)
Definition: except.c:1042
#define LOCKFILE_EXCLUSIVE_LOCK
Definition: winbase.h:412