xapian-core  1.4.25
flint_lock.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2005,2006,2007,2008,2009,2012,2014,2016,2017 Olly Betts
5  *
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.
10  *
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.
15  *
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
19  * USA
20  */
21 
22 #ifndef XAPIAN_INCLUDED_FLINT_LOCK_H
23 #define XAPIAN_INCLUDED_FLINT_LOCK_H
24 
25 #ifndef PACKAGE
26 # error config.h must be included first in each C++ source file
27 #endif
28 
29 #include <string>
30 
31 #if defined __CYGWIN__ || defined __WIN32__
32 # include "safewindows.h"
33 #else
34 # include <sys/types.h>
35 #endif
36 
37 #include "noreturn.h"
38 
39 class FlintLock {
40  std::string filename;
41 #if defined __CYGWIN__ || defined __WIN32__
42  HANDLE hFile = INVALID_HANDLE_VALUE;
43 #elif defined FLINTLOCK_USE_FLOCK
44  int fd = -1;
45 #else
46  int fd = -1;
47  pid_t pid;
48 #endif
49 
50  public:
51  typedef enum {
52  SUCCESS, // We got the lock!
53  INUSE, // Already locked by someone else.
54  UNSUPPORTED, // Locking probably not supported (e.g. NFS without lockd).
55  FDLIMIT, // Process hit its file descriptor limit.
56  UNKNOWN // The attempt failed for some unspecified reason.
57  } reason;
58 
60  explicit FlintLock(const std::string &filename_)
61  : filename(filename_) {
62  // Keep the same lockfile name as flint since the locking is compatible
63  // and this avoids the possibility of creating two databases in the
64  // same directory using different backends.
65  filename += "/flintlock";
66  }
67 
69  FlintLock() {}
70 
71  operator bool() const {
72 #if defined __CYGWIN__ || defined __WIN32__
73  return hFile != INVALID_HANDLE_VALUE;
74 #else
75  return fd != -1;
76 #endif
77  }
78 
79  // Release any lock held when we're destroyed.
81 
92  bool test() const;
93 
104  reason lock(bool exclusive, bool wait, std::string & explanation);
105 
107  void release();
108 
110  XAPIAN_NORETURN(
112  const std::string & db_dir,
113  const std::string & explanation) const);
114 };
115 
116 #endif // XAPIAN_INCLUDED_FLINT_LOCK_H
void throw_databaselockerror(FlintLock::reason why, const std::string &db_dir, const std::string &explanation) const
Throw Xapian::DatabaseLockError.
Definition: flint_lock.cc:499
void release()
Release the lock.
Definition: flint_lock.cc:463
Define the XAPIAN_NORETURN macro.
FlintLock()
Constructor for use in read-only cases (like single-file glass).
Definition: flint_lock.h:69
pid_t pid
Definition: flint_lock.h:47
~FlintLock()
Definition: flint_lock.h:80
bool test() const
Test if the lock is held.
Definition: flint_lock.cc:75
include <windows.h> without all the bloat and damage.
FlintLock(const std::string &filename_)
Standard constructor.
Definition: flint_lock.h:60
reason lock(bool exclusive, bool wait, std::string &explanation)
Attempt to obtain the lock.
Definition: flint_lock.cc:125
std::string filename
Definition: flint_lock.h:40