core: Fix use-after-free error on _threads

When smp::configure() is called from non-main thread, then the global
state which it allocates will be destroyed after reactor is destroyed,
because it will be destroyed from the main thread and the reactor will
be destroyed together with the thread which called
smp::configure(). This will result in SIGSEGV when allocator tries to
free _threads vector across CPU threads because the target CPU was
alrady freed. See issue #10.

To fix this, I introduced smp::cleanup() method which should cleanup
all global state and should be called in the same thread in which
smp::configure() was called.

I need to call smp::configure() from non-main thread for integration
with boost unit testing framework.
This commit is contained in:
Tomasz Grabiec
2015-01-28 15:10:44 +01:00
committed by Avi Kivity
parent 555977f5e6
commit 8a126b9088
3 changed files with 8 additions and 1 deletions

View File

@@ -70,7 +70,9 @@ app_template::run(int ac, char ** av, std::function<void ()>&& func) {
engine().exit(1);
}
});
return engine().run();
auto exit_code = engine().run();
smp::cleanup();
return exit_code;
}

View File

@@ -1266,6 +1266,10 @@ void smp::allocate_reactor() {
reactor_holder.reset(local_engine);
}
void smp::cleanup() {
smp::_threads = std::vector<thread_adaptor>();
}
void smp::configure(boost::program_options::variables_map configuration)
{
smp::count = 1;

View File

@@ -837,6 +837,7 @@ class smp {
public:
static boost::program_options::options_description get_options_description();
static void configure(boost::program_options::variables_map vm);
static void cleanup();
static void join_all();
static bool main_thread() { return std::this_thread::get_id() == _tmain; }