~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to sql/my_lock.c

Put errmsg.c in sql-common since it can be built only once and used twice.
Put client.c and net_serv.c in libmysql so that we can only have one
link_sources section. 
Got rid of just about all copying and other weirdness, other than some stuff
in client and client.c/net_serv.c, which need to be reworked.

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