Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_flock.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_flock.c,v 1.6 2005/06/16 20:23:28 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #include "db_int.h"
00013 
00014 /*
00015  * __os_fdlock --
00016  *      Acquire/release a lock on a byte in a file.
00017  *
00018  * PUBLIC: int __os_fdlock __P((DB_ENV *, DB_FH *, off_t, int, int));
00019  */
00020 int
00021 __os_fdlock(dbenv, fhp, offset, acquire, nowait)
00022         DB_ENV *dbenv;
00023         DB_FH *fhp;
00024         int acquire, nowait;
00025         off_t offset;
00026 {
00027         int ret;
00028         DWORD low, high;
00029         OVERLAPPED over;
00030 
00031         DB_ASSERT(F_ISSET(fhp, DB_FH_OPENED) &&
00032             fhp->handle != INVALID_HANDLE_VALUE);
00033 
00034         /*
00035          * Windows file locking interferes with read/write operations, so we
00036          * map the ranges to an area past the end of the file.
00037          */
00038         DB_ASSERT(offset < (u_int64_t)INT64_MAX);
00039         offset = UINT64_MAX - offset;
00040         low = (DWORD)offset;
00041         high = (DWORD)(offset >> 32);
00042 
00043         if (acquire) {
00044                 if (nowait)
00045                         RETRY_CHK_EINTR_ONLY(
00046                             !LockFile(fhp->handle, low, high, 1, 0), ret);
00047                 else if (__os_is_winnt()) {
00048                         memset(&over, 0, sizeof over);
00049                         over.Offset = low;
00050                         over.OffsetHigh = high;
00051                         RETRY_CHK_EINTR_ONLY(
00052                             !LockFileEx(fhp->handle, LOCKFILE_EXCLUSIVE_LOCK,
00053                             0, 1, 0, &over),
00054                             ret);
00055                 } else {
00056                         /* Windows 9x/ME doesn't support a blocking call. */
00057                         for (;;) {
00058                                 RETRY_CHK_EINTR_ONLY(
00059                                     !LockFile(fhp->handle, low, high, 1, 0),
00060                                     ret);
00061                                 if (ret != EAGAIN)
00062                                         break;
00063                                 __os_sleep(dbenv, 1, 0);
00064                         }
00065                 }
00066         } else
00067                 RETRY_CHK_EINTR_ONLY(
00068                     !UnlockFile(fhp->handle, low, high, 1, 0), ret);
00069 
00070         return (ret);
00071 }

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2