2011-04-27 11:58:34 +00:00
|
|
|
/*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Copyright 2011-2013 Blender Foundation
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-04-27 11:58:34 +00:00
|
|
|
*
|
2013-08-18 14:16:15 +00:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
2014-12-25 01:50:24 +00:00
|
|
|
* limitations under the License.
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_THREAD_H__
|
|
|
|
#define __UTIL_THREAD_H__
|
|
|
|
|
2018-07-25 14:59:46 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <functional>
|
2020-03-19 08:33:03 +00:00
|
|
|
#include <mutex>
|
2011-04-27 11:58:34 +00:00
|
|
|
#include <queue>
|
2020-03-19 08:33:03 +00:00
|
|
|
#include <thread>
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2018-03-28 09:31:51 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include "util_windows.h"
|
|
|
|
#else
|
|
|
|
# include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
2020-07-02 15:05:34 +00: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 07:16:46 +00:00
|
|
|
|
Cycles: Make all #include statements relative to cycles source directory
The idea is to make include statements more explicit and obvious where the
file is coming from, additionally reducing chance of wrong header being
picked up.
For example, it was not obvious whether bvh.h was refferring to builder
or traversal, whenter node.h is a generic graph node or a shader node
and cases like that.
Surely this might look obvious for the active developers, but after some
time of not touching the code it becomes less obvious where file is coming
from.
This was briefly mentioned in T50824 and seems @brecht is fine with such
explicitness, but need to agree with all active developers before committing
this.
Please note that this patch is lacking changes related on GPU/OpenCL
support. This will be solved if/when we all agree this is a good idea to move
forward.
Reviewers: brecht, lukasstockner97, maiself, nirved, dingto, juicyfruit, swerner
Reviewed By: lukasstockner97, maiself, nirved, dingto
Subscribers: brecht
Differential Revision: https://developer.blender.org/D2586
2017-03-28 18:39:14 +00:00
|
|
|
#include "util/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 20:12:22 +00: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 13:37:57 +00: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:
|
2019-01-11 14:01:54 +00:00
|
|
|
/* NOTE: Node index of -1 means that affinity will be inherited from the
|
|
|
|
* parent thread and no override on top of that will happen. */
|
2018-12-27 18:01:19 +00:00
|
|
|
thread(function<void()> run_cb, int node = -1);
|
2016-06-03 23:29:13 +00:00
|
|
|
~thread();
|
2015-03-29 20:12:22 +00:00
|
|
|
|
2016-06-03 23:29:13 +00: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 11:01:38 +00:00
|
|
|
function<void()> run_cb_;
|
2019-02-14 13:37:57 +00:00
|
|
|
#ifdef __APPLE__
|
|
|
|
pthread_t pthread_id;
|
|
|
|
#else
|
|
|
|
std::thread std_thread;
|
|
|
|
#endif
|
2016-06-03 23:29:13 +00:00
|
|
|
bool joined_;
|
2018-12-27 18:01:19 +00:00
|
|
|
int node_;
|
2012-02-04 19:58:09 +00:00
|
|
|
};
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2020-07-02 15:05:34 +00:00
|
|
|
using thread_spin_lock = tbb::spin_mutex;
|
2016-02-22 15:43:48 +00:00
|
|
|
|
2017-04-05 12:57:34 +00: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__ */
|