aboutsummaryrefslogtreecommitdiffstats
path: root/SQLiteStudio3/coreSQLiteStudio/common/objectpool.h
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@ubuntu.com>2014-12-06 17:33:25 -0500
committerLibravatarUnit 193 <unit193@ubuntu.com>2014-12-06 17:33:25 -0500
commit7167ce41b61d2ba2cdb526777a4233eb84a3b66a (patch)
treea35c14143716e1f2c98f808c81f89426045a946f /SQLiteStudio3/coreSQLiteStudio/common/objectpool.h
Imported Upstream version 2.99.6upstream/2.99.6
Diffstat (limited to 'SQLiteStudio3/coreSQLiteStudio/common/objectpool.h')
-rw-r--r--SQLiteStudio3/coreSQLiteStudio/common/objectpool.h84
1 files changed, 84 insertions, 0 deletions
diff --git a/SQLiteStudio3/coreSQLiteStudio/common/objectpool.h b/SQLiteStudio3/coreSQLiteStudio/common/objectpool.h
new file mode 100644
index 0000000..b9f6b9f
--- /dev/null
+++ b/SQLiteStudio3/coreSQLiteStudio/common/objectpool.h
@@ -0,0 +1,84 @@
+#ifndef OBJECTPOOL_H
+#define OBJECTPOOL_H
+
+#include <QHash>
+#include <QHashIterator>
+#include <QMutex>
+#include <QWaitCondition>
+
+template <class T>
+class ObjectPool
+{
+ public:
+ ObjectPool(quint32 min, quint32 max);
+
+ T* reserve();
+ void release(T* obj);
+
+ private:
+ QHash<T*, bool> pool;
+ QMutex mutex;
+ QWaitCondition waitCond;
+ int min;
+ int max;
+};
+
+template <class T>
+ObjectPool::ObjectPool(quint32 min, quint32 max)
+ : min(min), max(max)
+{
+ Q_ASSERT(min > 0);
+ T* obj = nullptr;
+ for (int i = 0; i < min; i++)
+ {
+ obj = new T();
+ pool[obj] = false;
+ }
+}
+
+T* ObjectPool::reserve()
+{
+ mutex.lock();
+
+ forever
+ {
+ QHashIterator<T*, bool> i(pool);
+ while (i.hasNext())
+ {
+ i.next();
+ if (!i.value())
+ {
+ pool[i.key()] = true;
+ T* obj = i.key();
+ mutex.unlock();
+ return obj;
+ }
+ }
+
+ // Check if we can enlarge the pool
+ if (pool.size() < max)
+ {
+ T* obj = new T();
+ pool[i.key()] = true;
+ mutex.unlock();
+ return obj;
+ }
+
+ // Wait for release
+ waitCond.wait(&mutex);
+ }
+
+ // no need to unlock, because the loop will repeat
+ // until the free obj is found and then mutex is unlocked.
+}
+
+template <class T>
+void ObjectPool::release(T* obj)
+{
+ mutex.lock();
+ pool[obj] = false;
+ mutex.unlock();
+ waitCond.wakeOne();
+}
+
+#endif // OBJECTPOOL_H