GCC Middle and Back End API Reference
lockfile.h
Go to the documentation of this file.
1/* File locking.
2 Copyright (C) 2023-2024 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along with GCC; see the file COPYING3. If not see
18<http://www.gnu.org/licenses/>. */
19
20#ifndef LOCKFILE_H
21#define LOCKFILE_H
22
23/* Used to synchronize across multiple processes. */
24class lockfile {
25public:
26 /* Default constructor. */
27 lockfile (): fd (-1)
28 {}
29 /* Intended constructor for use. Filename should not be used for anything
30 other than locking to prevent unintentional unlock. */
31 lockfile (std::string filename): lockfile ()
32 {
33 this->filename = std::move (filename);
34 }
36 {}
37
39 {
40 unlock ();
41 this->filename = o.filename;
42 this->fd = o.fd;
43 o.fd = -1;
44 }
45
46 /* Unique write lock. No other lock can be held on this lockfile.
47 Blocking call. */
48 int lock_write ();
49
50 /* Unique write lock. No other lock can be held on this lockfile.
51 Only locks if this filelock is not locked by any other process.
52 Return whether locking was successful. */
53 int try_lock_write ();
54
55 /* Shared read lock. Only read lock can be held concurrently.
56 If write lock is already held by this process, it will be
57 changed to read lock.
58 Blocking call. */
59 int lock_read ();
60
61 /* Unlock all previously placed locks. */
62 void unlock ();
63
64 /* Returns whether any lock is held. */
65 bool
67 {
68 return fd < 0;
69 }
70
71 /* Are lockfiles supported? */
72 static bool lockfile_supported ();
73private:
74 std::string filename;
75 int fd;
76};
77
78#endif
Definition lockfile.h:24
lockfile()
Definition lockfile.h:27
lockfile(std::string filename)
Definition lockfile.h:31
static bool lockfile_supported()
Definition lockfile.cc:129
int lock_write()
Definition lockfile.cc:29
bool locked()
Definition lockfile.h:66
int try_lock_write()
Definition lockfile.cc:54
std::string filename
Definition lockfile.h:74
lockfile(lockfile const &o)
Definition lockfile.h:35
void operator=(lockfile o)
Definition lockfile.h:38
void unlock()
Definition lockfile.cc:107
int fd
Definition lockfile.h:75
int lock_read()
Definition lockfile.cc:84