// This file is part of cevo-evo. // // This software is governed by the CeCILL license under French law and // abiding by the rules of distribution of free software. You can use, // modify and/or redistribute the software under the terms of the CeCILL // license as circulated by CEA, CNRS and INRIA at the following URL // "http://www.cecill.info". // // As a counterpart to the access to the source code and rights to copy, // modify and redistribute granted by the license, users are provided only // with a limited warranty and the software's author, the holder of the // economic rights, and the successive licensors have only limited // liability. // // In this respect, the user's attention is drawn to the risks associated // with loading, using, modifying and/or developing or reproducing the // software by the user in light of its specific status of free software, // that may mean that it is complicated to manipulate, and that also // therefore means that it is reserved for developers and experienced // professionals having in-depth computer knowledge. Users are therefore // encouraged to load and test the software's suitability as regards their // requirements in conditions enabling the security of their systems and/or // data to be ensured and, more generally, to use and operate it in the // same conditions as regards security. // // The fact that you are presently reading this means that you have had // knowledge of the CeCILL license and that you accept its terms. #pragma once #include #include #include #include namespace Cevo { // Equivalent of Pool.pas's TPool: a simple object pool (Acquire/Release). // Override newItemObject() to customize item construction. NOT thread-safe // by itself (matches the original) - see ThreadedPool below for that. template class Pool { public: virtual ~Pool() { setTotalCount(0); } int totalCount() const { return m_items.size(); } void setTotalCount(int value) { if (value > m_items.size()) { for (int i = m_items.size(); i < value; ++i) { T *item = newItemObject(); m_items.append(item); m_freeItems.append(item); } } else { for (int i = m_items.size() - 1; i >= value; --i) { T *item = m_items.at(i); const int freeIndex = m_freeItems.indexOf(item); if (freeIndex < 0) throw std::logic_error("Pool: shrinking below a still-acquired item"); m_freeItems.removeAt(freeIndex); m_items.removeAt(i); delete item; } } } int usedCount() const { return m_items.size() - m_freeItems.size(); } // NOTE: unlike the original (which busy-waits forever if empty, a // same-thread deadlock risk since nothing else runs to free an item), // this throws immediately - see ThreadedPool for the genuinely // multi-threaded blocking-wait behavior. virtual T *acquire() { if (m_freeItems.isEmpty()) throw std::runtime_error("Object pool is empty"); return m_freeItems.takeLast(); } virtual void release(T *item) { if (!m_items.contains(item)) throw std::invalid_argument("Unknown object for release from pool"); m_freeItems.append(item); } protected: virtual T *newItemObject() { return new T(); } QVector m_items; // owns all QVector m_freeItems; // non-owning subset }; // Equivalent of Pool.pas's TThreadedPool: acquire() blocks (instead of // throwing) while the pool has capacity but no free item, waking up once // another thread calls release() - replaces the original's Sleep(1) // busy-polling loop with a QWaitCondition. template class ThreadedPool : public Pool { public: int totalCount() const { QMutexLocker locker(&m_lock); return Pool::totalCount(); } void setTotalCount(int value) { QMutexLocker locker(&m_lock); Pool::setTotalCount(value); } int usedCount() const { QMutexLocker locker(&m_lock); return Pool::usedCount(); } T *acquire() override { QMutexLocker locker(&m_lock); if (Pool::totalCount() == 0) throw std::runtime_error("Object pool is empty"); while (Pool::m_freeItems.isEmpty()) m_releaseWait.wait(&m_lock); return Pool::m_freeItems.takeLast(); } void release(T *item) override { QMutexLocker locker(&m_lock); Pool::release(item); m_releaseWait.wakeOne(); } private: mutable QMutex m_lock; QWaitCondition m_releaseWait; }; } // namespace Cevo