00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef SBUILD_LOCK_H
00020 #define SBUILD_LOCK_H
00021
00022 #include <sbuild/sbuild-custom-error.h>
00023
00024 #include <string>
00025
00026 #include <sys/time.h>
00027 #include <fcntl.h>
00028 #include <signal.h>
00029
00030 namespace sbuild
00031 {
00032
00037 class lock
00038 {
00039 public:
00041 enum type
00042 {
00043 LOCK_SHARED = F_RDLCK,
00044 LOCK_EXCLUSIVE = F_WRLCK,
00045 LOCK_NONE = F_UNLCK
00046 };
00047
00049 enum error_code
00050 {
00051 TIMEOUT_HANDLER,
00052 TIMEOUT_SET,
00053 TIMEOUT_CANCEL,
00054 LOCK,
00055 UNLOCK,
00056 LOCK_TIMEOUT,
00057 UNLOCK_TIMEOUT,
00058 DEVICE_LOCK,
00059 DEVICE_LOCK_TIMEOUT,
00060 DEVICE_TEST,
00061 DEVICE_UNLOCK,
00062 DEVICE_UNLOCK_TIMEOUT
00063 };
00064
00066 typedef custom_error<error_code> error;
00067
00074 virtual void
00075 set_lock (type lock_type,
00076 unsigned int timeout) = 0;
00077
00082 virtual void
00083 unset_lock () = 0;
00084
00085 protected:
00087 lock ();
00089 virtual ~lock ();
00090
00096 void
00097 set_alarm ();
00098
00103 void
00104 clear_alarm ();
00105
00115 void
00116 set_timer (struct itimerval const& timer);
00117
00124 void
00125 unset_timer ();
00126
00127 private:
00129 struct sigaction saved_signals;
00130 };
00131
00136 class file_lock : public lock
00137 {
00138 public:
00144 file_lock (int fd);
00145
00147 virtual ~file_lock ();
00148
00149 virtual void
00150 set_lock (lock::type lock_type,
00151 unsigned int timeout);
00152
00153 virtual void
00154 unset_lock ();
00155
00156 private:
00158 int fd;
00160 bool locked;
00161 };
00162
00169 class device_lock : public lock
00170 {
00171 public:
00177 device_lock (std::string const& device);
00178
00180 virtual ~device_lock ();
00181
00182 virtual void
00183 set_lock (lock::type lock_type,
00184 unsigned int timeout);
00185
00186 virtual void
00187 unset_lock ();
00188
00189 private:
00191 std::string device;
00193 bool locked;
00194 };
00195
00196 }
00197
00198 #endif
00199
00200
00201
00202
00203
00204