sbuild-lock.h

00001 /* Copyright © 2005  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 <string>
00024 
00025 #include <fcntl.h>
00026 #include <signal.h>
00027 #include <unistd.h>
00028 
00029 #include "sbuild-error.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     typedef runtime_error_custom<Lock> error;
00051 
00058     virtual void
00059     set_lock (Type         lock_type,
00060               unsigned int timeout) = 0;
00061 
00066     virtual void
00067     unset_lock () = 0;
00068 
00069   protected:
00071     Lock();
00073     virtual ~Lock();
00074 
00080     void
00081     set_alarm ();
00082 
00087     void
00088     clear_alarm ();
00089 
00099     void
00100     set_timer(struct itimerval const& timer);
00101 
00108     void
00109     unset_timer();
00110 
00111   private:
00113     struct sigaction saved_signals;
00114   };
00115 
00120   class FileLock : public Lock
00121   {
00122   public:
00128     FileLock (int fd);
00129 
00131     virtual ~FileLock();
00132 
00133     void
00134     set_lock (Type         lock_type,
00135               unsigned int timeout);
00136 
00137     void
00138     unset_lock ();
00139 
00140   private:
00142     int fd;
00143   };
00144 
00151   class DeviceLock : public Lock
00152   {
00153   public:
00159     DeviceLock (const std::string& device);
00160     virtual ~DeviceLock();
00161 
00162     void
00163     set_lock (Type         lock_type,
00164               unsigned int timeout);
00165 
00166     void
00167     unset_lock ();
00168 
00169   private:
00171     std::string device;
00172   };
00173 
00174 }
00175 
00176 #endif /* SBUILD_LOCK_H */
00177 
00178 /*
00179  * Local Variables:
00180  * mode:C++
00181  * End:
00182  */

Generated on Thu Dec 29 17:20:02 2005 for schroot by  doxygen 1.4.5