// Author: Jochen Kerdels #include "TMutexPool.h" //_____________________________________________________________ // // The TMutexPool class provides a means to map a huge number of resources // that needs mutex protection onto a fixed and usually small number of // mutexes. // // The main idea of a mutex pool is to provide a fixed number of mutexes // and distribute them based on a simple indexing scheme. The mutex pool // overloads the [] operator such that a mutex of the pool can be accessed // via an index. The index can be any number. It does NOT have to be smaller // than the number of mutexes in the pool. Internally, each index is used // modulo the number of mutexes to access a certain mutex. Thus, the only // requirement is, that the index used to address a mutex is constant to // that usage, i.e., each resource should have a constant id, e.g., provided // by TObject::Hash(); // // The implementation uses the TMutex class. ClassImp(TMutexPool); TMutexPool::TMutexPool(UInt_t poolSize) : TObject(), fPoolSize(poolSize) { // default constructor of a mutex pool // // -- default pool size is 16 // -- if pool size is 0, it is correted to be 1 if (fPoolSize < 1) fPoolSize = 1; fPool = new TMutex*[fPoolSize]; UInt_t t; for (t = 0; t < fPoolSize; ++t) fPool[t] = new TMutex(); } TMutexPool::~TMutexPool() { // destructor UInt_t t; for (t = 0; t < fPoolSize; ++t) delete fPool[t]; delete[] fPool; } TMutex* TMutexPool::operator[](UInt_t const& index) { // overloaded [] operator // // The operator takes the provided index and calculates // the corresponding mutex by (index % fPoolSize) return fPool[index % fPoolSize]; }