/usr/include/arc/Counter.h is in nordugrid-arc-dev 5.0.5-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 | // -*- indent-tabs-mode: nil -*-
// Counter.h
#ifndef __Counter__
#define __Counter__
#include <glibmm/timeval.h>
namespace Arc {
/// A time very far in the future.
extern const Glib::TimeVal ETERNAL;
/// A time very far in the past.
extern const Glib::TimeVal HISTORIC;
// Some forward declarations.
class Counter;
class CounterTicket;
class ExpirationReminder;
/// A class defining a common interface for counters.
/** This class defines a common interface for counters as well as
some common functionality.
The purpose of a counter is to provide housekeeping some resource
such as e.g. disk space, memory or network bandwidth. The counter
itself will not be aware of what kind of resource it limits the
use of. Neither will it be aware of what unit is being used to
measure that resource. Counters are thus very similar to
semaphores. Furthermore, counters are designed to handle
concurrent operations from multiple threads/processes in a
consistent manner.
Every counter has a limit, an excess limit and a value. The limit
is a number that specify how many units are available for
reservation. The value is the number of units that are currently
available for reservation, i.e. has not already been reserved.
The excess limit specify how many extra units can be reserved for
high priority needs even if there are no normal units available
for reservation. The excess limit is similar to the credit limit
of e.g. a VISA card.
The users of the resource must thus first call the counter in
order to make a reservation of an appropriate amount of the
resource, then allocate and use the resource and finally call the
counter again to cancel the reservation.
Typical usage is:
\code
// Declare a counter. Replace XYZ by some appropriate kind of
// counter and provide required parameters. Unit is MB.
XYZCounter memory(...);
...
// Make a reservation of memory for 2000000 doubles.
CounterTicket tick = memory.reserve(2*sizeof(double));
// Use the memory.
double* A=new double[2000000];
doSomething(A);
delete[] A;
// Cancel the reservation.
tick.cancel();
\endcode
There are also alternative ways to make reservations, including
self-expiring reservations, prioritized reservations and
reservations that fail if they cannot be made fast enough.
For self expiring reservations, a duration is provided in the
reserve call:
\code
tick = memory.reserve(2*sizeof(double), Glib::TimeVal(1,0));
\endcode
A self-expiring reservation can be cancelled explicitly before it
expires, but if it is not cancelled it will expire automatically
when the duration has passed. The default value for the duration
is ETERNAL, which means that the reservation will not be
cancelled automatically.
Prioritized reservations may use the excess limit and succeed
immediately even if there are no normal units available for
reservation. The value of the counter will in this case become
negative. A prioritized reservation looks like this:
\code
tick = memory.reserve(2*sizeof(double), Glib::TimeVal(1,0), true);
\endcode
Finally, a time out option can be provided for a reservation. If
some task should be performed within two seconds or not at all,
the reservation can look like this:
\code
tick = memory.reserve(2*sizeof(double), Glib::TimeVal(1,0),
true, Glib::TimeVal(2,0));
if (tick.isValid())
doSomething(...);
\endcode
\ingroup common
\headerfile Counter.h arc/Counter.h
*/
class Counter {
protected:
/// A typedef of identification numbers for reservation.
/** This is a type that is used as identification numbers (keys)
for referencing of reservations. It is used internally in
counters for book keeping of reservations as well as in the
CounterTicket class in order to be able to cancel and extend
reservations.
*/
typedef unsigned long long int IDType;
/// Default constructor.
/** This is the default constructor. Since Counter is an abstract
class, it should only be used by subclasses. Therefore it is
protected. Furthermore, since the Counter class has no
attributes, nothing needs to be initialized and thus this
constructor is empty.
*/
Counter();
public:
/// The destructor.
/** This is the destructor of the Counter class. Since the Counter
class has no attributes, nothing needs to be cleaned up and thus
the destructor is empty.
*/
virtual ~Counter();
/// Returns the current limit of the counter.
/** This method returns the current limit of the counter, i.e. how
many units can be reserved simultaneously by different threads
without claiming high priority.
@return The current limit of the counter.
*/
virtual int getLimit() = 0;
/// Sets the limit of the counter.
/** This method sets a new limit for the counter.
@param newLimit The new limit, an absolute number.
@return The new limit.
*/
virtual int setLimit(int newLimit) = 0;
/// Changes the limit of the counter.
/** Changes the limit of the counter by adding a certain amount to
the current limit.
@param amount The amount by which to change the limit.
@return The new limit.
*/
virtual int changeLimit(int amount) = 0;
/// Returns the excess limit of the counter.
/** Returns the excess limit of the counter, i.e. by how much the
usual limit may be exceeded by prioritized reservations.
@return The excess limit.
*/
virtual int getExcess() = 0;
/// Sets the excess limit of the counter.
/** This method sets a new excess limit for the counter.
@param newExcess The new excess limit, an absolute number.
@return The new excess limit.
*/
virtual int setExcess(int newExcess) = 0;
/// Changes the excess limit of the counter.
/** Changes the excess limit of the counter by adding a certain
amount to the current excess limit.
@param amount The amount by which to change the excess limit.
@return The new excess limit.
*/
virtual int changeExcess(int amount) = 0;
/// Returns the current value of the counter.
/** Returns the current value of the counter, i.e. the number of
unreserved units. Initially, the value is equal to the limit of
the counter. When a reservation is made, the the value is
decreased. Normally, the value should never be negative, but
this may happen if there are prioritized reservations. It can
also happen if the limit is decreased after some reservations
have been made, since reservations are never revoked.
@return The current value of the counter.
*/
virtual int getValue() = 0;
/// Makes a reservation from the counter.
/** This method makes a reservation from the counter. If the
current value of the counter is too low to allow for the
reservation, the method blocks until the reservation is
possible or times out.
@param amount The amount to reserve, default value is 1.
@param duration The duration of a self expiring reservation,
default is that it lasts forever.
@param prioritized Whether this reservation is prioritized and
thus allowed to use the excess limit.
@param timeOut The maximum time to block if the value of the
counter is too low, default is to allow "eternal" blocking.
@return A CounterTicket that can be queried about the status of
the reservation as well as for cancellations and extensions.
*/
virtual CounterTicket reserve(int amount = 1,
Glib::TimeVal duration = ETERNAL,
bool prioritized = false,
Glib::TimeVal timeOut = ETERNAL) = 0;
protected:
/// Cancellation of a reservation.
/** This method cancels a reservation. It is called by the
CounterTicket that corresponds to the reservation.
@param reservationID The identity number (key) of the
reservation to cancel.
*/
virtual void cancel(IDType reservationID) = 0;
/// Extension of a reservation.
/** This method extends a reservation. It is called by the
CounterTicket that corresponds to the reservation.
@param reservationID Used for input as well as output. Contains
the identification number of the original reservation on entry
and the new identification number of the extended reservation on
exit.
@param expiryTime Used for input as well as output. Contains the
expiry time of the original reservation on entry and the new
expiry time of the extended reservation on exit.
@param duration The time by which to extend the reservation. The
new expiration time is computed based on the current time, NOT
the previous expiration time.
*/
virtual void extend(IDType& reservationID,
Glib::TimeVal& expiryTime,
Glib::TimeVal duration = ETERNAL) = 0;
/// Get the current time.
/** Returns the current time. An "adapter method" for the
assign_current_time() method in the Glib::TimeVal class.
return The current time.
*/
Glib::TimeVal getCurrentTime();
/// Computes an expiry time.
/** This method computes an expiry time by adding a duration to
the current time.
@param duration The duration.
@return The expiry time.
*/
Glib::TimeVal getExpiryTime(Glib::TimeVal duration);
/// A "relay method" for a constructor of the CounterTicket class.
/** This method acts as a relay for one of the constructors of the
CounterTicket class. That constructor is private, but needs to
be accessible from the subclasses of Counter (but not from
anywhere else). In order not to have to declare every possible
subclass of Counter as a friend of CounterTicket, only the base
class Counter is a friend and its subclasses access the
constructor through this method. (If C++ had supported "package
access", as Java does, this trick would not have been
necessary.)
@param reservationID The identity number of the reservation
corresponding to the CounterTicket.
@param expiryTime the expiry time of the reservation
corresponding to the CounterTicket.
@param counter The Counter from which the reservation has been
made.
@return The counter ticket that has been created.
*/
CounterTicket getCounterTicket(Counter::IDType reservationID,
Glib::TimeVal expiryTime,
Counter *counter);
/// A "relay method" for the constructor of ExpirationReminder.
/** This method acts as a relay for one of the constructors of the
ExpirationReminder class. That constructor is private, but needs
to be accessible from the subclasses of Counter (but not from
anywhere else). In order not to have to declare every possible
subclass of Counter as a friend of ExpirationReminder, only the
base class Counter is a friend and its subclasses access the
constructor through this method. (If C++ had supported "package
access", as Java does, this trick would not have been
necessary.)
@param expTime the expiry time of the reservation corresponding
to the ExpirationReminder.
@param resID The identity number of the reservation
corresponding to the ExpirationReminder.
@return The ExpirationReminder that has been created.
*/
ExpirationReminder getExpirationReminder(Glib::TimeVal expTime,
Counter::IDType resID);
private:
/// Copy constructor, should not be used.
/** A private copy constructor, since Counters should never be
copied. It should be impossible to use, but if that would happen
by accident the program will exit with the EXIT_FAILURE code.
*/
Counter(const Counter& unique);
/// Assignment operator, should not be used.
/** A private assignment operator, since Counters should never be
assigned. It should be impossible to use, but if that would
happen by accident the program will exit with the EXIT_FAILURE
code.
*/
void operator=(const Counter& unique);
/// The CounterTicket class needs to be a friend.
friend class CounterTicket;
/// The ExpirationReminder class needs to be a friend.
friend class ExpirationReminder;
};
/// A class for "tickets" that correspond to counter reservations.
/** This is a class for reservation tickets. When a reservation is
made from a Counter, a ReservationTicket is returned. This ticket
can then be queried about the validity of a reservation. It can
also be used for cancellation and extension of reservations.
Typical usage is:
\code
// Declare a counter. Replace XYZ by some appropriate kind of
// counter and provide required parameters. Unit is MB.
XYZCounter memory(...);
...
// Make a reservation of memory for 2000000 doubles.
CounterTicket tick = memory.reserve(2*sizeof(double));
// Use the memory.
double* A=new double[2000000];
doSomething(A);
delete[] A;
// Cancel the reservation.
tick.cancel();
\endcode
\ingroup common
\headerfile Counter.h arc/Counter.h
*/
class CounterTicket {
public:
/// The default constructor.
/** This is the default constructor. It creates a CounterTicket
that is not valid. The ticket object that is created can later
be assigned a ticket that is returned by the reserve() method of
a Counter.
*/
CounterTicket();
/// Returns the validity of a CounterTicket.
/** This method checks whether a CounterTicket is valid. The
ticket was probably returned earlier by the reserve() method of
a Counter but the corresponding reservation may have expired.
@return The validity of the ticket.
*/
bool isValid();
/// Extends a reservation.
/** Extends a self-expiring reservation. In order to succeed the
extension should be made before the previous reservation
expires.
@param duration The time by which to extend the reservation. The
new expiration time is computed based on the current time, NOT
the previous expiration time.
*/
void extend(Glib::TimeVal duration);
/// Cancels a reservation.
/** This method is called to cancel a reservation. It may be
called also for self-expiring reservations, which will then be
cancelled before they were originally planned to expire.
*/
void cancel();
private:
/// A private constructor.
/** This constructor creates an CounterTicket containing the
specified expiry time and identity number of a reservation
besides a pointer to the counter from which the reservation was
made. In order to prevent unintended use, it is private. Because
the Counter class must be able to use this constructor, it is
declared to be a friend of this class.
@param reservationID The identification number of the reservation.
@param expiryTime The expiry time of the reservation.
@param counter A pointer to the counter from which the
reservation was made.
*/
CounterTicket(Counter::IDType reservationID,
Glib::TimeVal expiryTime,
Counter *counter);
/// The identification number of the corresponding reservation.
Counter::IDType reservationID;
/// The expiry time of the corresponding reservation.
Glib::TimeVal expiryTime;
/// A pointer to the Counter from which the reservation was made.
Counter *counter;
//! The Counter class needs to be a friend.
friend class Counter;
};
/// A class intended for internal use within counters.
/** This class is used for "reminder objects" that are used for
automatic deallocation of self-expiring reservations.
\ingroup common
\headerfile Counter.h arc/Counter.h
*/
class ExpirationReminder {
public:
/// Less than operator, compares "soonness".
/** This is the less than operator for the ExpirationReminder
class. It compares the priority of such objects with respect to
which reservation expires first. It is used when reminder
objects are inserted in a priority queue in order to allways
place the next reservation to expire at the top.
*/
bool operator<(const ExpirationReminder& other) const;
/// Returns the expiry time.
/** This method returns the expiry time of the reservation that
this ExpirationReminder is associated with.
@return The expiry time.
*/
Glib::TimeVal getExpiryTime() const;
/// Returns the identification number of the reservation.
/** This method returns the identification number of the
self-expiring reservation that this ExpirationReminder is
associated with.
@return The identification number.
*/
Counter::IDType getReservationID() const;
private:
/// The constructor.
/** This constructor creates an ExpirationReminder containing the
specified expiry time and identity number of a reservation. In
order to prevent unintended use, it is private. Because the
Counter class must be able to use this constructor, it is
declared to be a friend of this class.
@param expiryTime The expiry time of the reservation.
@param reservationID The identification number of the reservation.
*/
ExpirationReminder(Glib::TimeVal expiryTime,
Counter::IDType reservationID);
/// The expiry time of the corresponding reservation.
Glib::TimeVal expiryTime;
/// The identification number of t he corresponding reservation.
Counter::IDType reservationID;
/// The Counter class needs to be a friend.
friend class Counter;
};
}
#endif
|