Branch data Line data Source code
1 : : /* File locking.
2 : : Copyright (C) 2023-2024 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along 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. */
24 : 8833 : class lockfile {
25 : : public:
26 : : /* Default constructor. */
27 : 8833 : 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 : 0 : lockfile (std::string filename): lockfile ()
32 : : {
33 : 0 : this->filename = std::move (filename);
34 : : }
35 : : lockfile (lockfile const& o): lockfile (o.filename)
36 : : {}
37 : :
38 : 0 : void operator=(lockfile o)
39 : : {
40 : 0 : unlock ();
41 : 0 : this->filename = o.filename;
42 : 0 : this->fd = o.fd;
43 : 0 : o.fd = -1;
44 : 0 : }
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
66 : : locked ()
67 : : {
68 : : return fd < 0;
69 : : }
70 : :
71 : : /* Are lockfiles supported? */
72 : : static bool lockfile_supported ();
73 : : private:
74 : : std::string filename;
75 : : int fd;
76 : : };
77 : :
78 : : #endif
|