ARC SDK
|
A class defining a common interface for counters. More...
#include <arc/Counter.h>
Public Member Functions | |
virtual | ~Counter () |
The destructor. More... | |
virtual int | getLimit ()=0 |
Returns the current limit of the counter. More... | |
virtual int | setLimit (int newLimit)=0 |
Sets the limit of the counter. More... | |
virtual int | changeLimit (int amount)=0 |
Changes the limit of the counter. More... | |
virtual int | getExcess ()=0 |
Returns the excess limit of the counter. More... | |
virtual int | setExcess (int newExcess)=0 |
Sets the excess limit of the counter. More... | |
virtual int | changeExcess (int amount)=0 |
Changes the excess limit of the counter. More... | |
virtual int | getValue ()=0 |
Returns the current value of the counter. More... | |
virtual CounterTicket | reserve (int amount=1, Glib::TimeVal duration=ETERNAL, bool prioritized=false, Glib::TimeVal timeOut=ETERNAL)=0 |
Makes a reservation from the counter. More... | |
Protected Types | |
typedef unsigned long long int | IDType |
A typedef of identification numbers for reservation. More... | |
Protected Member Functions | |
Counter () | |
Default constructor. More... | |
virtual void | cancel (IDType reservationID)=0 |
Cancellation of a reservation. More... | |
virtual void | extend (IDType &reservationID, Glib::TimeVal &expiryTime, Glib::TimeVal duration=ETERNAL)=0 |
Extension of a reservation. More... | |
Glib::TimeVal | getCurrentTime () |
Get the current time. More... | |
Glib::TimeVal | getExpiryTime (Glib::TimeVal duration) |
Computes an expiry time. More... | |
CounterTicket | getCounterTicket (Counter::IDType reservationID, Glib::TimeVal expiryTime, Counter *counter) |
A "relay method" for a constructor of the CounterTicket class. More... | |
ExpirationReminder | getExpirationReminder (Glib::TimeVal expTime, Counter::IDType resID) |
A "relay method" for the constructor of ExpirationReminder. More... | |
Friends | |
class | CounterTicket |
The CounterTicket class needs to be a friend. More... | |
class | ExpirationReminder |
The ExpirationReminder class needs to be a friend. More... | |
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:
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:
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:
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:
|
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.
|
protected |
|
virtual |
|
protectedpure virtual |
Cancellation of a reservation.
This method cancels a reservation. It is called by the CounterTicket that corresponds to the reservation.
reservationID | The identity number (key) of the reservation to cancel. |
Implemented in Arc::IntraProcessCounter.
|
pure virtual |
Changes the excess limit of the counter.
Changes the excess limit of the counter by adding a certain amount to the current excess limit.
amount | The amount by which to change the excess limit. |
Implemented in Arc::IntraProcessCounter.
|
pure virtual |
Changes the limit of the counter.
Changes the limit of the counter by adding a certain amount to the current limit.
amount | The amount by which to change the limit. |
Implemented in Arc::IntraProcessCounter.
|
protectedpure virtual |
Extension of a reservation.
This method extends a reservation. It is called by the CounterTicket that corresponds to the reservation.
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. |
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. |
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. |
Implemented in Arc::IntraProcessCounter.
|
protected |
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.)
reservationID | The identity number of the reservation corresponding to the CounterTicket. |
expiryTime | the expiry time of the reservation corresponding to the CounterTicket. |
counter | The Counter from which the reservation has been made. |
|
protected |
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.
|
pure virtual |
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.
Implemented in Arc::IntraProcessCounter.
|
protected |
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.)
expTime | the expiry time of the reservation corresponding to the ExpirationReminder. |
resID | The identity number of the reservation corresponding to the ExpirationReminder. |
|
protected |
Computes an expiry time.
This method computes an expiry time by adding a duration to the current time.
duration | The duration. |
|
pure virtual |
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.
Implemented in Arc::IntraProcessCounter.
|
pure virtual |
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.
Implemented in Arc::IntraProcessCounter.
|
pure virtual |
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.
amount | The amount to reserve, default value is 1. |
duration | The duration of a self expiring reservation, default is that it lasts forever. |
prioritized | Whether this reservation is prioritized and thus allowed to use the excess limit. |
timeOut | The maximum time to block if the value of the counter is too low, default is to allow "eternal" blocking. |
Implemented in Arc::IntraProcessCounter.
|
pure virtual |
Sets the excess limit of the counter.
This method sets a new excess limit for the counter.
newExcess | The new excess limit, an absolute number. |
Implemented in Arc::IntraProcessCounter.
|
pure virtual |
Sets the limit of the counter.
This method sets a new limit for the counter.
newLimit | The new limit, an absolute number. |
Implemented in Arc::IntraProcessCounter.
|
friend |
The CounterTicket class needs to be a friend.
|
friend |
The ExpirationReminder class needs to be a friend.