~drizzle-trunk/drizzle/development

1 by brian
clean slate
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
#if defined(__NETWARE__)
17
#include "../mysys/my_lock.c"
18
#else
19
20
#undef MAP_TO_USE_RAID			/* Avoid RAID mappings */
21
#include <my_global.h>
22
#include <my_sys.h>
23
#include <mysys_err.h>
24
#include <my_pthread.h>
25
#include <thr_alarm.h>
26
#include <errno.h>
27
28
	/* Lock a part of a file */
29
30
int my_lock(File fd,int locktype,my_off_t start,my_off_t length,myf MyFlags)
31
{
32
  thr_alarm_t alarmed;
33
  ALARM	alarm_buff;
34
  uint wait_for_alarm;
35
  struct flock m_lock;
36
  DBUG_ENTER("my_lock");
37
  DBUG_PRINT("my",("Fd: %d  Op: %d  start: %ld  Length: %ld  MyFlags: %d",
38
		   fd,locktype,(ulong) start,(ulong) length,MyFlags));
39
  if (my_disable_locking)
40
    DBUG_RETURN(0); /* purecov: inspected */
41
  m_lock.l_type=(short) locktype;
42
  m_lock.l_whence=0L;
43
  m_lock.l_start=(long) start;
44
  m_lock.l_len=(long) length;
45
  wait_for_alarm=(MyFlags & MY_DONT_WAIT ? MY_HOW_OFTEN_TO_ALARM :
46
		  (uint) 12*60*60);
47
  if (fcntl(fd,F_SETLK,&m_lock) != -1)	/* Check if we can lock */
48
    DBUG_RETURN(0);			/* Ok, file locked */
49
  DBUG_PRINT("info",("Was locked, trying with alarm"));
50
  if (!thr_alarm(&alarmed,wait_for_alarm,&alarm_buff))
51
  {
52
    int value;
53
    while ((value=fcntl(fd,F_SETLKW,&m_lock)) && !thr_got_alarm(&alarmed) &&
54
	   errno == EINTR) ;
55
    thr_end_alarm(&alarmed);
56
    if (value != -1)
57
      DBUG_RETURN(0);
58
  }
59
  else
60
  {
61
    errno=EINTR;
62
  }
63
  if (errno == EINTR || errno == EACCES)
64
    my_errno=EAGAIN;			/* Easier to check for this */
65
  else
66
    my_errno=errno;
67
68
  if (MyFlags & MY_WME)
69
  {
70
    if (locktype == F_UNLCK)
71
      my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),errno);
72
    else
73
      my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),errno);
74
  }
75
  DBUG_PRINT("error",("errno: %d",errno));
76
  DBUG_RETURN(-1);
77
}
78
#endif