From 4e33e969dc85377bbba8141b331689de34a5df9b Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 10 Jul 2012 13:27:47 -0600 Subject: [PATCH 1/3] Minor header file include cleanup --- include/chaiscript/language/chaiscript_engine.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/chaiscript/language/chaiscript_engine.hpp b/include/chaiscript/language/chaiscript_engine.hpp index 4f61f31..2539fa9 100644 --- a/include/chaiscript/language/chaiscript_engine.hpp +++ b/include/chaiscript/language/chaiscript_engine.hpp @@ -10,7 +10,7 @@ #include #include -#include +#include "chaiscript_common.hpp" #ifdef _POSIX_VERSION #include @@ -23,8 +23,8 @@ #endif -#include -#include +#include "chaiscript_prelude.hpp" +#include "chaiscript_parser.hpp" #include "../dispatchkit/exception_specification.hpp" namespace chaiscript From 9f309fcbe9f68bff13e54b3eeef14b1cfae086d6 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Tue, 10 Jul 2012 15:10:09 -0600 Subject: [PATCH 2/3] Add tests for multithreaded features #55 --- CMakeLists.txt | 12 ++++ src/multithreaded.cpp | 42 ------------- src/work.chai | 10 ---- unittests/multithreaded_test.cpp | 100 +++++++++++++++++++++++++++++++ unittests/multithreaded_work.inc | 15 +++++ 5 files changed, 127 insertions(+), 52 deletions(-) delete mode 100644 src/multithreaded.cpp delete mode 100644 src/work.chai create mode 100644 unittests/multithreaded_test.cpp create mode 100644 unittests/multithreaded_work.inc diff --git a/CMakeLists.txt b/CMakeLists.txt index 8f21280..d876061 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -196,6 +196,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) + endif() + + set_property(TEST Multithreaded_Test + PROPERTY ENVIRONMENT + "CHAI_USE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/unittests/" + "CHAI_MODULE_PATH=${CMAKE_CURRENT_BINARY_DIR}/" + ) + 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 595511f..0000000 --- a/src/multithreaded.cpp +++ /dev/null @@ -1,42 +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) - { - threads.push_back(boost::shared_ptr(new boost::thread(boost::bind(do_work, boost::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 +#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(boost::thread::hardware_concurrency() - 1, 2u); + + for (int i = 0; i < num_threads; ++i) + { + threads.push_back(boost::shared_ptr(new boost::thread(boost::bind(do_work, boost::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 Date: Fri, 13 Jul 2012 13:14:48 -0600 Subject: [PATCH 3/3] Correct building on threadless --- CMakeLists.txt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d876061..5674bae 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -200,13 +200,13 @@ if(BUILD_TESTING) add_executable(multithreaded_test unittests/multithreaded_test.cpp) target_link_libraries(multithreaded_test ${LIBS}) add_test(NAME Multithreaded_Test COMMAND multithreaded_test) - endif() + set_property(TEST Multithreaded_Test + PROPERTY ENVIRONMENT + "CHAI_USE_PATH=${CMAKE_CURRENT_SOURCE_DIR}/unittests/" + "CHAI_MODULE_PATH=${CMAKE_CURRENT_BINARY_DIR}/" + ) - 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