sbuild-lock.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2006  Roger Leigh <rleigh@debian.org>
00002  *
00003  * schroot is free software; you can redistribute it and/or modify it
00004  * under the terms of the GNU General Public License as published by
00005  * the Free Software Foundation; either version 2 of the License, or
00006  * (at your option) any later version.
00007  *
00008  * schroot is distributed in the hope that it will be useful, but
00009  * WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011  * General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program; if not, write to the Free Software
00015  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00016  * MA  02111-1307  USA
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 /* SBUILD_LOCK_H */
00194 
00195 /*
00196  * Local Variables:
00197  * mode:C++
00198  * End:
00199  */

Generated on Sat Jan 27 16:11:03 2007 for schroot by  doxygen 1.5.1