backends/flint/flint_lock.h

Go to the documentation of this file.
00001 /* flint_lock.h: database locking for flint backend.
00002  *
00003  * Copyright (C) 2005,2006,2007 Olly Betts
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License as
00007  * published by the Free Software Foundation; either version 2 of the
00008  * License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
00018  * USA
00019  */
00020 
00021 #ifndef XAPIAN_INCLUDED_FLINT_LOCK_H
00022 #define XAPIAN_INCLUDED_FLINT_LOCK_H
00023 
00024 #include <string>
00025 
00026 #if defined __CYGWIN__ || defined __WIN32__
00027 # include "safewindows.h"
00028 #elif defined __EMX__
00029 # define INCL_DOS
00030 # define INCL_DOSERRORS
00031 # include <os2.h>
00032 #else
00033 # include <sys/types.h>
00034 #endif
00035 
00036 class FlintLock {
00037     std::string filename;
00038 #if defined __CYGWIN__ || defined __WIN32__
00039     HANDLE hFile;
00040 #elif defined __EMX__
00041     HFILE hFile;
00042 #else
00043     int fd;
00044     pid_t pid;
00045 #endif
00046 
00047   public:
00048     typedef enum {
00049         SUCCESS, // We got the lock!
00050         INUSE, // Already locked by someone else.
00051         UNSUPPORTED, // Locking probably not supported (e.g. NFS without lockd).
00052         UNKNOWN // The attempt failed for some unspecified reason.
00053     } reason;
00054 #if defined __CYGWIN__ || defined __WIN32__
00055     FlintLock(const std::string &filename_)
00056         : filename(filename_), hFile(INVALID_HANDLE_VALUE) { }
00057     operator bool() { return hFile != INVALID_HANDLE_VALUE; }
00058 #elif defined __EMX__
00059     FlintLock(const std::string &filename_)
00060         : filename(filename_), hFile(NULLHANDLE) { }
00061     operator bool() { return hFile != NULLHANDLE; }
00062 #else
00063     FlintLock(const std::string &filename_) : filename(filename_), fd(-1) { }
00064     operator bool() { return fd != -1; }
00065 #endif
00066     // Release any lock held when we're destroyed.
00067     ~FlintLock() { release(); }
00068 
00069     reason lock(bool exclusive);
00070     void release();
00071 };
00072 
00073 #endif // XAPIAN_INCLUDED_FLINT_LOCK_H

Documentation for Xapian (version 1.0.10).
Generated on 24 Dec 2008 by Doxygen 1.5.2.