~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to storage/myisam/my_lock.c

Removed/replaced DBUG symbols and TRUE/FALSE

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2003 MySQL AB
 
2
 
 
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.
 
6
 
 
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.
 
11
 
 
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 */
 
15
 
 
16
#include <my_global.h>
 
17
#include <my_sys.h>
 
18
#include <mysys_err.h>
 
19
 
 
20
 
 
21
/*
 
22
  Lock a part of a file
 
23
 
 
24
  RETURN VALUE
 
25
    0   Success
 
26
    -1  An error has occured and 'my_errno' is set
 
27
        to indicate the actual error code.
 
28
*/
 
29
 
 
30
int my_lock(File fd, int locktype, my_off_t start, my_off_t length,
 
31
            myf MyFlags)
 
32
{
 
33
  int value;
 
34
  long alarm_pos=0,alarm_end_pos=MY_HOW_OFTEN_TO_WRITE-1;
 
35
 
 
36
  DBUG_ENTER("my_lock");
 
37
  DBUG_PRINT("my",("Fd: %d  Op: %d  start: %ld  Length: %ld  MyFlags: %d",
 
38
                   fd,locktype,(long) start,(long) length,MyFlags));
 
39
  if (my_disable_locking)
 
40
    DBUG_RETURN(0);
 
41
 
 
42
  {
 
43
    struct flock lock;
 
44
 
 
45
    lock.l_type=   (short) locktype;
 
46
    lock.l_whence= SEEK_SET;
 
47
    lock.l_start=  (off_t) start;
 
48
    lock.l_len=    (off_t) length;
 
49
 
 
50
    if (MyFlags & MY_DONT_WAIT)
 
51
    {
 
52
      if (fcntl(fd,F_SETLK,&lock) != -1)        /* Check if we can lock */
 
53
        DBUG_RETURN(0);                 /* Ok, file locked */
 
54
      DBUG_PRINT("info",("Was locked, trying with alarm"));
 
55
      while ((value=fcntl(fd,F_SETLKW,&lock)) && !  (alarm_pos++ >= alarm_end_pos) &&
 
56
             errno == EINTR)
 
57
      {                 /* Setup again so we don`t miss it */
 
58
        alarm_end_pos+=MY_HOW_OFTEN_TO_WRITE;
 
59
      }
 
60
      if (value != -1)
 
61
        DBUG_RETURN(0);
 
62
      if (errno == EINTR)
 
63
        errno=EAGAIN;
 
64
    }
 
65
    else if (fcntl(fd,F_SETLKW,&lock) != -1) /* Wait until a lock */
 
66
      DBUG_RETURN(0);
 
67
  }
 
68
 
 
69
        /* We got an error. We don't want EACCES errors */
 
70
  my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1;
 
71
  if (MyFlags & MY_WME)
 
72
  {
 
73
    if (locktype == F_UNLCK)
 
74
      my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
 
75
    else
 
76
      my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
 
77
  }
 
78
  DBUG_PRINT("error",("my_errno: %d (%d)",my_errno,errno));
 
79
  DBUG_RETURN(-1);
 
80
} /* my_lock */