diff --git a/CMakeLists.txt b/CMakeLists.txt index 1deb880..32601c1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -208,6 +208,18 @@ if(BUILD_TESTING) target_link_libraries(expected_eval_errors_test ${LIBS}) add_test(NAME Expected_Eval_Errors_Test COMMAND expected_eval_errors_test) + if (MULTITHREAD_SUPPORT_ENABLED) + add_executable(multithreaded_test unittests/multithreaded_test.cpp) + target_link_libraries(multithreaded_test ${LIBS}) + add_test(NAME Multithreaded_Test COMMAND multithreaded_test) + set_property(TEST Multithreaded_Test + PROPERTY ENVIRONMENT + "CHAI_USE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/unittests/" + "CHAI_MODULE_PATH=${CMAKE_CURRENT_BINARY_DIR}/" + ) + + endif() + add_executable(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp unittests/multifile_test_module.cpp) diff --git a/src/multithreaded.cpp b/src/multithreaded.cpp deleted file mode 100644 index 2a540b8..0000000 --- a/src/multithreaded.cpp +++ /dev/null @@ -1,45 +0,0 @@ -// This file is distributed under the BSD License. -// See "license.txt" for details. -// Copyright 2009-2010, Jonathan Turner (jonathan@emptycrate.com) -// and Jason Turner (jason@emptycrate.com) -// http://www.chaiscript.com - -#include - -#include - -#include -#include - - - - -void do_work(chaiscript::ChaiScript &c) -{ -// c("use(\"work.chai\"); do_chai_work(num_iterations);"); - std::stringstream ss; - ss << "MyVar" << rand(); - c.add(chaiscript::var(5), ss.str()); - c("use(\"work.chai\"); do_chai_work(10000);"); -} - -int main(int argc, char *argv[]) { - std::string input; - chaiscript::ChaiScript chai; - - //chai.add_shared_object(chaiscript::Boxed_Value(10000), "num_iterations"); - - std::vector > threads; - - for (int i = 0; i < argc - 1; ++i) - { -// std::thread t(&do_work, std::ref(chai)); - threads.push_back(std::shared_ptr(new std::thread(&do_work, std::ref(chai)))); - } - - for (int i = 0; i < argc - 1; ++i) - { - threads[i]->join(); - } -} - diff --git a/src/work.chai b/src/work.chai deleted file mode 100644 index c17470c..0000000 --- a/src/work.chai +++ /dev/null @@ -1,10 +0,0 @@ -def do_chai_work(num_iters) -{ - var i = 0; - for (var k = 0; k + +#include + +#include + +int expected_value(int num_iters) +{ + int i = 0; + for (int k = 0; k usepaths; + usepaths.push_back(""); + if (usepath) + { + usepaths.push_back(usepath); + } + + std::vector modulepaths; + modulepaths.push_back(""); + if (modulepath) + { + modulepaths.push_back(modulepath); + } + + chaiscript::ChaiScript chai(modulepaths,usepaths); + + std::vector > threads; + + // Ensure at least two, but say only 7 on an 8 core processor + int num_threads = std::max(std::thread::hardware_concurrency() - 1, 2u); + + for (int i = 0; i < num_threads; ++i) + { + threads.push_back(std::shared_ptr(new std::thread(do_work, std::ref(chai), i))); + } + + for (int i = 0; i < num_threads; ++i) + { + threads[i]->join(); + } + + + for (int i = 0; i < num_threads; ++i) + { + std::stringstream ss; + ss << i; + if (chai.eval("getvalue(" + ss.str() + ")") != expected_value(4000)) + { + return EXIT_FAILURE; + } + + if (chai.eval("getid(" + ss.str() + ")") != i) + { + return EXIT_FAILURE; + } + } + + return EXIT_SUCCESS; +} + diff --git a/unittests/multithreaded_work.inc b/unittests/multithreaded_work.inc new file mode 100644 index 0000000..7035423 --- /dev/null +++ b/unittests/multithreaded_work.inc @@ -0,0 +1,15 @@ +def do_chai_work(num_iters, id) +{ + var i = 0; + for (var k = 0; k