sbuild  1.7.1
lock.h
1 /* Copyright © 2005-2013 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/lock.h>
23 #include <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 (const struct itimerval& 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  */