1
/* Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include "mysys_priv.h"
17
#include "mysys_err.h"
19
#undef MY_HOW_OFTEN_TO_ALARM
20
#define MY_HOW_OFTEN_TO_ALARM ((int) my_time_to_wait_for_lock)
27
#define WIN_LOCK_INFINITE -1
28
#define WIN_LOCK_SLEEP_MILLIS 100
30
static int win_lock(File fd, int locktype, my_off_t start, my_off_t length,
33
LARGE_INTEGER liOffset,liLength;
36
HANDLE hFile= (HANDLE)my_get_osfhandle(fd);
39
int timeout_millis= timeout_sec * 1000;
41
DBUG_ENTER("win_lock");
43
liOffset.QuadPart= start;
44
liLength.QuadPart= length;
46
ov.Offset= liOffset.LowPart;
47
ov.OffsetHigh= liOffset.HighPart;
49
if (locktype == F_UNLCK)
51
if (UnlockFileEx(hFile, 0, liLength.LowPart, liLength.HighPart, &ov))
54
For compatibility with fcntl implementation, ignore error,
55
if region was not locked
57
if (GetLastError() == ERROR_NOT_LOCKED)
64
else if (locktype == F_RDLCK)
65
/* read lock is mapped to a shared lock. */
68
/* write lock is mapped to an exclusive lock. */
69
dwFlags= LOCKFILE_EXCLUSIVE_LOCK;
72
Drop old lock first to avoid double locking.
73
During analyze of Bug#38133 (Myisamlog test fails on Windows)
74
I met the situation that the program myisamlog locked the file
75
exclusively, then additionally shared, then did one unlock, and
76
then blocked on an attempt to lock it exclusively again.
77
Unlocking before every lock fixed the problem.
78
Note that this introduces a race condition. When the application
79
wants to convert an exclusive lock into a shared one, it will now
80
first unlock the file and then lock it shared. A waiting exclusive
81
lock could step in here. For reasons described in Bug#38133 and
82
Bug#41124 (Server hangs on Windows with --external-locking after
83
INSERT...SELECT) and in the review thread at
84
http://lists.mysql.com/commits/60721 it seems to be the better
85
option than not to unlock here.
86
If one day someone notices a way how to do file lock type changes
87
on Windows without unlocking before taking the new lock, please
88
change this code accordingly to fix the race condition.
90
if (!UnlockFileEx(hFile, 0, liLength.LowPart, liLength.HighPart, &ov) &&
91
(GetLastError() != ERROR_NOT_LOCKED))
94
if (timeout_sec == WIN_LOCK_INFINITE)
96
if (LockFileEx(hFile, dwFlags, 0, liLength.LowPart, liLength.HighPart, &ov))
101
dwFlags|= LOCKFILE_FAIL_IMMEDIATELY;
102
timeout_millis= timeout_sec * 1000;
103
/* Try lock in a loop, until the lock is acquired or timeout happens */
104
for(i= 0; ;i+= WIN_LOCK_SLEEP_MILLIS)
106
if (LockFileEx(hFile, dwFlags, 0, liLength.LowPart, liLength.HighPart, &ov))
109
if (GetLastError() != ERROR_LOCK_VIOLATION)
112
if (i >= timeout_millis)
114
Sleep(WIN_LOCK_SLEEP_MILLIS);
122
my_osmaperr(GetLastError());
130
Lock a part of a file
134
-1 An error has occured and 'my_errno' is set
135
to indicate the actual error code.
138
int my_lock(File fd, int locktype, my_off_t start, my_off_t length,
146
DBUG_ENTER("my_lock");
147
DBUG_PRINT("my",("fd: %d Op: %d start: %ld Length: %ld MyFlags: %d",
148
fd,locktype,(long) start,(long) length,MyFlags));
149
if (my_disable_locking && ! (MyFlags & MY_FORCE_LOCK))
155
if (MyFlags & MY_NO_WAIT)
158
timeout_sec= WIN_LOCK_INFINITE;
160
if (win_lock(fd, locktype, start, length, timeout_sec) == 0)
164
#if defined(HAVE_FCNTL)
168
lock.l_type= (short) locktype;
169
lock.l_whence= SEEK_SET;
170
lock.l_start= (off_t) start;
171
lock.l_len= (off_t) length;
173
if (MyFlags & (MY_NO_WAIT | MY_SHORT_WAIT))
175
if (fcntl(fd,F_SETLK,&lock) != -1) /* Check if we can lock */
176
DBUG_RETURN(0); /* Ok, file locked */
177
if (MyFlags & MY_NO_WAIT)
179
my_errno= (errno == EACCES) ? EAGAIN : errno ? errno : -1;
183
DBUG_PRINT("info",("Was locked, trying with alarm"));
185
while ((value=fcntl(fd,F_SETLKW,&lock)) && ! ALARM_TEST &&
187
{ /* Setup again so we don`t miss it */
196
else if (fcntl(fd,F_SETLKW,&lock) != -1) /* Wait until a lock */
200
if (MyFlags & MY_SEEK_NOT_DONE)
202
if (my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE))
206
If an error has occured in my_seek then we will already
207
have an error code in my_errno; Just return error code.
212
if (lockf(fd,locktype,length) != -1)
214
#endif /* HAVE_FCNTL */
217
/* We got an error. We don't want EACCES errors */
218
my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1;
220
if (MyFlags & MY_WME)
222
if (locktype == F_UNLCK)
223
my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
225
my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
227
DBUG_PRINT("error",("my_errno: %d (%d)",my_errno,errno));