summaryrefslogtreecommitdiff
path: root/include/comphelper
diff options
context:
space:
mode:
authorMichael Meeks <michael.meeks@collabora.com>2014-10-30 18:37:42 +0000
committerMichael Meeks <michael.meeks@collabora.com>2014-10-30 22:12:27 +0000
commit62090f65b804a08a66ca26675ae610ed07c7c341 (patch)
tree8b227d43555e4c10d5b410ffeb6a9b9b0d9e4220 /include/comphelper
parent2f55701c550950ab4530df3c9ca305a819e3cabb (diff)
Move thread-pool down into comphelper for re-use elsewhere.
Change-Id: Ib27b8b1ccc07ff194035d6c2ef3d45c429e3cea1
Diffstat (limited to 'include/comphelper')
-rw-r--r--include/comphelper/threadpool.hxx64
1 files changed, 64 insertions, 0 deletions
diff --git a/include/comphelper/threadpool.hxx b/include/comphelper/threadpool.hxx
new file mode 100644
index 000000000000..ae103f1164f1
--- /dev/null
+++ b/include/comphelper/threadpool.hxx
@@ -0,0 +1,64 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_COMPHELPER_THREADPOOL_HXX
+#define INCLUDED_COMPHELPER_THREADPOOL_HXX
+
+#include <sal/config.h>
+#include <salhelper/thread.hxx>
+#include <osl/mutex.hxx>
+#include <osl/conditn.hxx>
+#include <rtl/ref.hxx>
+#include <vector>
+#include <comphelper/comphelperdllapi.h>
+
+namespace comphelper
+{
+
+class COMPHELPER_DLLPUBLIC ThreadTask
+{
+public:
+ virtual ~ThreadTask() {}
+ virtual void doWork() = 0;
+};
+
+/// A very basic thread pool implementation
+class COMPHELPER_DLLPUBLIC ThreadPool
+{
+public:
+ /// returns a pointer to a shared pool with optimal thread
+ /// count for the CPU
+ static ThreadPool& getSharedOptimalPool();
+
+ ThreadPool( sal_Int32 nWorkers );
+ virtual ~ThreadPool();
+
+ void pushTask( ThreadTask *pTask /* takes ownership */ );
+ void waitUntilEmpty();
+ void waitUntilWorkersDone();
+
+private:
+ class ThreadWorker;
+ friend class ThreadWorker;
+
+ ThreadTask *waitForWork( osl::Condition &rNewWork );
+ ThreadTask *popWork();
+
+ osl::Mutex maGuard;
+ osl::Condition maTasksEmpty;
+ bool mbTerminate;
+ std::vector< rtl::Reference< ThreadWorker > > maWorkers;
+ std::vector< ThreadTask * > maTasks;
+};
+
+} // namespace comphelper
+
+#endif // INCLUDED_COMPHELPER_THREADPOOL_HXX
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */