home *** CD-ROM | disk | FTP | other *** search
- // This may look like C code, but it is really -*- C++ -*-
- /*
- ************************************************************************
- *
- * Generic Asynchronous Queue
- *
- * The present file declares classes to handle simple double-linked queues
- * in a multiple access mode. Two semaphores are provided to resolve
- * possible conflicts due to the access to the queue by several concurrently
- * running processes (threads). One semaphore counts the no. of available
- * resources (elements of the queue). A thread requesting the unavailable
- * resource, i.e. wanting to get an element from the empty queue, is to be
- * put down to sleep until somebody else would enqueue smth into the queue.
- * It means, the dequeuing process performs P-operation and the enqueuing
- * one needs to do V-operation on the count semaphore of the queue. Another
- * semaphor provides for the establishing of the critical section when
- * the process updates control structures of the queue.
- *
- * The queue is double-linked and allows a variety of operations,
- * inserting an element to the head/tail of the queue, dequeuing an element
- * from the head as well as from arbitrary position of the queue,
- * search on id and key. A queue element (slot) contains, besides
- * linking pointers, two integer fields, id and key, which the user
- * can use in any way he thinks fit. Queue opeartions do not change them,
- * though some functions are provided for searching for the id and the key.
- *
- * The implementation of queues is based on the double-linked list.
- *
- ************************************************************************
- */
-
- #ifndef _sysqueues_h
- #define _sysqueues_h 1
- #pragma interface
-
- #include "mult_proc.h"
-
- class BasicQueue;
-
- // This is how an element of the queue
- // looks like
- class QueueLink
- {
- friend class BasicQueue;
-
- QueueLink * prev; // Ptr to the prev element
- QueueLink * next; // Ptr to the next element
-
- public:
- int id; // Some ID
- int key; // Arbitrary key
-
- QueueLink(void) : prev(0), next(0) {}
- virtual ~QueueLink(void) {}
- };
-
- class BasicQueue
- {
- int no_elements; // No. of occupied slots
- QueueLink * head; // Ptr to the 1st element in the
- // queue
- QueueLink * tail; // Ptr to the last element in the
- // queue
- Semaphore access_lock;
- Semaphore queue_sema;
-
- public:
-
- BasicQueue(void); // Create a queue of a given capacity
- ~BasicQueue(void) {}
-
- // Inquires
-
- // Return TRUE if the queue is empty
- int is_empty() const { return no_elements == 0; }
-
- // Return the no. elems in the queue
- int q_no_elems() const { return no_elements; }
- void dump_ids(void); // Dump IDs of elements in the queue
-
- QueueLink * get_from_head(void); // Get and dequeue the top element
- void append(QueueLink& el); // Add an element to the end of queue
- void dequeue(QueueLink& el); // Dequeue a given element
- int purge_id(const int id); // Purge a queue from a given element
- };
-
- #endif
-