00001 /** 00002 * @file orderqueue.h 00003 * @author Reshen Amin <reshen@zensrc.com> 00004 * 00005 * @brief Contains the class definition for @ref OrderQueue 00006 * 00007 * @license Byzantine Generals Problem Coding Sample.<br> 00008 * Copyright (C) 2010 Reshen Amin.<br> 00009 * See @ref license_sec for details.<br> 00010 */ 00011 00012 #ifndef SRC_ORDERQUEUE_H_ 00013 #define SRC_ORDERQUEUE_H_ 00014 00015 #include <boost/interprocess/sync/interprocess_mutex.hpp> 00016 #include <boost/interprocess/sync/interprocess_semaphore.hpp> 00017 00018 #include "order.h" 00019 00020 /** 00021 * @brief A FIFO Data Structure (Queue) which we specialize to store 00022 * Orders. 00023 */ 00024 class OrderQueue { 00025 public: 00026 /** 00027 * @brief Constructor 00028 * 00029 * @param order_alloc Shared Memory Allocator from which Orders are 00030 * allocated 00031 */ 00032 explicit OrderQueue(const OrderAllocator &order_alloc); 00033 00034 /** 00035 * @brief Destructor 00036 */ 00037 ~OrderQueue(); 00038 00039 /** 00040 * @brief Insert the provided order at the end of the queue. This 00041 * operation is thread safe and will block until it can be completed. 00042 * 00043 * @param ord Order to insert 00044 */ 00045 void PushBack(const Order& ord); 00046 00047 /** 00048 * @brief Remove and return the front @ref Order of the queue. This 00049 * operation is thread safe and will block until it can be completed 00050 * (if there are no orders in the queue, this operation will pend 00051 * until one becomes available). 00052 * 00053 * @return The front @ref Order in the queue 00054 */ 00055 Order PopFront(); 00056 00057 /** 00058 * @brief Check if the queue is empty. 00059 * 00060 * @return TRUE if the Queue is empty (size 0), FALSE otherwise. 00061 */ 00062 bool Empty(); 00063 00064 private: 00065 /** 00066 * @brief Underlying non-thread-safe container used to store the Orders 00067 */ 00068 OrderList order_list_; 00069 00070 /** 00071 * @brief Mutex used to allow thread-safe access to the underlying 00072 * @ref order_list_ 00073 */ 00074 boost::interprocess::interprocess_mutex mutex_; 00075 00076 /** 00077 * @brief Semaphore used to track the size of the queue. The 00078 * semaphore is incremented on calls to @ref PushBack and decremented 00079 * on calls to @ref PopFront 00080 */ 00081 boost::interprocess::interprocess_semaphore sem_fill_; 00082 }; 00083 00084 #endif // SRC_ORDERQUEUE_H_
1.6.3