2022-02-11 13:53:21 +01:00
|
|
|
/* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
* Copyright 2011-2022 Blender Foundation */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
|
#ifndef __UTIL_THREAD_H__
|
|
|
|
|
#define __UTIL_THREAD_H__
|
|
|
|
|
|
2018-07-25 16:59:46 +02:00
|
|
|
#include <condition_variable>
|
|
|
|
|
#include <functional>
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <mutex>
|
2011-04-27 11:58:34 +00:00
|
|
|
#include <queue>
|
2020-03-19 09:33:03 +01:00
|
|
|
#include <thread>
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2018-03-28 11:31:51 +02:00
|
|
|
#ifdef _WIN32
|
2021-10-24 14:19:19 +02:00
|
|
|
# include "util/windows.h"
|
2018-03-28 11:31:51 +02:00
|
|
|
#else
|
|
|
|
|
# include <pthread.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-07-02 17:05:34 +02:00
|
|
|
/* NOTE: Use tbb/spin_mutex.h instead of util_tbb.h because some of the TBB
|
|
|
|
|
* functionality requires RTTI, which is disabled for OSL kernel. */
|
|
|
|
|
#include <tbb/spin_mutex.h>
|
2016-04-01 09:16:46 +02:00
|
|
|
|
2021-10-24 14:19:19 +02:00
|
|
|
#include "util/function.h"
|
2012-02-04 19:58:09 +00:00
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
|
2015-03-29 22:12:22 +02:00
|
|
|
typedef std::mutex thread_mutex;
|
|
|
|
|
typedef std::unique_lock<std::mutex> thread_scoped_lock;
|
|
|
|
|
typedef std::condition_variable thread_condition_variable;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2019-02-14 14:37:57 +01:00
|
|
|
/* Own thread implementation similar to std::thread, so we can set a
|
|
|
|
|
* custom stack size on macOS. */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
class thread {
|
|
|
|
|
public:
|
2022-01-07 11:46:31 +01:00
|
|
|
thread(function<void()> run_cb);
|
2016-06-04 01:29:13 +02:00
|
|
|
~thread();
|
2015-03-29 22:12:22 +02:00
|
|
|
|
2016-06-04 01:29:13 +02:00
|
|
|
static void *run(void *arg);
|
|
|
|
|
bool join();
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
protected:
|
2018-11-09 12:01:38 +01:00
|
|
|
function<void()> run_cb_;
|
2019-02-14 14:37:57 +01:00
|
|
|
#ifdef __APPLE__
|
|
|
|
|
pthread_t pthread_id;
|
|
|
|
|
#else
|
|
|
|
|
std::thread std_thread;
|
|
|
|
|
#endif
|
2016-06-04 01:29:13 +02:00
|
|
|
bool joined_;
|
2012-02-04 19:58:09 +00:00
|
|
|
};
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2020-07-02 17:05:34 +02:00
|
|
|
using thread_spin_lock = tbb::spin_mutex;
|
2016-02-22 16:43:48 +01:00
|
|
|
|
2017-04-05 14:57:34 +02:00
|
|
|
class thread_scoped_spin_lock {
|
|
|
|
|
public:
|
|
|
|
|
explicit thread_scoped_spin_lock(thread_spin_lock &lock) : lock_(lock)
|
|
|
|
|
{
|
|
|
|
|
lock_.lock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~thread_scoped_spin_lock()
|
|
|
|
|
{
|
|
|
|
|
lock_.unlock();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* TODO(sergey): Implement manual control over lock/unlock. */
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
thread_spin_lock &lock_;
|
|
|
|
|
};
|
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
|
|
#endif /* __UTIL_THREAD_H__ */
|