Commit: 3c0d11126727dd76bc540392571643027a7006fb
Author: Sergey Sharybin
Date:   Mon Apr 27 15:33:21 2015 +0500
Branches: alembic
https://developer.blender.org/rB3c0d11126727dd76bc540392571643027a7006fb

Alembic: Add utilities for task pool and scoped lock

Currently unused, but handy for some further development.

===================================================================

M       source/blender/pointcache/CMakeLists.txt
A       source/blender/pointcache/util/util_function.h
A       source/blender/pointcache/util/util_task.cpp
A       source/blender/pointcache/util/util_task.h
A       source/blender/pointcache/util/util_thread.h

===================================================================

diff --git a/source/blender/pointcache/CMakeLists.txt 
b/source/blender/pointcache/CMakeLists.txt
index 52f62af..e89ccc9 100644
--- a/source/blender/pointcache/CMakeLists.txt
+++ b/source/blender/pointcache/CMakeLists.txt
@@ -45,6 +45,9 @@ set(SRC
 
        util/util_error_handler.h
        util/util_error_handler.cpp
+       util/util_task.cpp
+       util/util_task.h
+       util/util_thread.h
        util/util_types.h
 
        PTC_api.h
diff --git a/source/blender/pointcache/util/util_function.h 
b/source/blender/pointcache/util/util_function.h
new file mode 100644
index 0000000..97f0278
--- /dev/null
+++ b/source/blender/pointcache/util/util_function.h
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2015, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef PTC_UTIL_FUNCTION
+#define PTC_UTIL_FUNCTION
+
+#if __cplusplus > 199711L
+
+#include <functional>
+
+namespace PTC {
+
+using std::function;
+using namespace std::placeholders;
+#define function_bind std::bind
+
+}  /* namespace PTC */
+
+#else
+
+#include <boost/bind.hpp>
+#include <boost/function.hpp>
+
+namespace PTC {
+
+using boost::function;
+#define function_bind boost::bind
+
+}  /* namespace PTC */
+
+#endif
+
+#endif  /* PTC_UTIL_FUNCTION */
diff --git a/source/blender/pointcache/util/util_task.cpp 
b/source/blender/pointcache/util/util_task.cpp
new file mode 100644
index 0000000..feecdd5
--- /dev/null
+++ b/source/blender/pointcache/util/util_task.cpp
@@ -0,0 +1,87 @@
+/*
+ * Copyright 2015, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#include "util_task.h"
+
+#include "BLI_task.h"
+#include "BLI_threads.h"
+
+namespace PTC {
+
+class UtilTask {
+public:
+       explicit UtilTask(const UtilTaskFunction& run) : run_(run) {}
+       void run()
+       {
+               run_();
+       }
+protected:
+       UtilTaskFunction run_;
+};
+
+static void task_function(TaskPool * /*pool*/,
+                          void *taskdata,
+                          int /*threadid*/)
+{
+       UtilTask *task = reinterpret_cast<UtilTask*>(taskdata);
+       task->run();
+       delete task;
+}
+
+UtilTaskPool::UtilTaskPool()
+{
+       scheduler_ = BLI_task_scheduler_get();
+       pool_ = BLI_task_pool_create(scheduler_, NULL);
+}
+
+UtilTaskPool::~UtilTaskPool()
+{
+       BLI_task_pool_free(pool_);
+}
+
+void UtilTaskPool::push(const UtilTaskFunction& run, bool front)
+{
+       UtilTask *task = new UtilTask(run);
+       BLI_task_pool_push(pool_,
+                          task_function,
+                          task,
+                          false,
+                          front? TASK_PRIORITY_HIGH: TASK_PRIORITY_LOW);
+}
+
+void UtilTaskPool::wait_work()
+{
+       BLI_task_pool_work_and_wait(pool_);
+}
+
+void UtilTaskPool::cancel()
+{
+       BLI_task_pool_cancel(pool_);
+}
+
+void UtilTaskPool::stop()
+{
+       BLI_task_pool_stop(pool_);
+}
+
+bool UtilTaskPool::cancelled()
+{
+       return BLI_task_pool_canceled(pool_);
+}
+
+}  /* namespace PTC */
diff --git a/source/blender/pointcache/util/util_task.h 
b/source/blender/pointcache/util/util_task.h
new file mode 100644
index 0000000..be2f1cc
--- /dev/null
+++ b/source/blender/pointcache/util/util_task.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright 2015, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef PTC_UTIL_TASK_H
+#define PTC_UTIL_TASK_H
+
+#include "util_function.h"
+
+struct TaskScheduler;
+struct TaskPool;
+
+namespace PTC {
+
+typedef function<void(void)> UtilTaskFunction;
+
+class UtilTaskPool {
+public:
+       UtilTaskPool();
+       ~UtilTaskPool();
+
+       void push(const UtilTaskFunction& run, bool front = false);
+
+       void wait_work();
+       void cancel();
+       void stop();
+
+       bool cancelled();
+
+protected:
+       struct TaskScheduler *scheduler_;
+       struct TaskPool *pool_;
+};
+
+}  /* namespace PTC */
+
+#endif  /* PTC_UTIL_TASK_H */
diff --git a/source/blender/pointcache/util/util_thread.h 
b/source/blender/pointcache/util/util_thread.h
new file mode 100644
index 0000000..6b41a35
--- /dev/null
+++ b/source/blender/pointcache/util/util_thread.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright 2015, Blender Foundation.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+#ifndef PTC_UTIL_THREAD_H
+#define PTC_UTIL_THREAD_H
+
+#include "BLI_threads.h"
+
+namespace PTC {
+
+class thread_mutex {
+public:
+       thread_mutex()
+       {
+               BLI_mutex_init(&mutex_);
+       }
+
+       ~thread_mutex()
+       {
+               BLI_mutex_end(&mutex_);
+       }
+
+       void lock()
+       {
+               BLI_mutex_lock(&mutex_);
+       }
+
+       bool trylock()
+       {
+               return BLI_mutex_trylock(&mutex_);
+       }
+
+       void unlock()
+       {
+               BLI_mutex_unlock(&mutex_);
+       }
+
+protected:
+       ThreadMutex mutex_;
+};
+
+class thread_scoped_lock {
+public:
+       explicit thread_scoped_lock(thread_mutex& mutex)
+         : mutex_(mutex)
+       {
+               mutex_.lock();
+       }
+
+       ~thread_scoped_lock() {
+               mutex_.unlock();
+       }
+protected:
+       thread_mutex& mutex_;
+};
+
+}  /* namespace PTC */
+
+#endif  /* PTC_UTIL_THREAD_H */

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to