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))
57 if (GetLastError() == ERROR_NOT_LOCKED)
64 else if (locktype == F_RDLCK)
69 dwFlags= LOCKFILE_EXCLUSIVE_LOCK;
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;
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());
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)
155 if (MyFlags & MY_DONT_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_DONT_WAIT)
175 if (fcntl(fd,F_SETLK,&lock) != -1)
177 DBUG_PRINT(
"info",(
"Was locked, trying with alarm"));
179 while ((value=fcntl(fd,F_SETLKW,&lock)) && ! ALARM_TEST &&
190 else if (fcntl(fd,F_SETLKW,&lock) != -1)
194 if (MyFlags & MY_SEEK_NOT_DONE)
196 if (my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE))
206 if (lockf(fd,locktype,length) != -1)
212 my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1;
214 if (MyFlags & MY_WME)
216 char errbuf[MYSYS_STRERROR_SIZE];
217 if (locktype == F_UNLCK)
218 my_error(EE_CANTUNLOCK, MYF(ME_BELL+ME_WAITTANG),
219 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
221 my_error(EE_CANTLOCK, MYF(ME_BELL+ME_WAITTANG),
222 my_errno, my_strerror(errbuf,
sizeof(errbuf), my_errno));
224 DBUG_PRINT(
"error",(
"my_errno: %d (%d)",my_errno,errno));