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
|
|
|
|
* limitations under the License
|
2011-04-27 11:58:34 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __UTIL_THREAD_H__
|
|
|
|
#define __UTIL_THREAD_H__
|
|
|
|
|
|
|
|
#include <boost/thread.hpp>
|
2012-02-04 19:58:09 +00:00
|
|
|
#include <pthread.h>
|
2011-04-27 11:58:34 +00:00
|
|
|
#include <queue>
|
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
#include "util_function.h"
|
|
|
|
|
2011-04-27 11:58:34 +00:00
|
|
|
CCL_NAMESPACE_BEGIN
|
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
/* use boost for mutexes */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
typedef boost::mutex thread_mutex;
|
|
|
|
typedef boost::mutex::scoped_lock thread_scoped_lock;
|
|
|
|
typedef boost::condition_variable thread_condition_variable;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
/* own pthread based implementation, to avoid boost version conflicts with
|
2012-06-09 17:22:52 +00:00
|
|
|
* dynamically loaded blender plugins */
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
class thread {
|
|
|
|
public:
|
|
|
|
thread(boost::function<void(void)> run_cb_)
|
|
|
|
{
|
|
|
|
joined = false;
|
|
|
|
run_cb = run_cb_;
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
pthread_create(&pthread_id, NULL, run, (void*)this);
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
~thread()
|
|
|
|
{
|
|
|
|
if(!joined)
|
|
|
|
join();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *run(void *arg)
|
|
|
|
{
|
2012-05-27 00:36:50 +00:00
|
|
|
((thread*)arg)->run_cb();
|
2012-02-04 19:58:09 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool join()
|
|
|
|
{
|
2012-10-09 14:28:29 +00:00
|
|
|
joined = true;
|
2012-02-04 19:58:09 +00:00
|
|
|
return pthread_join(pthread_id, NULL) == 0;
|
|
|
|
}
|
2011-04-27 11:58:34 +00:00
|
|
|
|
2012-02-04 19:58:09 +00:00
|
|
|
protected:
|
|
|
|
boost::function<void(void)> run_cb;
|
|
|
|
pthread_t pthread_id;
|
|
|
|
bool joined;
|
|
|
|
};
|
2011-04-27 11:58:34 +00:00
|
|
|
|
|
|
|
CCL_NAMESPACE_END
|
|
|
|
|
|
|
|
#endif /* __UTIL_THREAD_H__ */
|
|
|
|
|