ARC SDK
Public Member Functions | Protected Types | Protected Member Functions | Friends
Arc::Counter Class Referenceabstract

A class defining a common interface for counters. More...

#include <arc/Counter.h>

Inheritance diagram for Arc::Counter:
Arc::IntraProcessCounter

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...
 

Detailed Description

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:

// 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();

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:

tick = memory.reserve(2*sizeof(double), Glib::TimeVal(1,0));

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:

tick = memory.reserve(2*sizeof(double), Glib::TimeVal(1,0), true);

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:

tick = memory.reserve(2*sizeof(double), Glib::TimeVal(1,0),
true, Glib::TimeVal(2,0));
if (tick.isValid())
doSomething(...);

Member Typedef Documentation

◆ IDType

typedef unsigned long long int Arc::Counter::IDType
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.

Constructor & Destructor Documentation

◆ Counter()

Arc::Counter::Counter ( )
protected

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()

virtual Arc::Counter::~Counter ( )
virtual

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.

Member Function Documentation

◆ cancel()

virtual void Arc::Counter::cancel ( IDType  reservationID)
protectedpure virtual

Cancellation of a reservation.

This method cancels a reservation. It is called by the CounterTicket that corresponds to the reservation.

Parameters
reservationIDThe identity number (key) of the reservation to cancel.

Implemented in Arc::IntraProcessCounter.

◆ changeExcess()

virtual int Arc::Counter::changeExcess ( int  amount)
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.

Parameters
amountThe amount by which to change the excess limit.
Returns
The new excess limit.

Implemented in Arc::IntraProcessCounter.

◆ changeLimit()

virtual int Arc::Counter::changeLimit ( int  amount)
pure virtual

Changes the limit of the counter.

Changes the limit of the counter by adding a certain amount to the current limit.

Parameters
amountThe amount by which to change the limit.
Returns
The new limit.

Implemented in Arc::IntraProcessCounter.

◆ extend()

virtual void Arc::Counter::extend ( IDType reservationID,
Glib::TimeVal &  expiryTime,
Glib::TimeVal  duration = ETERNAL 
)
protectedpure virtual

Extension of a reservation.

This method extends a reservation. It is called by the CounterTicket that corresponds to the reservation.

Parameters
reservationIDUsed 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.
expiryTimeUsed 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.
durationThe 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.

◆ getCounterTicket()

CounterTicket Arc::Counter::getCounterTicket ( Counter::IDType  reservationID,
Glib::TimeVal  expiryTime,
Counter counter 
)
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.)

Parameters
reservationIDThe identity number of the reservation corresponding to the CounterTicket.
expiryTimethe expiry time of the reservation corresponding to the CounterTicket.
counterThe Counter from which the reservation has been made.
Returns
The counter ticket that has been created.

◆ getCurrentTime()

Glib::TimeVal Arc::Counter::getCurrentTime ( )
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.

◆ getExcess()

virtual int Arc::Counter::getExcess ( )
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.

Returns
The excess limit.

Implemented in Arc::IntraProcessCounter.

◆ getExpirationReminder()

ExpirationReminder Arc::Counter::getExpirationReminder ( Glib::TimeVal  expTime,
Counter::IDType  resID 
)
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.)

Parameters
expTimethe expiry time of the reservation corresponding to the ExpirationReminder.
resIDThe identity number of the reservation corresponding to the ExpirationReminder.
Returns
The ExpirationReminder that has been created.

◆ getExpiryTime()

Glib::TimeVal Arc::Counter::getExpiryTime ( Glib::TimeVal  duration)
protected

Computes an expiry time.

This method computes an expiry time by adding a duration to the current time.

Parameters
durationThe duration.
Returns
The expiry time.

◆ getLimit()

virtual int Arc::Counter::getLimit ( )
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.

Returns
The current limit of the counter.

Implemented in Arc::IntraProcessCounter.

◆ getValue()

virtual int Arc::Counter::getValue ( )
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.

Returns
The current value of the counter.

Implemented in Arc::IntraProcessCounter.

◆ reserve()

virtual CounterTicket Arc::Counter::reserve ( int  amount = 1,
Glib::TimeVal  duration = ETERNAL,
bool  prioritized = false,
Glib::TimeVal  timeOut = ETERNAL 
)
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.

Parameters
amountThe amount to reserve, default value is 1.
durationThe duration of a self expiring reservation, default is that it lasts forever.
prioritizedWhether this reservation is prioritized and thus allowed to use the excess limit.
timeOutThe maximum time to block if the value of the counter is too low, default is to allow "eternal" blocking.
Returns
A CounterTicket that can be queried about the status of the reservation as well as for cancellations and extensions.

Implemented in Arc::IntraProcessCounter.

◆ setExcess()

virtual int Arc::Counter::setExcess ( int  newExcess)
pure virtual

Sets the excess limit of the counter.

This method sets a new excess limit for the counter.

Parameters
newExcessThe new excess limit, an absolute number.
Returns
The new excess limit.

Implemented in Arc::IntraProcessCounter.

◆ setLimit()

virtual int Arc::Counter::setLimit ( int  newLimit)
pure virtual

Sets the limit of the counter.

This method sets a new limit for the counter.

Parameters
newLimitThe new limit, an absolute number.
Returns
The new limit.

Implemented in Arc::IntraProcessCounter.

Friends And Related Function Documentation

◆ CounterTicket

friend class CounterTicket
friend

The CounterTicket class needs to be a friend.

◆ ExpirationReminder

friend class ExpirationReminder
friend

The ExpirationReminder class needs to be a friend.


The documentation for this class was generated from the following file: