Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

Leases.H

Go to the documentation of this file.
00001 // Copyright (C) 2001, Compaq Computer Corporation
00002 // 
00003 // This file is part of Vesta.
00004 // 
00005 // Vesta is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 // 
00010 // Vesta is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 // 
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with Vesta; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 
00019 // Last modified on Mon May 23 22:32:33 EDT 2005 by ken@xorian.net
00020 //      modified on Mon Apr 14 16:42:32 PDT 1997 by heydon
00021 
00022 /* A lease is an abstract object containing a timestamp and named by a
00023    non-negative integer. A "Leases" object is a set of leases together with a
00024    background thread that automatically removes lease objects from the set
00025    whose timestamps are older than some fixed amount of time before the
00026    current time. It provides operations for creating a new lease, renewing the
00027    timestamp of an existing lease, testing for the existence of a lease, etc.
00028 
00029    Each "Leases" object has an associated mutex (passed to the constructor)
00030    that must be used as a monitor on the calls of its methods. This mutex is
00031    denoted in "REQUIRES" clauses below by "mu[SELF]". When a method below
00032    "REQUIRES mu[SELF] IN LL", the mutex associated with a "Leases" object must
00033    be held by the thread invoking the method. The mutex is needed to
00034    synchronize with the background thread that periodically expires leases. */
00035 
00036 #ifndef _LEASES_H
00037 #define _LEASES_H
00038 
00039 #include <Basics.H>
00040 #include <BitVector.H>
00041 
00042 class Leases {
00043   public:
00044     class NoLease {};
00045 
00046     Leases(Basics::mutex *mu, int timeout, bool debug = false) throw ();
00047     /* REQUIRES *mu IN LL */
00048     /* Create a new "Leases" object protected by the mutex "*mu" whose leases
00049        time out after at least "timeout" seconds. The mutex "*mu" is said to
00050        be the mutex associated with this "Leases" object, and is denoted by
00051        "mu[SELF]" in the "REQUIRES" clauses below. If "debug" is true, then a
00052        debugging message is written to stdout when the background thread
00053        expires leases. By default, lease expiration is enabled for a new
00054        "Leases" object. */
00055 
00056     BitVector *LeaseSet() const throw ();
00057     /* REQUIRES mu[SELF] IN LL */
00058     /* Return a newly-allocated bit vector whose bits correspond to the
00059        indices of leased cache entries. */
00060 
00061     void NewLease(int li) throw () { (void) newLs->Set(li); }
00062     /* REQUIRES mu[SELF] IN LL */
00063     /* Take out a new lease on the lease object with index "li". */
00064 
00065     void RenewLease(int li) throw (NoLease)
00066         { if (!IsLeased(li)) throw NoLease(); NewLease(li); }
00067     /* REQUIRES mu[SELF] IN LL */
00068     /* Renew the lease of the lease object "li"; "NoLease" is thrown if "li"
00069        does not presently have a lease, in which case, its lease is not
00070        renewed. */
00071 
00072     bool IsLeased(int li) throw ()
00073         { return (newLs->Read(li) || oldLs->Read(li)); }
00074     /* REQUIRES mu[SELF] IN LL */
00075     /* Return "true" iff "li" is leased. */
00076 
00077     void DisableExpiration() throw () { expiring = false; }
00078     /* REQUIRES mu[SELF] IN LL */
00079     /* Disable lease expiration. No leases will expire until
00080        "EnableExpiration" is called. */
00081 
00082     void EnableExpiration() throw () { expiring = true; }
00083     /* REQUIRES mu[SELF] IN LL */
00084     /* Re-enable lease expiration. */
00085 
00086     bool ExpirationIsEnabled() const throw() { return expiring; }
00087     /* REQUIRES mu[SELF] IN LL */
00088     /* Return true iff lease expiration is enabled (i.e., if lease
00089        expiration is *not* frozen). */
00090 
00091     void Debug(std::ostream &os) const throw ();
00092     /* REQUIRES mu[SELF] IN LL */
00093     /* Write debugging information for the leases to "os". The output is
00094        indented and ends with a newline. */
00095 
00096   private:
00097     // read-only after initialization
00098     int timeout;              // lease time (in seconds)
00099     bool debug;               // print debugging info at lease expiration?
00100 
00101     // data fields
00102     Basics::mutex *mu;        // protects following fields
00103     bool expiring;            // is lease expiration enabled?
00104     BitVector *oldLs, *newLs; // old and new lease sets
00105 
00106     // methods
00107     void Expire() throw ();
00108     /* REQUIRES Sup(LL) < mu[SELF] */
00109     /* Expire the old leases, make the new leases old, and reset the new
00110        leases. */
00111 
00112     // friends
00113     friend void* Leases_TimeoutProc(void*) throw ();
00114     /* REQUIRES Sup(LL) < mu[SELF] */
00115 
00116     // hide copy constructor from clients
00117     Leases(const Leases&);
00118 };
00119 
00120 #endif // _LEASES_H

Generated on Mon May 8 00:48:35 2006 for Vesta by  doxygen 1.4.2