00001 /* 00002 * 00003 * 00004 * Distributed under the OpenDDS License. 00005 * See: http://www.opendds.org/license.html 00006 */ 00007 00008 #ifndef DATABLOCKLOCKPOOL_H 00009 #define DATABLOCKLOCKPOOL_H 00010 00011 #include "ace/Lock_Adapter_T.h" 00012 #include "ace/Thread_Mutex.h" 00013 #include "ace/Containers_T.h" 00014 #include "dcps_export.h" 00015 #include "dds/DCPS/PoolAllocationBase.h" 00016 00017 /** 00018 * @class DataBlockLockPool 00019 * 00020 * @brief Holds and distributes locks to be used for locking 00021 * ACE_Data_Blocks. Currently it does not require returning the 00022 * locks. 00023 * 00024 * @NOTE: The lock returned is not guaranteed to be unique. 00025 * 00026 * @NOTE: This class is thread safe. 00027 */ 00028 class OpenDDS_Dcps_Export DataBlockLockPool : public OpenDDS::DCPS::PoolAllocationBase { 00029 public: 00030 typedef ACE_Lock_Adapter<ACE_Thread_Mutex> DataBlockLock; 00031 00032 DataBlockLockPool(unsigned long size) 00033 : pool_(size), 00034 size_(size), 00035 iterator_(0) 00036 { 00037 } 00038 00039 ~DataBlockLockPool() { } 00040 00041 DataBlockLock * get_lock() { 00042 unsigned long index = iterator_++ % size_; 00043 return &(pool_[index]); 00044 } 00045 00046 private: 00047 typedef ACE_Array<DataBlockLock> Pool; 00048 00049 Pool pool_; 00050 const unsigned long size_; 00051 /// Counter used to track which lock to give out next (modulus size_) 00052 ACE_Atomic_Op<ACE_Thread_Mutex, unsigned long> iterator_; 00053 }; 00054 00055 #endif /* DATABLOCKLOCKPOOL_H */