00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef SBUILD_LOCK_H
00021 #define SBUILD_LOCK_H
00022
00023 #include <sbuild/sbuild-custom-error.h>
00024
00025 #include <string>
00026
00027 #include <sys/time.h>
00028 #include <fcntl.h>
00029 #include <signal.h>
00030
00031 namespace sbuild
00032 {
00033
00038 class lock
00039 {
00040 public:
00042 enum type
00043 {
00044 LOCK_SHARED = F_RDLCK,
00045 LOCK_EXCLUSIVE = F_WRLCK,
00046 LOCK_NONE = F_UNLCK
00047 };
00048
00050 enum error_code
00051 {
00052 TIMEOUT_HANDLER,
00053 TIMEOUT_SET,
00054 TIMEOUT_CANCEL,
00055 LOCK,
00056 LOCK_TIMEOUT,
00057 DEVICE_LOCK,
00058 DEVICE_LOCK_TIMEOUT,
00059 DEVICE_TEST,
00060 DEVICE_UNLOCK,
00061 DEVICE_UNLOCK_TIMEOUT
00062 };
00063
00065 typedef custom_error<error_code> error;
00066
00073 virtual void
00074 set_lock (type lock_type,
00075 unsigned int timeout) = 0;
00076
00081 virtual void
00082 unset_lock () = 0;
00083
00084 protected:
00086 lock ();
00088 virtual ~lock ();
00089
00095 void
00096 set_alarm ();
00097
00102 void
00103 clear_alarm ();
00104
00114 void
00115 set_timer (struct itimerval const& timer);
00116
00123 void
00124 unset_timer ();
00125
00126 private:
00128 struct sigaction saved_signals;
00129 };
00130
00135 class file_lock : public lock
00136 {
00137 public:
00143 file_lock (int fd);
00144
00146 virtual ~file_lock ();
00147
00148 virtual void
00149 set_lock (lock::type lock_type,
00150 unsigned int timeout);
00151
00152 virtual void
00153 unset_lock ();
00154
00155 private:
00157 int fd;
00158 };
00159
00166 class device_lock : public lock
00167 {
00168 public:
00174 device_lock (std::string const& device);
00175
00177 virtual ~device_lock ();
00178
00179 virtual void
00180 set_lock (lock::type lock_type,
00181 unsigned int timeout);
00182
00183 virtual void
00184 unset_lock ();
00185
00186 private:
00188 std::string device;
00189 };
00190
00191 }
00192
00193 #endif
00194
00195
00196
00197
00198
00199