sbuild-lock.h

Go to the documentation of this file.
00001 /* Copyright © 2005-2007  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         UNLOCK,               
00057         LOCK_TIMEOUT,         
00058         UNLOCK_TIMEOUT,       
00059         DEVICE_LOCK,          
00060         DEVICE_LOCK_TIMEOUT,  
00061         DEVICE_TEST,          
00062         DEVICE_UNLOCK,        
00063         DEVICE_UNLOCK_TIMEOUT 
00064       };
00065 
00067     typedef custom_error<error_code> error;
00068 
00075     virtual void
00076     set_lock (type         lock_type,
00077               unsigned int timeout) = 0;
00078 
00083     virtual void
00084     unset_lock () = 0;
00085 
00086   protected:
00088     lock ();
00090     virtual ~lock ();
00091 
00097     void
00098     set_alarm ();
00099 
00104     void
00105     clear_alarm ();
00106 
00116     void
00117     set_timer (struct itimerval const& timer);
00118 
00125     void
00126     unset_timer ();
00127 
00128   private:
00130     struct sigaction saved_signals;
00131   };
00132 
00137   class file_lock : public lock
00138   {
00139   public:
00145     file_lock (int fd);
00146 
00148     virtual ~file_lock ();
00149 
00150     virtual void
00151     set_lock (lock::type   lock_type,
00152               unsigned int timeout);
00153 
00154     virtual void
00155     unset_lock ();
00156 
00157   private:
00159     int fd;
00161     bool locked;
00162   };
00163 
00170   class device_lock : public lock
00171   {
00172   public:
00178     device_lock (std::string const& device);
00179 
00181     virtual ~device_lock ();
00182 
00183     virtual void
00184     set_lock (lock::type   lock_type,
00185               unsigned int timeout);
00186 
00187     virtual void
00188     unset_lock ();
00189 
00190   private:
00192     std::string device;
00194     bool locked;
00195   };
00196 
00197 }
00198 
00199 #endif /* SBUILD_LOCK_H */
00200 
00201 /*
00202  * Local Variables:
00203  * mode:C++
00204  * End:
00205  */

Generated on Sun Jul 8 21:23:22 2007 for sbuild by  doxygen 1.5.2