1
/* Copyright (C) 2000-2003 MySQL AB
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
16
#include <my_global.h>
18
#include <mysys_err.h>
26
-1 An error has occured and 'my_errno' is set
27
to indicate the actual error code.
30
int my_lock(File fd, int locktype, my_off_t start, my_off_t length,
34
long alarm_pos=0,alarm_end_pos=MY_HOW_OFTEN_TO_WRITE-1;
36
if (my_disable_locking)
42
lock.l_type= (short) locktype;
43
lock.l_whence= SEEK_SET;
44
lock.l_start= (off_t) start;
45
lock.l_len= (off_t) length;
47
if (MyFlags & MY_DONT_WAIT)
49
if (fcntl(fd,F_SETLK,&lock) != -1) /* Check if we can lock */
50
return(0); /* Ok, file locked */
51
while ((value=fcntl(fd,F_SETLKW,&lock)) && ! (alarm_pos++ >= alarm_end_pos) &&
53
{ /* Setup again so we don`t miss it */
54
alarm_end_pos+=MY_HOW_OFTEN_TO_WRITE;
61
else if (fcntl(fd,F_SETLKW,&lock) != -1) /* Wait until a lock */
65
/* We got an error. We don't want EACCES errors */
66
my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1;
69
if (locktype == F_UNLCK)
70
my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
72
my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);