Line data Source code
1 : /* 2 : * 3 : * 4 : * Distributed under the OpenDDS License. 5 : * See: http://www.opendds.org/license.html 6 : */ 7 : 8 : #ifndef OPENDDS_DCPS_DATABLOCKLOCKPOOL_H 9 : #define OPENDDS_DCPS_DATABLOCKLOCKPOOL_H 10 : 11 : #include "Atomic.h" 12 : #include "dcps_export.h" 13 : #include "PoolAllocationBase.h" 14 : 15 : #include <ace/Lock_Adapter_T.h> 16 : #include <ace/Thread_Mutex.h> 17 : #include <ace/Containers_T.h> 18 : 19 : /** 20 : * @class DataBlockLockPool 21 : * 22 : * @brief Holds and distributes locks to be used for locking 23 : * ACE_Data_Blocks. Currently it does not require returning the 24 : * locks. 25 : * 26 : * @NOTE: The lock returned is not guaranteed to be unique. 27 : * 28 : * @NOTE: This class is thread safe. 29 : */ 30 : class OpenDDS_Dcps_Export DataBlockLockPool : public OpenDDS::DCPS::PoolAllocationBase { 31 : public: 32 : typedef ACE_Lock_Adapter<ACE_Thread_Mutex> DataBlockLock; 33 : 34 0 : DataBlockLockPool(unsigned long size) 35 0 : : pool_(size), 36 0 : size_(size), 37 0 : iterator_(0) 38 : { 39 0 : } 40 : 41 0 : ~DataBlockLockPool() { } 42 : 43 0 : DataBlockLock * get_lock() { 44 0 : const unsigned long index = iterator_++ % size_; 45 0 : return &(pool_[index]); 46 : } 47 : 48 : private: 49 : typedef ACE_Array<DataBlockLock> Pool; 50 : 51 : Pool pool_; 52 : const unsigned long size_; 53 : /// Counter used to track which lock to give out next (modulus size_) 54 : OpenDDS::DCPS::Atomic<unsigned long> iterator_; 55 : }; 56 : 57 : #endif /* DATABLOCKLOCKPOOL_H */