www.pudn.com > multi-tasking.rar > sharedQT.cc
// sharedQT.cc - SharedQTclass method templates // Copyright 1998-2010 Wind River Systems, Inc. // // modification history // -------------------- // 02a,30mar98,pai added Wind River coding conventions // 01a,24mar98,pai written // // // DESCRIPTION // This class provides the following public methods: // // -sharedEnQ // -sharedDeQ // -sharedEmptyQ // -sharedFullQ // -sharedCountQ // // This is a very basic shared queue template. The type stored in the queue // and the maximum number of elements to be stored in the queue are specified // when the object is constructed. For example: // // SharedQT myQ (10); // // will construct a queue which can hold up to 10 integers. If a queue size // is not specified during construction, the queue will use a default size of // 50 items. // // This class is essentially a very thin wrapper around the QueueT class. // As the class name implies, the intent behind the sharedQT class is to // provide a thread safe container. QueueT does not provide a mutual // exclusion mechanism. SharedQT attempts to make QueueT thread safe // by guarding calls to QueueT methods with a mutex semaphore. // // It is assumed that multiple tasks will be trying to read from and write to // a SharedQT object. A single mutex semaphore insures that when one task // is in the middle of some queue operation, another task will not preempt the // operation at an inappropriate moment. // // SharedQT ::SharedQT initializes the object's mutex semaphore using // SEM_Q_PRIORITY and SEM_DELETE_SAFE options. Pended task will be put on the // ready queue on the basis of priority, and a task which owns the semaphore // will be safe from unexpected deletion. The SEM_DELETE_SAFE enables an // implicit taskSafe() for each semTask(), and an implicit taskUnsafe() for // each semGive(). // // INCLUDE FILES: sharedQT.h // //////////////////////////////////////////////////////////////////////////////// // // SharedQT ::SharedQT - construct a SharedQT object // // There is but one constructor in this class. After the base class // constructor is called, this constructor will initialize the mutex semaphore. // // If an argument, qs, is supplied to this constructor, the queue will be // constructed to hold up to 'qs' number of elements. If no argument is // supplied to the constructor, the queue will, by default, allow up to 50 // items to be stored in the queue. // // RETURNS: N/A // template SharedQT - ::SharedQT (int qs) : QueueT
- (qs) { sharedQMutx = semMCreate (SEM_Q_PRIORITY | SEM_DELETE_SAFE); } //////////////////////////////////////////////////////////////////////////////// // // SharedQT
::~SharedQT - destroy a shared queue object // // This destructor deletes the mutex semaphore. The base class destructor // will delete all of the memory allocated for queue items. // // RETURNS: N/A // template SharedQT - ::~SharedQT () { // It is a good idea to own a semaphore you intend to delete semTake (sharedQMutx, WAIT_FOREVER); // QueueT
::~QueueT is virtual; it will take care of // deleting memory allocated to hold items in the queue. semDelete (sharedQMutx); } //////////////////////////////////////////////////////////////////////////////// // // SharedQT ::sharedEnQ - add an item to a shared queue // // This method will attempt to take an internal semaphore before adding // an item to the queue. The argument passed to this method should be // a reference to the type specified when the shared queue was constructed. // // RETURNS: True, if the item was added to the queue. False is returned // to indicate that the item could not be added to the queue. // template Bool SharedQT - ::sharedEnQ (const Item & item) { Bool status; //// GUARD //// semTake (sharedQMutx, WAIT_FOREVER); /////////////// status = enQueue (item); semGive (sharedQMutx); return status; } //////////////////////////////////////////////////////////////////////////////// // // SharedQT
::sharedDeQ - remove an item from a shared queue // // This method will remove an item from a shared queue and delete the memory // allocated to store the item. The dequeued item is placed in the argument // for this method. The method will not attempt to remove an item until it // has taken an internal semaphore. // // RETURNS: True, if the item was dequeued. False is returned to indicate // that an item could not be dequeued. // template Bool SharedQT - ::sharedDeQ (Item & item) { Bool status; //// GUARD //// semTake (sharedQMutx, WAIT_FOREVER); /////////////// status = deQueue (item); semGive (sharedQMutx); return status; } //////////////////////////////////////////////////////////////////////////////// // // SharedQT
::sharedEmptyQ - test for an empty shared queue // // This method tests a shared queue for an empty state. // // RETURNS: True, if the shared queue is empty. False is returned to // indicate that the queue is not empty. // template Bool SharedQT - ::sharedEmptyQ () const { Bool status; //// GUARD //// semTake (sharedQMutx, WAIT_FOREVER); /////////////// status = emptyQueue (); semGive (sharedQMutx); return status; } //////////////////////////////////////////////////////////////////////////////// // // SharedQT
::sharedFullQ - test for a full shared queue // // This method tests for a full queue. // // RETURNS: True, if the queue is full. False is returned to indicate that // the queue is not full. // template Bool SharedQT - ::sharedFullQ () const { Bool status; //// GUARD //// semTake (sharedQMutx, WAIT_FOREVER); /////////////// status = fullQueue (); semGive (sharedQMutx); return status; } //////////////////////////////////////////////////////////////////////////////// // // SharedQT
::sharedCountQ - return the number of items in a shared queue // // This method will return the number of items currently stored in a queue. // // RETURNS: The number of elements in the queue. // template int SharedQT - ::sharedCountQ () const { int sharedCount; //// GUARD //// semTake (sharedQMutx, WAIT_FOREVER); /////////////// sharedCount = countQueue (); semGive (sharedQMutx); return sharedCount; }