2013-12-10 00:55:13 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2014-09-24 19:51:04 +00:00
|
|
|
#include <vector>
|
2013-12-10 00:55:13 +00:00
|
|
|
#include "util/thread.h"
|
|
|
|
|
|
|
|
namespace lean {
|
|
|
|
#if defined(LEAN_USE_BOOST)
|
2014-09-23 15:00:21 +00:00
|
|
|
static boost::thread::attributes * g_thread_attributes = nullptr;
|
|
|
|
void initialize_thread() {
|
|
|
|
g_thread_attributes = new boost::thread::attributes();
|
|
|
|
g_thread_attributes->set_stack_size(8192*1024); // 8Mb
|
|
|
|
}
|
|
|
|
void finalize_thread() {
|
|
|
|
delete g_thread_attributes;
|
|
|
|
}
|
2013-12-10 00:55:13 +00:00
|
|
|
void set_thread_stack_size(size_t sz) {
|
2014-09-23 15:00:21 +00:00
|
|
|
g_thread_attributes->set_stack_size(sz);
|
2013-12-10 00:55:13 +00:00
|
|
|
}
|
|
|
|
boost::thread::attributes const & get_thread_attributes() {
|
2014-09-23 15:00:21 +00:00
|
|
|
return *g_thread_attributes;
|
2013-12-10 00:55:13 +00:00
|
|
|
}
|
2014-09-23 15:00:21 +00:00
|
|
|
#else
|
|
|
|
void initialize_thread() {}
|
|
|
|
void finalize_thread() {}
|
2013-12-10 00:55:13 +00:00
|
|
|
#endif
|
2014-09-24 19:51:04 +00:00
|
|
|
|
|
|
|
typedef std::vector<thread_finalizer> thread_finalizers;
|
|
|
|
LEAN_THREAD_PTR(thread_finalizers, g_finalizers);
|
|
|
|
LEAN_THREAD_PTR(thread_finalizers, g_post_finalizers);
|
|
|
|
|
|
|
|
void register_thread_finalizer(thread_finalizer fn) {
|
|
|
|
if (!g_finalizers)
|
|
|
|
g_finalizers = new thread_finalizers();
|
|
|
|
g_finalizers->push_back(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void register_post_thread_finalizer(thread_finalizer fn) {
|
|
|
|
if (!g_post_finalizers)
|
|
|
|
g_post_finalizers = new thread_finalizers();
|
|
|
|
g_post_finalizers->push_back(fn);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_thread_finalizers(thread_finalizers * fns) {
|
|
|
|
if (fns) {
|
|
|
|
unsigned i = fns->size();
|
|
|
|
while (i > 0) {
|
|
|
|
--i;
|
|
|
|
auto fn = (*fns)[i];
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
delete fns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_thread_finalizers() {
|
|
|
|
run_thread_finalizers(g_finalizers);
|
|
|
|
}
|
|
|
|
|
|
|
|
void run_post_thread_finalizers() {
|
|
|
|
run_thread_finalizers(g_post_finalizers);
|
|
|
|
}
|
2013-12-10 00:55:13 +00:00
|
|
|
}
|