xapian-core  2.0.0
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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef XAPIAN_INCLUDED_FLINT_LOCK_H
22 #define XAPIAN_INCLUDED_FLINT_LOCK_H
23 
24 #ifndef PACKAGE
25 # error config.h must be included first in each C++ source file
26 #endif
27 
28 #include <string>
29 
30 #if defined __CYGWIN__ || defined __WIN32__
31 # include "safewindows.h"
32 #else
33 # include <sys/types.h>
34 #endif
35 
36 class FlintLock {
37  std::string filename;
38 #if defined __CYGWIN__ || defined __WIN32__
39  HANDLE hFile = INVALID_HANDLE_VALUE;
40 #elif defined FLINTLOCK_USE_FLOCK
41  int fd = -1;
42 #else
43  int fd = -1;
44  pid_t pid;
45 #endif
46 
47  public:
48  typedef enum {
49  SUCCESS, // We got the lock!
50  INUSE, // Already locked by someone else.
51  UNSUPPORTED, // Locking probably not supported (e.g. NFS without lockd).
52  FDLIMIT, // Process hit its file descriptor limit.
53  UNKNOWN // Locking failed for some unspecified reason (keep this last).
54  } reason;
55 
57  explicit FlintLock(const std::string &filename_)
58  : filename(filename_) {
59  // Keep the same lockfile name as flint since the locking is compatible
60  // and this avoids the possibility of creating two databases in the
61  // same directory using different backends.
62  filename += "/flintlock";
63  }
64 
66  FlintLock() {}
67 
68  operator bool() const {
69 #if defined __CYGWIN__ || defined __WIN32__
70  return hFile != INVALID_HANDLE_VALUE;
71 #else
72  return fd != -1;
73 #endif
74  }
75 
76  // Release any lock held when we're destroyed.
78 
89  bool test() const;
90 
101  reason lock(bool exclusive, bool wait, std::string & explanation);
102 
104  void release();
105 
107  [[noreturn]]
109  const std::string & db_dir,
110  const std::string & explanation) const;
111 };
112 
113 #endif // XAPIAN_INCLUDED_FLINT_LOCK_H
FlintLock()
Constructor for use in read-only cases (like single-file glass).
Definition: flint_lock.h:66
void release()
Release the lock.
Definition: flint_lock.cc:458
FlintLock(const std::string &filename_)
Standard constructor.
Definition: flint_lock.h:57
reason lock(bool exclusive, bool wait, std::string &explanation)
Attempt to obtain the lock.
Definition: flint_lock.cc:124
bool test() const
Test if the lock is held.
Definition: flint_lock.cc:74
@ UNSUPPORTED
Definition: flint_lock.h:51
pid_t pid
Definition: flint_lock.h:44
void throw_databaselockerror(FlintLock::reason why, const std::string &db_dir, const std::string &explanation) const
Throw Xapian::DatabaseLockError.
Definition: flint_lock.cc:494
~FlintLock()
Definition: flint_lock.h:77
std::string filename
Definition: flint_lock.h:37
include <windows.h> without all the bloat and damage.