nodes::safe_list Class Reference

Implements a thread safe list. More...

#include <safe_list.h>

Inheritance diagram for nodes::safe_list:

Inheritance graph
[legend]
Collaboration diagram for nodes::safe_list:

Collaboration graph
[legend]
List of all members.

Public Types

typedef common::list_positions iterator_positions

Public Member Functions

 safe_list ()
 creates a thread safe list that will clean its contents when destroyed.
 ~safe_list ()
 IMPLEMENT_CLASS_NAME ("safe_list")
safe_list_read_iteratoropen_reader (iterator_positions where=HEAD)
 creates a new read_iterator on this list.
safe_list_write_iteratoropen_writer (iterator_positions where=HEAD)
 creates a new write_iterator on this list.
void close_reader (safe_list_read_iterator *&to_close)
 shuts down the read iterator.
void close_writer (safe_list_write_iterator *&to_close)
 shuts down the write iterator.
bool empty (safe_list_read_iterator &hook)
 returns true if the list is empty.
bool empty (safe_list_write_iterator &hook)
 returns true if the list is empty.
int elements (safe_list_read_iterator &hook)
 returns the number of elements in the list.
int elements (safe_list_write_iterator &hook)
 returns the number of elements in the list.
void zap (safe_list_write_iterator &writer)
 destroys the current safe_node using the lock from the "writer".
void insert (safe_list_write_iterator &writer, safe_node *to_insert)
 inserts a node "to_insert" at the "writer" position.
safe_noderemove (safe_list_write_iterator &writer)
 extracts the current safe_node at the position in the "writer".

Static Public Attributes

static const iterator_positions HEAD
 the list beginning.
static const iterator_positions TAIL
 the list end.

Friends

class safe_list_read_iterator
class safe_list_write_iterator

Detailed Description

Implements a thread safe list.

The list is protected from race conditions by categorizing methods on the list into read and write operations. Operations that modify the list contents or structure are considered write operations, while others that only examine the contents of the list without changing its structure are read operations. Only one writer may lock the list or list elements at a time, but many readers may share a read lock. These restrictions are implemented by providing two types of iterators, one for reading and another writing. The only way to access the safe_list at all is through these iterators. The iterators ensure that the access pattern is controlled as described above. To use the safe_list, your structure to be stored in the list must be derived from the safe_node type, which is defined separately.

NOTE: reading the documentation for the list class (nodes/list.h) is highly beneficial for understanding usage of this object. The safe_list is essentially the same interface, but is aimed at thread safety.

Definition at line 53 of file safe_list.h.


Member Typedef Documentation

typedef common::list_positions nodes::safe_list::iterator_positions

Definition at line 65 of file safe_list.h.


Constructor & Destructor Documentation

nodes::safe_list::safe_list (  ) 

creates a thread safe list that will clean its contents when destroyed.

Definition at line 187 of file safe_list.cpp.

nodes::safe_list::~safe_list (  ) 

Definition at line 192 of file safe_list.cpp.

References WHACK().


Member Function Documentation

nodes::safe_list::IMPLEMENT_CLASS_NAME ( "safe_list"   ) 

safe_list_read_iterator * nodes::safe_list::open_reader ( iterator_positions  where = HEAD  ) 

creates a new read_iterator on this list.

note that any iterators that you open must be closed before the safe_list is destroyed. they also should be closed as soon as possible to avoid contention on the list.

Definition at line 210 of file safe_list.cpp.

References reader_writer_lock::begin_read(), and safe_list_read_iterator.

safe_list_write_iterator * nodes::safe_list::open_writer ( iterator_positions  where = HEAD  ) 

creates a new write_iterator on this list.

Definition at line 216 of file safe_list.cpp.

References reader_writer_lock::begin_write(), and safe_list_write_iterator.

Referenced by scheduler::add(), scheduler::filter_missing_actors(), scheduler::find(), scheduler::open_iterator(), scheduler::remove_by_actor(), scheduler::remove_by_id(), and scheduler::~scheduler().

void nodes::safe_list::close_reader ( safe_list_read_iterator *&  to_close  ) 

shuts down the read iterator.

Definition at line 222 of file safe_list.cpp.

References reader_writer_lock::end_read(), and WHACK().

void nodes::safe_list::close_writer ( safe_list_write_iterator *&  to_close  ) 

shuts down the write iterator.

Definition at line 228 of file safe_list.cpp.

References reader_writer_lock::end_write(), and WHACK().

Referenced by scheduler::add(), scheduler::close_iterator(), scheduler::filter_missing_actors(), scheduler::find(), scheduler::remove_by_actor(), scheduler::remove_by_id(), and scheduler::~scheduler().

bool nodes::safe_list::empty ( safe_list_read_iterator hook  ) 

returns true if the list is empty.

note that an iterator is necessary to ensure the list semantics are not violated. it is not modified.

Referenced by scheduler::~scheduler().

bool nodes::safe_list::empty ( safe_list_write_iterator hook  ) 

returns true if the list is empty.

int nodes::safe_list::elements ( safe_list_read_iterator hook  ) 

returns the number of elements in the list.

this is a fairly costly operation. note that an iterator is necessary to ensure the list semantics are not violated. it is not modified.

int nodes::safe_list::elements ( safe_list_write_iterator hook  ) 

returns the number of elements in the list.

void nodes::safe_list::zap ( safe_list_write_iterator writer  ) 

destroys the current safe_node using the lock from the "writer".

the current position in the iterator is set to the safe_node that used to reside after the unfortunate deceased safe_node (or to the new end of the list if that was the last safe_node).

Definition at line 248 of file safe_list.cpp.

References CAST_WRITER, and nodes::list::zap().

Referenced by scheduler::filter_missing_actors(), scheduler::remove_by_actor(), scheduler::remove_by_id(), scheduler::zap(), and scheduler::~scheduler().

void nodes::safe_list::insert ( safe_list_write_iterator writer,
safe_node to_insert 
)

inserts a node "to_insert" at the "writer" position.

adds the safe_node "to_insert" into the list at the current position using the lock from the "writer". the safe_node that used to be at the current position in the iterator will now be after this safe_node and "writer" will now be pointing at the node "to_insert".

Definition at line 254 of file safe_list.cpp.

References CAST_WRITER, and nodes::list::insert().

Referenced by scheduler::add().

safe_node * nodes::safe_list::remove ( safe_list_write_iterator writer  ) 

extracts the current safe_node at the position in the "writer".

the safe_list relinquishes responsibility for the safe_node after it is taken out of the list. NIL is returned if the list is empty.

Definition at line 237 of file safe_list.cpp.

References CAST_WRITER, NIL, and nodes::list::remove().


Friends And Related Function Documentation

friend class safe_list_read_iterator [friend]

Definition at line 116 of file safe_list.h.

Referenced by open_reader().

friend class safe_list_write_iterator [friend]

Definition at line 117 of file safe_list.h.

Referenced by open_writer().


Member Data Documentation

const safe_list::iterator_positions nodes::safe_list::HEAD [static]

the list beginning.

Definition at line 66 of file safe_list.h.

Referenced by nodes::safe_list_read_iterator::safe_list_read_iterator(), and nodes::safe_list_write_iterator::safe_list_write_iterator().

const safe_list::iterator_positions nodes::safe_list::TAIL [static]

the list end.

Definition at line 67 of file safe_list.h.

Referenced by nodes::object_catalog::add(), and nodes::object_catalog::find_tail().


The documentation for this class was generated from the following files:
Generated on Sat Oct 11 04:30:53 2008 for HOOPLE Libraries by  doxygen 1.5.1