karoo::referable Class Reference

Any classes which may be auto-garbage collected, must inherit this class. More...

#include <util.h>

Inheritance diagram for karoo::referable:

karoo::exepool karoo::exeque karoo::rock_datagram_message_handler karoo::datagram_message karoo::datagram_server karoo::rock_datagram_reply karoo::rock_datagram_message

List of all members.

Public Member Functions

unsigned reference ()
 Increment the reference count.
unsigned dereference ()
 Decrement the reference count.
unsigned getReferenceCount () const


Detailed Description

Any classes which may be auto-garbage collected, must inherit this class.

It keeps track of reference counts. It will automatically delete the object when its reference count is decremented to zero.

This class is especially useful if you want to throw a newly allocated pebble into a queue and have it auto-garbage-collected after it has run. The pebble class inherits referable, and it also includes the pebble::dereferenceAfterRun method, which, if called, sets the pebble up so that when it is added to the exeque, its reference count is incremented, and then after its run, its reference count is decremented. If its reference count starts from zero before it is added to the exeque (as it would be if its just been allocated) then when it has been run, its reference count will become zero, and it will be automatically deleted.

It contains a reference ound integer, and a semaphore which is used to lock accesses to the reference count. The reference count is not a semaphore, though this would have been the simplest way to implement a counter. Its not a semaphore because when it goes to zero, we need to synchronously determine that now it is time to delete the object. This must be done inside of a semaphore lock, otherwise race conditions can happen. Hence, it has to do two semaphore operations for each increment or decrement, but there is no avoiding this.


Member Function Documentation

unsigned karoo::referable::dereference (  )  [inline]

Decrement the reference count.

When the reference count goes from 1 to zero, the object will be deleted. Therefore you should not call dereference() on an object that was not allocated with new.

Returns:
the current reference count

unsigned karoo::referable::reference (  )  [inline]

Increment the reference count.

Returns:
the current reference count


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

Generated on Tue Feb 16 15:04:30 2010 for Karoo by  doxygen 1.5.8