2 * @brief Flint-compatible database locking.
4 /* Copyright (C) 2005,2006,2007,2008,2009,2012,2014,2016 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 #ifndef XAPIAN_INCLUDED_FLINT_LOCK_H
23 #define XAPIAN_INCLUDED_FLINT_LOCK_H
27 #if defined __CYGWIN__ || defined __WIN32__
28 # include "safewindows.h"
30 # include <sys/types.h>
37 #if defined __CYGWIN__ || defined __WIN32__
39 #elif defined FLINTLOCK_USE_FLOCK
48 SUCCESS
, // We got the lock!
49 INUSE
, // Already locked by someone else.
50 UNSUPPORTED
, // Locking probably not supported (e.g. NFS without lockd).
51 FDLIMIT
, // Process hit its file descriptor limit.
52 UNKNOWN
// The attempt failed for some unspecified reason.
54 #if defined __CYGWIN__ || defined __WIN32__
55 FlintLock(const std::string
&filename_
)
56 : filename(filename_
), hFile(INVALID_HANDLE_VALUE
) {
57 // Keep the same lockfile name as flint since the locking is
58 // compatible and this avoids the possibility of creating two databases
59 // in the same directory using different backends.
60 filename
+= "/flintlock";
62 operator bool() const { return hFile
!= INVALID_HANDLE_VALUE
; }
63 #elif defined FLINTLOCK_USE_FLOCK
64 FlintLock(const std::string
&filename_
) : filename(filename_
), fd(-1) {
65 filename
+= "/flintlock";
67 operator bool() const { return fd
!= -1; }
69 FlintLock(const std::string
&filename_
) : filename(filename_
), fd(-1) {
70 filename
+= "/flintlock";
72 operator bool() const { return fd
!= -1; }
74 // Release any lock held when we're destroyed.
75 ~FlintLock() { release(); }
77 /** Attempt to obtain the lock.
79 * If the attempt fails with code "UNKNOWN", the string supplied in the
80 * explanation parameter will be set to contain any details available of
81 * the reason for the failure.
83 * @param exclusive Get an exclusive lock? Value currently ignored,
84 * and the lock is always exclusive.
85 * @param wait Wait until we can get the lock?
87 reason
lock(bool exclusive
, bool wait
, std::string
& explanation
);
92 /// Throw Xapian::DatabaseLockError.
94 void throw_databaselockerror(FlintLock::reason why
,
95 const std::string
& db_dir
,
96 const std::string
& explanation
));
99 #endif // XAPIAN_INCLUDED_FLINT_LOCK_H