sbuild  1.6.10
sbuild-lock.h
1 /* Copyright © 2005-2007 Roger Leigh <rleigh@debian.org>
2  *
3  * schroot is free software: you can redistribute it and/or modify it
4  * under the terms of the GNU General Public License as published by
5  * the Free Software Foundation, either version 3 of the License, or
6  * (at your option) any later version.
7  *
8  * schroot is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  *********************************************************************/
18 
19 #ifndef SBUILD_LOCK_H
20 #define SBUILD_LOCK_H
21 
22 #include <sbuild/sbuild-lock.h>
23 #include <sbuild/sbuild-custom-error.h>
24 
25 #include <string>
26 
27 #include <sys/time.h>
28 #include <fcntl.h>
29 #include <signal.h>
30 
31 namespace sbuild
32 {
33 
38  class lock
39  {
40  public:
42  enum type
43  {
44  LOCK_SHARED = F_RDLCK,
45  LOCK_EXCLUSIVE = F_WRLCK,
46  LOCK_NONE = F_UNLCK
47  };
48 
51  {
55  LOCK,
64  };
65 
68 
75  virtual void
76  set_lock (type lock_type,
77  unsigned int timeout) = 0;
78 
83  virtual void
84  unset_lock () = 0;
85 
86  protected:
88  lock ();
90  virtual ~lock ();
91 
97  void
98  set_alarm ();
99 
104  void
105  clear_alarm ();
106 
116  void
117  set_timer (struct itimerval const& timer);
118 
125  void
126  unset_timer ();
127 
128  private:
130  struct sigaction saved_signals;
131  };
132 
137  class file_lock : public lock
138  {
139  public:
145  file_lock (int fd);
146 
148  virtual ~file_lock ();
149 
150  virtual void
151  set_lock (lock::type lock_type,
152  unsigned int timeout);
153 
154  virtual void
155  unset_lock ();
156 
157  private:
159  int fd;
161  bool locked;
162  };
163 
164 }
165 
166 #endif /* SBUILD_LOCK_H */
167 
168 /*
169  * Local Variables:
170  * mode:C++
171  * End:
172  */
A shared (read) lock.
Definition: sbuild-lock.h:44
Failed to lock file (timed out).
Definition: sbuild-lock.h:57
struct sigaction saved_signals
Signals saved during timeout.
Definition: sbuild-lock.h:130
Debian source builder components.
Definition: sbuild-auth-null.h:24
Failed to unlock device (timed out)
Definition: sbuild-lock.h:63
Failed to unlock file.
Definition: sbuild-lock.h:56
void unset_timer()
Remove any itimer currently set up.
Definition: sbuild-lock.cc:130
No lock.
Definition: sbuild-lock.h:46
virtual void unset_lock()=0
Release a lock.
Failed to lock device.
Definition: sbuild-lock.h:59
void set_timer(struct itimerval const &timer)
Set up an itimer for future expiry.
Definition: sbuild-lock.cc:118
File lock.
Definition: sbuild-lock.h:137
Advisory locking.
Definition: sbuild-lock.h:38
An exclusive (write) lock.
Definition: sbuild-lock.h:45
custom_error< error_code > error
Exception type.
Definition: sbuild-lock.h:67
error_code
Error codes.
Definition: sbuild-lock.h:50
Failed to unlock device.
Definition: sbuild-lock.h:62
virtual void set_lock(type lock_type, unsigned int timeout)=0
Acquire a lock.
int fd
The file descriptor to lock.
Definition: sbuild-lock.h:159
bool locked
Is the file locked?
Definition: sbuild-lock.h:161
Custom error.
Definition: sbuild-custom-error.h:32
Failed to set timeout.
Definition: sbuild-lock.h:53
Failed to cancel timeout.
Definition: sbuild-lock.h:54
Failed to set timeout handler.
Definition: sbuild-lock.h:52
virtual ~lock()
The destructor.
Definition: sbuild-lock.cc:94
void set_alarm()
Set the SIGALARM handler.
Definition: sbuild-lock.cc:99
Failed to test device lock.
Definition: sbuild-lock.h:61
void clear_alarm()
Restore the state of SIGALRM prior to starting lock acquisition.
Definition: sbuild-lock.cc:111
Failed to lock device (timed out).
Definition: sbuild-lock.h:60
lock()
The constructor.
Definition: sbuild-lock.cc:89
Failed to unlock file (timed out).
Definition: sbuild-lock.h:58
Failed to lock file.
Definition: sbuild-lock.h:55
type
Lock type.
Definition: sbuild-lock.h:42