my_lock.c
上传用户:tsgydb
上传日期:2007-04-14
资源大小:10674k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
  2.    
  3.    This library is free software; you can redistribute it and/or
  4.    modify it under the terms of the GNU Library General Public
  5.    License as published by the Free Software Foundation; either
  6.    version 2 of the License, or (at your option) any later version.
  7.    
  8.    This library is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  11.    Library General Public License for more details.
  12.    
  13.    You should have received a copy of the GNU Library General Public
  14.    License along with this library; if not, write to the Free
  15.    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  16.    MA 02111-1307, USA */
  17. #include "mysys_priv.h"
  18. #include "mysys_err.h"
  19. #include <errno.h>
  20. #undef MY_HOW_OFTEN_TO_ALARM
  21. #define MY_HOW_OFTEN_TO_ALARM ((int) my_time_to_wait_for_lock)
  22. #ifdef NO_ALARM_LOOP
  23. #undef NO_ALARM_LOOP
  24. #endif
  25. #include <my_alarm.h>
  26. #ifdef __WIN__
  27. #include <sys/locking.h>
  28. #endif
  29. #ifdef __EMX__
  30. #define INCL_BASE
  31. #define INCL_NOPMAPI
  32. #include <os2emx.h>
  33. #endif
  34. /* Lock a part of a file */
  35. int my_lock(File fd, int locktype, my_off_t start, my_off_t length,
  36.     myf MyFlags)
  37. {
  38. #ifdef __EMX__
  39.   FILELOCK LockArea = {0,0}, UnlockArea = {0,0};
  40.   APIRET rc = 0;
  41.   fpos_t oldpos;
  42.   int lockflags = 0;
  43. #endif
  44. #ifdef HAVE_FCNTL
  45.   int value;
  46.   ALARM_VARIABLES;
  47. #endif
  48.   DBUG_ENTER("my_lock");
  49.   DBUG_PRINT("my",("Fd: %d  Op: %d  start: %ld  Length: %ld  MyFlags: %d",
  50.    fd,locktype,(long) start,(long) length,MyFlags));
  51. #ifdef VMS
  52.   DBUG_RETURN(0);
  53. #else
  54.   if (my_disable_locking)
  55.     DBUG_RETURN(0);
  56. #if defined(__EMX__)
  57.   if (locktype == F_UNLCK) {
  58.     UnlockArea.lOffset = start;
  59.     if (length)
  60.       UnlockArea.lRange = length;
  61.     else
  62.       UnlockArea.lRange = 0x7FFFFFFFL;
  63.   } else
  64.   if (locktype == F_RDLCK || locktype == F_WRLCK) {
  65.     if (locktype == F_RDLCK) lockflags |= 1;
  66.     LockArea.lOffset = start;
  67.     if (length)
  68.       LockArea.lRange = length;
  69.     else
  70.       LockArea.lRange = 0x7FFFFFFFL;
  71.   } else {
  72.     my_errno = EINVAL;
  73.     DBUG_RETURN(-1);
  74.   }
  75.   if (!LockArea.lRange && !UnlockArea.lRange)
  76.     DBUG_RETURN(0);
  77.   if (MyFlags & MY_DONT_WAIT) {
  78.     if (!(rc = DosSetFileLocks(fd,&UnlockArea,&LockArea,0,lockflags)))
  79.       DBUG_RETURN(0); /* Lock was OK */
  80.     if (rc == 175 && locktype == F_RDLCK) {
  81.       lockflags &= ~1;
  82.       rc = DosSetFileLocks(fd,&UnlockArea,&LockArea,0,lockflags);
  83.     }
  84.     if (rc == 33) {  /* Lock Violation */
  85.       DBUG_PRINT("info",("Was locked, trying with timeout"));
  86.       rc = DosSetFileLocks(fd,&UnlockArea,&LockArea,MY_HOW_OFTEN_TO_ALARM * 1000,lockflags);
  87.     }
  88.     if (!rc) DBUG_RETURN(0);
  89.     if (rc == 33) errno = EAGAIN;
  90.     else {
  91.       errno = EINVAL;
  92.       printf("Error: DosSetFileLocks() == %dn",rc);
  93.     }
  94.   } else {
  95.     while (rc = DosSetFileLocks(fd,&UnlockArea,&LockArea,
  96. MY_HOW_OFTEN_TO_ALARM * 1000,lockflags) && (rc == 33 || rc == 175)) {
  97.       printf(".");
  98.       if (rc == 175) lockflags &= ~1;
  99.     }
  100.     if (!rc) DBUG_RETURN(0);
  101.     errno = EINVAL;
  102.     printf("Error: DosSetFileLocks() == %dn",rc);
  103.   }
  104. #elif defined(HAVE_LOCKING)
  105.   /* Windows */
  106.   {
  107.     my_bool error;
  108.     pthread_mutex_lock(&my_file_info[fd].mutex);
  109.     if (MyFlags & MY_SEEK_NOT_DONE)
  110.       VOID(my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE)));
  111.     error= locking(fd,locktype,(ulong) length) && errno != EINVAL;
  112.     pthread_mutex_unlock(&my_file_info[fd].mutex);
  113.     if (!error)
  114.       DBUG_RETURN(0);
  115.   }
  116. #else
  117. #if defined(HAVE_FCNTL)
  118.   {
  119.     struct flock lock;
  120.     lock.l_type= (short) locktype;
  121.     lock.l_whence=0L;
  122.     lock.l_start=(long) start;
  123.     lock.l_len=(long) length;
  124.     if (MyFlags & MY_DONT_WAIT)
  125.     {
  126.       if (fcntl(fd,F_SETLK,&lock) != -1) /* Check if we can lock */
  127. DBUG_RETURN(0); /* Ok, file locked */
  128.       DBUG_PRINT("info",("Was locked, trying with alarm"));
  129.       ALARM_INIT;
  130.       while ((value=fcntl(fd,F_SETLKW,&lock)) && ! ALARM_TEST &&
  131.      errno == EINTR)
  132.       { /* Setup again so we don`t miss it */
  133. ALARM_REINIT;
  134.       }
  135.       ALARM_END;
  136.       if (value != -1)
  137. DBUG_RETURN(0);
  138.       if (errno == EINTR)
  139. errno=EAGAIN;
  140.     }
  141.     else if (fcntl(fd,F_SETLKW,&lock) != -1) /* Wait until a lock */
  142.       DBUG_RETURN(0);
  143.   }
  144. #else
  145.   if (MyFlags & MY_SEEK_NOT_DONE)
  146.     VOID(my_seek(fd,start,MY_SEEK_SET,MYF(MyFlags & ~MY_SEEK_NOT_DONE)));
  147.   if (lockf(fd,locktype,length) != -1)
  148.     DBUG_RETURN(0);
  149. #endif /* HAVE_FCNTL */
  150. #endif /* HAVE_LOCKING */
  151. /* We got an error. We don't want EACCES errors */
  152.   my_errno=(errno == EACCES) ? EAGAIN : errno ? errno : -1;
  153.   if (MyFlags & MY_WME)
  154.   {
  155.     if (locktype == F_UNLCK)
  156.       my_error(EE_CANTUNLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
  157.     else
  158.       my_error(EE_CANTLOCK,MYF(ME_BELL+ME_WAITTANG),my_errno);
  159.   }
  160.   DBUG_PRINT("error",("my_errno: %d (%d)",my_errno,errno));
  161.   DBUG_RETURN(-1);
  162. #endif /* ! VMS */
  163. } /* my_lock */