From e2aeef314e3af7cbaf838eeec387af5fdee2be69 Mon Sep 17 00:00:00 2001 From: Marcus Winter Date: Tue, 24 Mar 2015 15:00:01 +0100 Subject: [PATCH] mtapi_cpp_ext: first revision of lower level mtapi_cpp library --- CMakeLists.txt | 1 + base_cpp/include/embb/base/core_set.h | 5 +++++ doc/reference/Doxyfile.in | 2 +- mtapi_cpp_ext/CMakeLists.txt | 44 ++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/action.h | 140 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/action_attributes.h | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/affinity.h | 139 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/group.h | 311 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/group_attributes.h | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/internal/check_status.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/job.h | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/mtapi.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/node.h | 299 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/node_attributes.h | 161 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/queue.h | 310 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/queue_attributes.h | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/status_exception.h | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/task.h | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/task_attributes.h | 121 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/include/embb/mtapi_ext/task_context.h | 147 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/src/check_status.cc | 203 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/src/node.cc | 35 +++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/main.cc | 42 ++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/mtapi_cpp_test_config.h | 36 ++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/mtapi_cpp_test_group.cc | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/mtapi_cpp_test_group.h | 40 ++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/mtapi_cpp_test_queue.cc | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/mtapi_cpp_test_queue.h | 40 ++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/mtapi_cpp_test_task.cc | 126 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ mtapi_cpp_ext/test/mtapi_cpp_test_task.h | 40 ++++++++++++++++++++++++++++++++++++++++ 30 files changed, 3188 insertions(+), 1 deletion(-) create mode 100644 mtapi_cpp_ext/CMakeLists.txt create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/action.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/action_attributes.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/affinity.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/group.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/group_attributes.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/internal/check_status.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/job.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/mtapi.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/node.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/node_attributes.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/queue.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/queue_attributes.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/status_exception.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/task.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/task_attributes.h create mode 100644 mtapi_cpp_ext/include/embb/mtapi_ext/task_context.h create mode 100644 mtapi_cpp_ext/src/check_status.cc create mode 100644 mtapi_cpp_ext/src/node.cc create mode 100644 mtapi_cpp_ext/test/main.cc create mode 100644 mtapi_cpp_ext/test/mtapi_cpp_test_config.h create mode 100644 mtapi_cpp_ext/test/mtapi_cpp_test_group.cc create mode 100644 mtapi_cpp_ext/test/mtapi_cpp_test_group.h create mode 100644 mtapi_cpp_ext/test/mtapi_cpp_test_queue.cc create mode 100644 mtapi_cpp_ext/test/mtapi_cpp_test_queue.h create mode 100644 mtapi_cpp_ext/test/mtapi_cpp_test_task.cc create mode 100644 mtapi_cpp_ext/test/mtapi_cpp_test_task.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 53a327e..fbbad63 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ add_subdirectory(base_c) add_subdirectory(base_cpp) add_subdirectory(mtapi_c) add_subdirectory(mtapi_cpp) +add_subdirectory(mtapi_cpp_ext) add_subdirectory(containers_cpp) add_subdirectory(algorithms_cpp) add_subdirectory(dataflow_cpp) diff --git a/base_cpp/include/embb/base/core_set.h b/base_cpp/include/embb/base/core_set.h index 74c1360..f5d9fec 100644 --- a/base_cpp/include/embb/base/core_set.h +++ b/base_cpp/include/embb/base/core_set.h @@ -184,6 +184,11 @@ class CoreSet { /** [IN] Core set on right-hand side of union operation */ ); + /** + * Provides access to internal representation to use it with C API. + */ + embb_core_set_t const & GetInternal() const { return rep_; } + private: /** * Internal representation of core set. diff --git a/doc/reference/Doxyfile.in b/doc/reference/Doxyfile.in index f1948b5..f429d66 100644 --- a/doc/reference/Doxyfile.in +++ b/doc/reference/Doxyfile.in @@ -148,7 +148,7 @@ INPUT = "@CMAKE_SOURCE_DIR@/doc/reference/embb.dox" \ "@CMAKE_SOURCE_DIR@/containers_cpp/include" \ "@CMAKE_SOURCE_DIR@/dataflow_cpp/include" \ "@CMAKE_SOURCE_DIR@/algorithms_cpp/include" \ - "@CMAKE_SOURCE_DIR@/mtapi_cpp/include" \ + "@CMAKE_SOURCE_DIR@/mtapi_cpp_ext/include" \ "@CMAKE_SOURCE_DIR@/base_cpp/include" \ "@CMAKE_SOURCE_DIR@/mtapi_c/include" \ "@CMAKE_SOURCE_DIR@/base_c/include" diff --git a/mtapi_cpp_ext/CMakeLists.txt b/mtapi_cpp_ext/CMakeLists.txt new file mode 100644 index 0000000..79c7433 --- /dev/null +++ b/mtapi_cpp_ext/CMakeLists.txt @@ -0,0 +1,44 @@ +project (project_mtapi_cpp_ext) + +file(GLOB_RECURSE EMBB_MTAPI_CPP_EXT_SOURCES "src/*.cc" "src/*.h") +file(GLOB_RECURSE EMBB_MTAPI_CPP_EXT_HEADERS "include/*.h") +file(GLOB_RECURSE EMBB_MTAPI_CPP_EXT_TEST_SOURCES "test/*.cc" "test/*.h") + +if (USE_AUTOMATIC_INITIALIZATION STREQUAL ON) + message("-- Automatic initialization enabled (default)") + set(MTAPI_CPP_AUTOMATIC_INITIALIZE 1) +else() + set(MTAPI_CPP_AUTOMATIC_INITIALIZE 0) + message("-- Automatic initialization disabled") +endif() +message(" (set with command line option -DUSE_AUTOMATIC_INITIALIZATION=ON/OFF)") + + +# Execute the GroupSources macro +include(${CMAKE_SOURCE_DIR}/CMakeCommon/GroupSourcesMSVC.cmake) +GroupSourcesMSVC(include) +GroupSourcesMSVC(src) +GroupSourcesMSVC(test) + +set (EMBB_MTAPI_CPP_EXT_INCLUDE_DIRS "include" "src" "test") +include_directories(${EMBB_MTAPI_CPP_EXT_INCLUDE_DIRS} + ${CMAKE_CURRENT_SOURCE_DIR}/../base_c/include + ${CMAKE_CURRENT_BINARY_DIR}/../base_c/include + ${CMAKE_CURRENT_SOURCE_DIR}/../base_cpp/include + ${CMAKE_CURRENT_BINARY_DIR}/../base_cpp/include + ${CMAKE_CURRENT_SOURCE_DIR}/../mtapi_c/include) + +add_library (embb_mtapi_cpp_ext ${EMBB_MTAPI_CPP_EXT_SOURCES} ${EMBB_MTAPI_CPP_EXT_HEADERS}) +target_link_libraries(embb_mtapi_cpp_ext embb_mtapi_c) + +if (BUILD_TESTS STREQUAL ON) + include_directories(${CMAKE_CURRENT_BINARY_DIR}/../partest/include) + add_executable (embb_mtapi_cpp_ext_test ${EMBB_MTAPI_CPP_EXT_TEST_SOURCES}) + target_link_libraries(embb_mtapi_cpp_ext_test embb_mtapi_cpp_ext embb_mtapi_c partest + embb_base_cpp embb_base_c ${compiler_libs}) + CopyBin(BIN embb_mtapi_cpp_ext_test DEST ${local_install_dir}) +endif() + +install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include/ + DESTINATION include FILES_MATCHING PATTERN "*.h") +install(TARGETS embb_mtapi_cpp_ext DESTINATION lib) diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/action.h b/mtapi_cpp_ext/include/embb/mtapi_ext/action.h new file mode 100644 index 0000000..1425595 --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/action.h @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_ACTION_H_ +#define EMBB_MTAPI_ACTION_H_ + +#include +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Holds the actual worker function used to execute a Task. + * + * \ingroup CPP_MTAPI_EXT + */ +class Action { + public: + /** + * Constructs an Action. + */ + Action( + mtapi_job_id_t job_id, /**< Job ID the Action belongs to */ + mtapi_action_function_t func, /**< The action function */ + const void * node_local_data, /**< Node local data available to all + Tasks using this Action */ + mtapi_size_t node_local_data_size, /**< Size of node local data */ + ActionAttributes const & attributes + /**< Attributes of the Action */ + ) { + Create(job_id, func, node_local_data, node_local_data_size, + &attributes.GetInternal()); + } + + /** + * Constructs an Action. + */ + Action( + mtapi_job_id_t job_id, /**< Job ID the Action belongs to */ + mtapi_action_function_t func, /**< The action function */ + const void * node_local_data, /**< Node local data available to all + Tasks using this Action */ + mtapi_size_t node_local_data_size /**< Size of node local data */ + ) { + Create(job_id, func, node_local_data, node_local_data_size, + MTAPI_DEFAULT_ACTION_ATTRIBUTES); + } + + /** + * Constructs an Action. + */ + Action( + mtapi_job_id_t job_id, /**< Job ID the Action belongs to */ + mtapi_action_function_t func, /**< The action function */ + ActionAttributes const & attributes + /**< Attributes of the Action */ + ) { + Create(job_id, func, MTAPI_NULL, 0, &attributes.GetInternal()); + } + + /** + * Constructs an Action. + */ + Action( + mtapi_job_id_t job_id, /**< Job ID the Action belongs to */ + mtapi_action_function_t func /**< The action function */ + ) { + Create(job_id, func, MTAPI_NULL, 0, MTAPI_DEFAULT_ACTION_ATTRIBUTES); + } + + /** + * Destroys an Action. + */ + ~Action() { + mtapi_action_delete(handle_, MTAPI_INFINITE, MTAPI_NULL); + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns The internal mtapi_action_hndl_t. + */ + mtapi_action_hndl_t GetInternal() const { + return handle_; + } + + private: + // no default constructor + Action(); + + // not copyable + Action(Action const & other); + void operator=(Action const & other); + + void Create( + mtapi_job_id_t job_id, + mtapi_action_function_t func, + const void * node_local_data, + mtapi_size_t node_local_data_size, + mtapi_action_attributes_t const * attributes + ) { + mtapi_status_t status; + handle_ = mtapi_action_create(job_id, func, node_local_data, node_local_data_size, + attributes, &status); + internal::CheckStatus(status); + } + + mtapi_action_hndl_t handle_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_ACTION_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/action_attributes.h b/mtapi_cpp_ext/include/embb/mtapi_ext/action_attributes.h new file mode 100644 index 0000000..410616f --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/action_attributes.h @@ -0,0 +1,120 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_ACTION_ATTRIBUTES_H_ +#define EMBB_MTAPI_ACTION_ATTRIBUTES_H_ + +#include +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Contains attributes of an Action. + * + * \ingroup CPP_MTAPI_EXT + */ +class ActionAttributes { +public: + /** + * Constructs an ActionAttributes object. + */ + ActionAttributes() { + mtapi_status_t status; + mtapi_actionattr_init(&attributes_, &status); + internal::CheckStatus(status); + } + + /** + * Sets the global property of an Action. + * This determines whether the object will be visible across nodes. + * + * \returns Reference to this object. + */ + ActionAttributes & SetGlobal( + bool state /**< The state to set */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_actionattr_set(&attributes_, MTAPI_ACTION_GLOBAL, + &st, sizeof(st), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the affinity of an Action. + * + * \returns Reference to this object. + */ + ActionAttributes & SetAffinity( + Affinity const & affinity /**< The Affinity to set. */ + ) { + mtapi_status_t status; + mtapi_affinity_t af = affinity.GetInternal(); + mtapi_actionattr_set(&attributes_, MTAPI_ACTION_AFFINITY, + &af, sizeof(af), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the domain shared property of an Action. + * This determines whether the object will be visible across domains. + * + * \returns Reference to this object. + */ + ActionAttributes & SetDomainShared( + bool state /**< The state to set */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_actionattr_set(&attributes_, MTAPI_ACTION_DOMAIN_SHARED, + &st, sizeof(st), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns A reference to the internal mtapi_action_attributes_t structure. + */ + mtapi_action_attributes_t const & GetInternal() const { + return attributes_; + } + +private: + mtapi_action_attributes_t attributes_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_ACTION_ATTRIBUTES_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/affinity.h b/mtapi_cpp_ext/include/embb/mtapi_ext/affinity.h new file mode 100644 index 0000000..b7c902c --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/affinity.h @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_AFFINITY_H_ +#define EMBB_MTAPI_AFFINITY_H_ + +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Describes the affinity of an Action or Task to a worker thread of a Node. + * + * \ingroup CPP_MTAPI_EXT + */ +class Affinity { +public: + /** + * Constructs an Affinity object. + */ + Affinity() { + Init(true); + } + + /** + * Copies an Affinity object. + */ + Affinity( + Affinity const & other /**< The Affinity to copy from */ + ) + : affinity_(other.affinity_) { + // empty + } + + /** + * Copies an Affinity object. + */ + void operator=( + Affinity const & other /**< The Affinity to copy from */ + ) { + affinity_ = other.affinity_; + } + + /** + * Constructs an Affinity object with the given initial affinity. + * If \c initial_affinity is \c true the Affinity will map to all worker + * threads, otherwise it will map to no worker threads. + */ + Affinity( + bool initial_affinity /**< The initial affinity to set. */ + ) { + Init(initial_affinity); + } + + /** + * Initializes an Affinity object with the given initial affinity. + * If \c initial_affinity is \c true the Affinity will map to all worker + * threads, otherwise it will map to no worker threads. + */ + void Init( + bool initial_affinity /**< The initial affinity to set. */ + ) { + mtapi_status_t status; + mtapi_boolean_t ia = initial_affinity ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_affinity_init(&affinity_, ia, &status); + internal::CheckStatus(status); + } + + /** + * Sets affinity to the given worker. + */ + void Set( + mtapi_uint_t worker, /**< The worker to set affinity to. */ + bool state /**< The state of the affinity. */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_affinity_set(&affinity_, worker, st, &status); + internal::CheckStatus(status); + } + + /** + * Gets affinity to the given worker. + * + * \returns \c true, if the Affinity maps to the worker, \c false otherwise. + */ + bool Get( + mtapi_uint_t worker /**< The worker to get affinity of. */ + ) { + mtapi_status_t status; + mtapi_boolean_t state = + mtapi_affinity_get(&affinity_, worker, &status); + internal::CheckStatus(status); + return (state != MTAPI_FALSE) ? true : false; + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns The internal mtapi_affinity_t. + */ + mtapi_affinity_t GetInternal() const { + return affinity_; + } + +private: + mtapi_affinity_t affinity_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_AFFINITY_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/group.h b/mtapi_cpp_ext/include/embb/mtapi_ext/group.h new file mode 100644 index 0000000..34253e8 --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/group.h @@ -0,0 +1,311 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_GROUP_H_ +#define EMBB_MTAPI_GROUP_H_ + +#include +#include +#include +#include +#include + +namespace embb { + +namespace base { + +class Allocation; + +} // namespace base + +namespace mtapi { + +/** + * Represents a facility to wait for multiple related + * \link Task Tasks\endlink. + * + * \ingroup CPP_MTAPI_EXT + */ +class Group { + public: + /** + * Constructs a Group object with default attributes. + * Requires an initialized Node. + */ + Group() { + Create(MTAPI_GROUP_ID_NONE, MTAPI_DEFAULT_GROUP_ATTRIBUTES); + } + + /** + * Constructs a Group object using the given Attributes. + * Requires an initialized Node. + */ + Group( + GroupAttributes const & attr /**< The GroupAttributes to use. */ + ) { + Create(MTAPI_GROUP_ID_NONE, &attr.GetInternal()); + } + + /** + * Constructs a Group object with default attributes and the given ID. + * Requires an initialized Node. + */ + Group( + mtapi_group_id_t id /**< A user defined ID of the Group. */ + ) { + Create(id, MTAPI_DEFAULT_GROUP_ATTRIBUTES); + } + + /** + * Constructs a Group object with given attributes and ID. + * Requires an initialized Node. + */ + Group( + mtapi_group_id_t id, /**< A user defined ID of the Group. */ + GroupAttributes const & attr /**< The GroupAttributes to use. */ + ) { + Create(id, &attr.GetInternal()); + } + + /** + * Destroys a Group object. + */ + ~Group() { + // delete the group, ignore status + mtapi_group_delete(handle_, MTAPI_NULL); + } + + /** + * Starts a new Task in this Group. + * + * \returns The handle to the started Task. + */ + template + Task Start( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes /**< Attributes of the Task */ + ) { + return Start(task_id, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal()); + } + + /** + * Starts a new Task in this Group. + * + * \returns The handle to the started Task. + */ + template + Task Start( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results /**< Pointer to the results. */ + ) { + return Start(task_id, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES); + } + + /** + * Starts a new Task in this Group. + * + * \returns The handle to the started Task. + */ + template + Task Start( + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes /**< Attributes of the Task */ + ) { + return Start(MTAPI_TASK_ID_NONE, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal()); + } + + /** + * Starts a new Task in this Group. + * + * \returns The handle to the started Task. + */ + template + Task Start( + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results /**< Pointer to the results. */ + ) { + return Start(MTAPI_TASK_ID_NONE, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES); + } + + /** + * Waits for any Task in the Group to finish for \c timeout milliseconds and + * retrieves the result buffer given in Start(). + * \return The status of the Task that finished execution, \c MTAPI_TIMEOUT + * or \c MTAPI_ERR_* + * \threadsafe + */ + mtapi_status_t WaitAny( + mtapi_timeout_t timeout, /**< [in] Timeout duration in + milliseconds */ + void ** result /**< [out] The result buffer given in + Node::Start, Group::Start or + Queue::Enqueue */ + ) { + mtapi_status_t status; + mtapi_group_wait_any(handle_, result, timeout, &status); + needs_delete_ = status != MTAPI_GROUP_COMPLETED; + return status; + } + + /** + * Waits for any Task in the Group to finish and + * retrieves the result buffer given in Start(). + * \return The status of the Task that finished execution or \c MTAPI_ERR_* + * \threadsafe + */ + mtapi_status_t WaitAny( + void ** result /**< [out] The result buffer given in + Node::Start, Group::Start or + Queue::Enqueue */ + ) { + return WaitAny(MTAPI_INFINITE, result); + } + + /** + * Waits for any Task in the Group to finish for \c timeout milliseconds. + * \return The status of the Task that finished execution + * \threadsafe + */ + mtapi_status_t WaitAny( + mtapi_timeout_t timeout /**< [in] Timeout duration in + milliseconds */ + ) { + return WaitAny(timeout, MTAPI_NULL); + } + + /** + * Waits for any Task in the Group to finish. + * \return The status of the Task that finished execution + * \threadsafe + */ + mtapi_status_t WaitAny() { + return WaitAny(MTAPI_INFINITE, MTAPI_NULL); + } + + /** + * Waits for all Task in the Group to finish for \c timeout milliseconds. + * \return \c MTAPI_SUCCESS, \c MTAPI_TIMEOUT, \c MTAPI_ERR_* or the status + * of any failed Task + * \threadsafe + */ + mtapi_status_t WaitAll( + mtapi_timeout_t timeout /**< [in] Timeout duration in + milliseconds */ + ) { + mtapi_status_t status; + mtapi_group_wait_all(handle_, timeout, &status); + needs_delete_ = status != MTAPI_SUCCESS; + return status; + } + + /** + * Waits for all Task in the Group to finish. + * \return \c MTAPI_SUCCESS, \c MTAPI_TIMEOUT, \c MTAPI_ERR_* or the status + * of any failed Task + * \threadsafe + */ + mtapi_status_t WaitAll() { + return WaitAll(MTAPI_INFINITE); + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns The internal mtapi_group_hndl_t. + */ + mtapi_group_hndl_t GetInternal() const { + return handle_; + } + + friend class embb::base::Allocation; + + private: + // not copyable + Group(Group const & other); + void operator=(Group const & other); + + void Create( + mtapi_group_id_t group_id, + mtapi_group_attributes_t const * attributes + ) { + needs_delete_ = false; + mtapi_status_t status; + handle_ = mtapi_group_create(group_id, attributes, &status); + internal::CheckStatus(status); + needs_delete_ = true; + } + + Task Start( + mtapi_task_id_t task_id, + mtapi_job_hndl_t job, + const void * arguments, + mtapi_size_t arguments_size, + void * results, + mtapi_size_t results_size, + mtapi_task_attributes_t const * attributes + ) { + mtapi_status_t status; + mtapi_task_hndl_t task_hndl = + mtapi_task_start(task_id, job, arguments, arguments_size, + results, results_size, attributes, handle_, + &status); + internal::CheckStatus(status); + return Task(task_hndl); + } + + mtapi_group_hndl_t handle_; + bool needs_delete_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_GROUP_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/group_attributes.h b/mtapi_cpp_ext/include/embb/mtapi_ext/group_attributes.h new file mode 100644 index 0000000..0aa76d1 --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/group_attributes.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_GROUP_ATTRIBUTES_H_ +#define EMBB_MTAPI_GROUP_ATTRIBUTES_H_ + +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Contains attributes of a Group. + * + * \ingroup CPP_MTAPI_EXT + */ +class GroupAttributes { +public: + /** + * Constructs a GroupAttributes object. + */ + GroupAttributes() { + mtapi_status_t status; + mtapi_groupattr_init(&attributes_, &status); + internal::CheckStatus(status); + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns A reference to the internal mtapi_group_attributes_t structure. + */ + mtapi_group_attributes_t const & GetInternal() const { + return attributes_; + } + +private: + mtapi_group_attributes_t attributes_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_GROUP_ATTRIBUTES_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/internal/check_status.h b/mtapi_cpp_ext/include/embb/mtapi_ext/internal/check_status.h new file mode 100644 index 0000000..5176a4d --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/internal/check_status.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_INTERNAL_CHECK_STATUS_H_ +#define EMBB_MTAPI_INTERNAL_CHECK_STATUS_H_ + +#include + +namespace embb { +namespace mtapi { +namespace internal { + +void CheckStatus(mtapi_status_t status); + +template +inline mtapi_size_t SizeOfType() { + return sizeof(T); +} + +template <> +inline mtapi_size_t SizeOfType() { + return 0u; +} + +} // namespace internal +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_INTERNAL_CHECK_STATUS_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/job.h b/mtapi_cpp_ext/include/embb/mtapi_ext/job.h new file mode 100644 index 0000000..9f9bdb9 --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/job.h @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_JOB_H_ +#define EMBB_MTAPI_JOB_H_ + +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Represents a collection of Actions. + * + * \ingroup CPP_MTAPI_EXT + */ +class Job { +public: + /** + * Constructs a Job. + * The Job object will be invalid. + */ + Job() { + handle_.id = 0; + handle_.tag = 0; + } + + /** + * Constructs a Job with the given \c job_id and \c domain_id. + * Requires an initialized Node. + */ + Job( + mtapi_job_id_t job_id, /**< Job ID to use. */ + mtapi_domain_t domain_id /**< Domain ID to use. */ + ) { + mtapi_status_t status; + handle_ = mtapi_job_get(job_id, domain_id, &status); + internal::CheckStatus(status); + } + + /** + * Copies a Job object. + */ + Job( + Job const & other /**< The Job to copy from */ + ) : handle_(other.handle_) { + // empty + } + + /** + * Copies a Job object. + */ + void operator=( + Job const & other /**< The Job to copy from */ + ) { + handle_ = other.handle_; + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns The internal mtapi_job_hndl_t. + */ + mtapi_job_hndl_t GetInternal() const { + return handle_; + } + +private: + mtapi_job_hndl_t handle_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_GROUP_ATTRIBUTES_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/mtapi.h b/mtapi_cpp_ext/include/embb/mtapi_ext/mtapi.h new file mode 100644 index 0000000..ab965af --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/mtapi.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_MTAPI_H_ +#define EMBB_MTAPI_MTAPI_H_ + +/** + * \defgroup CPP_MTAPI_EXT MTAPI EXT + * C++ wrapper around C implementation of MTAPI. + * For a description of the basic concepts, see the + * \ref C_MTAPI "C implementation of MTAPI". + * \ingroup CPP + */ + +#include +#include +#include +#include +#include +#include +#include + +#endif // EMBB_MTAPI_MTAPI_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/node.h b/mtapi_cpp_ext/include/embb/mtapi_ext/node.h new file mode 100644 index 0000000..20d76f2 --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/node.h @@ -0,0 +1,299 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_NODE_H_ +#define EMBB_MTAPI_NODE_H_ + +#include +#include +#include +#include +#include +#include +#include + +namespace embb { + +namespace base { + +class Allocation; + +} // namespace base + +namespace mtapi { + +/** + * A singleton representing the MTAPI runtime. + * + * \ingroup CPP_MTAPI_EXT + */ +class Node { + public: + /** + * Initializes the runtime singleton using default values: + * - all available cores will be used + * - maximum number of tasks is 1024 + * - maximum number of groups is 128 + * - maximum number of queues is 16 + * - maximum queue capacity is 1024 + * - maximum number of priorities is 4. + * + * \notthreadsafe + * \throws ErrorException if the singleton was already initialized or the + * Node could not be initialized. + * \memory Allocates about 200kb of memory. + */ + static void Initialize( + mtapi_domain_t domain_id, /**< [in] The domain id to use */ + mtapi_node_t node_id /**< [in] The node id to use */ + ) { + if (IsInitialized()) { + EMBB_THROW(StatusException, + "MTAPI: node was already initialized."); + } + else { + NodeAttributes attributes; // default attributes + node_instance_ = embb::base::Allocation::New( + domain_id, node_id, attributes); + } + } + + /** + * Initializes the runtime singleton. + * \notthreadsafe + * \throws ErrorException if the singleton was already initialized or the + * Node could not be initialized. + * \memory Allocates some memory depending on the values given. + */ + static void Initialize( + mtapi_domain_t domain_id, /**< [in] The domain id to use */ + mtapi_node_t node_id, /**< [in] The node id to use */ + NodeAttributes const & attributes /**< [in] Attributes to use */ + ) { + if (IsInitialized()) { + EMBB_THROW(StatusException, + "MTAPI: node was already initialized."); + } + else { + node_instance_ = embb::base::Allocation::New( + domain_id, node_id, attributes); + } + } + + /** + * Checks if runtime is initialized. + * \return \c true if the Node singleton is already initialized, false + * otherwise + * \waitfree + */ + static bool IsInitialized() { + return NULL != node_instance_; + } + + /** + * Gets the instance of the runtime system. + * \return Reference to the Node singleton + * \threadsafe + */ + static Node & GetInstance() { + if (IsInitialized()) { + return *node_instance_; + } + else { + EMBB_THROW(StatusException, + "MTAPI: node is not initialized."); + } + } + + /** + * Shuts the runtime system down. + * \throws ErrorException if the singleton is not initialized. + * \notthreadsafe + */ + static void Finalize() { + if (IsInitialized()) { + embb::base::Allocation::Delete(node_instance_); + node_instance_ = NULL; + } + else { + EMBB_THROW(StatusException, + "MTAPI: node is not initialized."); + } + } + + /** + * Returns the number of available cores. + * \return The number of available cores + * \waitfree + */ + mtapi_uint_t GetCoreCount() const { + return core_count_; + } + + /** + * Returns the number of worker threads. + * \return The number of worker threads. + * \waitfree + */ + mtapi_uint_t GetWorkerThreadCount() const { + return worker_thread_count_; + } + + /** + * Starts a new Task. + * + * \returns The handle to the started Task. + */ + template + Task Start( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes /**< Attributes of the Task */ + ) { + return Start(task_id, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal()); + } + + /** + * Starts a new Task. + * + * \returns The handle to the started Task. + */ + template + Task Start( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results /**< Pointer to the results. */ + ) { + return Start(task_id, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES); + } + + /** + * Starts a new Task. + * + * \returns The handle to the started Task. + */ + template + Task Start( + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes /**< Attributes of the Task */ + ) { + return Start(MTAPI_TASK_ID_NONE, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal()); + } + + /** + * Starts a new Task. + * + * \returns The handle to the started Task. + */ + template + Task Start( + Job const & job, /**< The Job to execute. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results /**< Pointer to the results. */ + ) { + return Start(MTAPI_TASK_ID_NONE, + job.GetInternal(), + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES); + } + + friend class embb::base::Allocation; + + private: + // not copyable + Node(Node const & node); + Node const & operator=(Node const & other); + + Node( + mtapi_domain_t domain_id, + mtapi_node_t node_id, + NodeAttributes const & attr) { + mtapi_status_t status; + mtapi_info_t info; + mtapi_initialize(domain_id, node_id, &attr.GetInternal(), &info, &status); + needs_finalize_ = status == MTAPI_SUCCESS; + internal::CheckStatus(status); + + core_count_ = info.hardware_concurrency; + worker_thread_count_ = embb_core_set_count( + &attr.GetInternal().core_affinity); + } + + ~Node() { + if (needs_finalize_) { + mtapi_status_t status; + mtapi_finalize(&status); + internal::CheckStatus(status); + } + } + + Task Start( + mtapi_task_id_t task_id, + mtapi_job_hndl_t job, + const void * arguments, + mtapi_size_t arguments_size, + void * results, + mtapi_size_t results_size, + mtapi_task_attributes_t const * attributes + ) { + mtapi_status_t status; + mtapi_task_hndl_t task_hndl = + mtapi_task_start(task_id, job, arguments, arguments_size, + results, results_size, attributes, MTAPI_GROUP_NONE, + &status); + internal::CheckStatus(status); + return Task(task_hndl); + } + + static embb::mtapi::Node * node_instance_; + + mtapi_uint_t core_count_; + mtapi_uint_t worker_thread_count_; + bool needs_finalize_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_NODE_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/node_attributes.h b/mtapi_cpp_ext/include/embb/mtapi_ext/node_attributes.h new file mode 100644 index 0000000..bc6f73a --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/node_attributes.h @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_NODE_ATTRIBUTES_H_ +#define EMBB_MTAPI_NODE_ATTRIBUTES_H_ + +#include +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Contains attributes of a Node. + * + * \ingroup CPP_MTAPI_EXT + */ +class NodeAttributes { +public: + /** + * Constructs a NodeAttributes object. + */ + NodeAttributes() { + mtapi_status_t status; + mtapi_nodeattr_init(&attributes_, &status); + internal::CheckStatus(status); + } + + /** + * Copies a NodeAttributes object. + */ + NodeAttributes( + NodeAttributes const & other /**< The NodeAttributes to copy. */ + ) + : attributes_(other.attributes_) { + // empty + } + + /** + * Copies a NodeAttributes object. + */ + void operator=( + NodeAttributes const & other /**< The NodeAttributes to copy. */ + ) { + attributes_ = other.attributes_; + } + + NodeAttributes & SetCoreAffinity(embb::base::CoreSet const & cores) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_CORE_AFFINITY, + &cores.GetInternal(), sizeof(embb_core_set_t), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetMaxTasks(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_MAX_TASKS, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetMaxActions(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_MAX_ACTIONS, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetMaxGroups(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_MAX_GROUPS, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetMaxQueues(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_MAX_QUEUES, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetQueueLimit(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_QUEUE_LIMIT, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetMaxJobs(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_MAX_JOBS, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetMaxActionsPerJob(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_MAX_ACTIONS_PER_JOB, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + NodeAttributes & SetMaxPriorities(mtapi_uint_t value) { + mtapi_status_t status; + mtapi_nodeattr_set(&attributes_, MTAPI_NODE_MAX_PRIORITIES, + &value, sizeof(value), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns A reference to the internal mtapi_node_attributes_t structure. + */ + mtapi_node_attributes_t const & GetInternal() const { + return attributes_; + } + +private: + mtapi_node_attributes_t attributes_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_NODE_ATTRIBUTES_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/queue.h b/mtapi_cpp_ext/include/embb/mtapi_ext/queue.h new file mode 100644 index 0000000..081c86c --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/queue.h @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_QUEUE_H_ +#define EMBB_MTAPI_QUEUE_H_ + +#include +#include +#include +#include +#include +#include + +namespace embb { + +namespace base { + +class Allocation; + +} // namespace base + +namespace mtapi { + +/** + * Allows for stream processing, either ordered or unordered. + * + * \ingroup CPP_MTAPI_EXT + */ +class Queue { + public: + /** + * Constructs a Queue with the given Job and default attributes. + * Requires an initialized Node. + */ + Queue( + Job const & job /**< The Job to use for the Queue. */ + ) { + Create(MTAPI_QUEUE_ID_NONE, job, MTAPI_DEFAULT_QUEUE_ATTRIBUTES); + } + + /** + * Constructs a Queue with the given Job and QueueAttributes. + * Requires an initialized Node. + */ + Queue( + Job const & job, /**< The Job to use for the Queue. */ + QueueAttributes const & attr /**< The attributes to use. */ + ) { + Create(MTAPI_QUEUE_ID_NONE, job, &attr.GetInternal()); + } + + /** + * Destroys a Queue object. + */ + ~Queue() { + mtapi_queue_delete(handle_, MTAPI_INFINITE, MTAPI_NULL); + } + + /** + * Enables the Queue. \link Task Tasks \endlink enqueued while the Queue was + * disabled are executed. + * \waitfree + */ + void Enable() { + mtapi_status_t status; + mtapi_queue_enable(handle_, &status); + internal::CheckStatus(status); + } + + /** + * Disables the Queue. Running \link Task Tasks \endlink are canceled. The + * Queue waits for the Tasks to finish for \c timout milliseconds. + * \waitfree + */ + void Disable( + mtapi_timeout_t timeout /**< The timeout in milliseconds. */ + ) { + mtapi_status_t status; + mtapi_queue_disable(handle_, timeout, &status); + internal::CheckStatus(status); + } + + /** + * Disables the Queue. Running \link Task Tasks \endlink are canceled. The + * Queue waits for the Tasks to finish. + * \waitfree + */ + void Disable() { + Disable(MTAPI_INFINITE); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes, /**< Attributes of the Task */ + Group const & group /**< The Group to start the Task in */ + ) { + return Enqueue(task_id, + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal(), group.GetInternal()); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + Group const & group /**< The Group to start the Task in */ + ) { + return Enqueue(task_id, + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES, group.GetInternal()); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes /**< Attributes of the Task */ + ) { + return Enqueue(task_id, + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal(), MTAPI_GROUP_NONE); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + mtapi_task_id_t task_id, /**< A user defined ID of the Task. */ + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results /**< Pointer to the results. */ + ) { + return Enqueue(task_id, + arguments, internal::SizeOfType(), + results, sizeofinternal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES, MTAPI_GROUP_NONE); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes, /**< Attributes of the Task */ + Group const & group /**< The Group to start the Task in */ + ) { + return Enqueue(MTAPI_TASK_ID_NONE, + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal(), group.GetInternal()); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + Group const & group /**< The Group to start the Task in */ + ) { + return Enqueue(MTAPI_TASK_ID_NONE, + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES, group.GetInternal()); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results, /**< Pointer to the results. */ + TaskAttributes const & attributes /**< Attributes of the Task */ + ) { + return Enqueue(MTAPI_TASK_ID_NONE, + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + &attributes.GetInternal(), MTAPI_GROUP_NONE); + } + + /** + * Enqueues a new Task. + * + * \returns The handle to the enqueued Task. + */ + template + Task Enqueue( + const ARGS * arguments, /**< Pointer to the arguments. */ + RES * results /**< Pointer to the results. */ + ) { + return Enqueue(MTAPI_TASK_ID_NONE, + arguments, internal::SizeOfType(), + results, internal::SizeOfType(), + MTAPI_DEFAULT_TASK_ATTRIBUTES, MTAPI_GROUP_NONE); + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns The internal mtapi_queue_hndl_t. + */ + mtapi_queue_hndl_t GetInternal() const { + return handle_; + } + + friend class embb::base::Allocation; + + private: + // no default constructor + Queue(); + + // not copyable + Queue(Queue const & other); + void operator=(Queue const & other); + + void Create( + mtapi_queue_id_t queue_id, + Job const & job, + mtapi_queue_attributes_t const * attributes + ) { + mtapi_status_t status; + handle_ = mtapi_queue_create(queue_id, job.GetInternal(), + attributes, &status); + internal::CheckStatus(status); + } + + Task Enqueue( + mtapi_task_id_t task_id, + const void * arguments, + mtapi_size_t arguments_size, + void * results, + mtapi_size_t results_size, + mtapi_task_attributes_t const * attributes, + mtapi_group_hndl_t group + ) { + mtapi_status_t status; + mtapi_task_hndl_t task_hndl = + mtapi_task_enqueue(task_id, handle_, arguments, arguments_size, + results, results_size, attributes, group, + &status); + internal::CheckStatus(status); + return Task(task_hndl); + } + + mtapi_queue_hndl_t handle_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_QUEUE_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/queue_attributes.h b/mtapi_cpp_ext/include/embb/mtapi_ext/queue_attributes.h new file mode 100644 index 0000000..e8d6558 --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/queue_attributes.h @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_QUEUE_ATTRIBUTES_H_ +#define EMBB_MTAPI_QUEUE_ATTRIBUTES_H_ + +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Contains attributes of a Queue. + * + * \ingroup CPP_MTAPI_EXT + */ +class QueueAttributes { +public: + /** + * Constructs a QueueAttributes object. + */ + QueueAttributes() { + mtapi_status_t status; + mtapi_queueattr_init(&attributes_, &status); + internal::CheckStatus(status); + } + + /** + * Sets the global property of a Queue. + * This determines whether the object will be visible across nodes. + * + * \returns Reference to this object. + */ + QueueAttributes & SetGlobal( + bool state /**< The state to set. */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_queueattr_set(&attributes_, MTAPI_QUEUE_GLOBAL, + &st, sizeof(st), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the ordered property of a Queue. + * If set to \c true, tasks enqueued will be executed in order. + * + * \returns Reference to this object. + */ + QueueAttributes & SetOrdered( + bool state /**< The state to set. */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_queueattr_set(&attributes_, MTAPI_QUEUE_ORDERED, + &st, sizeof(st), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the retain property of a Queue. + * If set to \c true, tasks will be retained while a queue is disabled. + * Otherwise the will be canceled. + * + * \returns Reference to this object. + */ + QueueAttributes & SetRetain( + bool state /**< The state to set. */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_queueattr_set(&attributes_, MTAPI_QUEUE_RETAIN, + &st, sizeof(st), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the domain shared property of a Queue. + * This determines whether the object will be visible across domains. + * + * \returns Reference to this object. + */ + QueueAttributes & SetDomainShared( + bool state /**< The state to set. */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_queueattr_set(&attributes_, MTAPI_QUEUE_DOMAIN_SHARED, + &st, sizeof(st), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the priority of a Queue. + * The priority influences the order in which tasks are chosen for execution. + * + * \returns Reference to this object. + */ + QueueAttributes & SetPriority( + mtapi_uint_t priority /**< The priority to set. */ + ) { + mtapi_status_t status; + mtapi_queueattr_set(&attributes_, MTAPI_QUEUE_PRIORITY, + &priority, sizeof(priority), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the limit (capacity) of a Queue. + * + * \returns Reference to this object. + */ + QueueAttributes & SetLimit( + mtapi_uint_t limit /**< The limit to set. */ + ) { + mtapi_status_t status; + mtapi_queueattr_set(&attributes_, MTAPI_QUEUE_LIMIT, + &limit, sizeof(limit), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns A reference to the internal mtapi_queue_attributes_t structure. + */ + mtapi_queue_attributes_t const & GetInternal() const { + return attributes_; + } + +private: + mtapi_queue_attributes_t attributes_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_QUEUE_ATTRIBUTES_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/status_exception.h b/mtapi_cpp_ext/include/embb/mtapi_ext/status_exception.h new file mode 100644 index 0000000..52e3d20 --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/status_exception.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_STATUS_EXCEPTION_H_ +#define EMBB_MTAPI_STATUS_EXCEPTION_H_ + +#include + +namespace embb { +namespace mtapi { + +/** + * Represents an MTAPI error state and is thrown by almost all mtapi_cpp + * methods. + * + * \ingroup CPP_MTAPI_EXT + */ +class StatusException : public embb::base::Exception { + public: + /** + * Constructs a StatusException. + */ + explicit StatusException( + const char* message /**< The message to use. */ + ) + : embb::base::Exception(message) { + // empty + } + + /** + * Returns the code of the exception. + */ + virtual int Code() const { return EMBB_ERROR; } +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_STATUS_EXCEPTION_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/task.h b/mtapi_cpp_ext/include/embb/mtapi_ext/task.h new file mode 100644 index 0000000..ba8ec9d --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/task.h @@ -0,0 +1,138 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_TASK_H_ +#define EMBB_MTAPI_TASK_H_ + +#include +#include + +namespace embb { +namespace mtapi { + +/** + * A Task represents a running Action of a specific Job. + * + * \ingroup CPP_MTAPI_EXT + */ +class Task { + public: + /** + * Constructs an invalid Task. + */ + Task() { + handle_.id = 0; + handle_.tag = 0; + } + + /** + * Copies a Task. + */ + Task( + Task const & other /**< The task to copy. */ + ) : handle_(other.handle_) { + // emtpy + } + + /** + * Copies a Task. + */ + void operator=( + Task const & other /**< The task to copy. */ + ) { + handle_ = other.handle_; + } + + /** + * Destroys a Task. + */ + ~Task() { + // empty + } + + /** + * Waits for Task to finish for \c timeout milliseconds. + * \return The status of the finished Task, \c MTAPI_TIMEOUT or + * \c MTAPI_ERR_* + * \threadsafe + */ + mtapi_status_t Wait( + mtapi_timeout_t timeout /**< [in] Timeout duration in + milliseconds */ + ) { + mtapi_status_t status; + mtapi_task_wait(handle_, timeout, &status); + return status; + } + + /** + * Waits for Task to finish. + * \return The status of the finished Task or \c MTAPI_ERR_* + * \threadsafe + */ + mtapi_status_t Wait() { + mtapi_status_t status; + mtapi_task_wait(handle_, MTAPI_INFINITE, &status); + return status; + } + + /** + * Signals the Task to cancel computation. + * \waitfree + */ + void Cancel() { + mtapi_status_t status; + mtapi_task_cancel(handle_, &status); + internal::CheckStatus(status); + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns The internal mtapi_task_hndl_t. + */ + mtapi_task_hndl_t GetInternal() const { + return handle_; + } + + private: + Task(mtapi_task_hndl_t handle) + : handle_(handle) { + // empty + } + + mtapi_task_hndl_t handle_; + + friend class Node; + friend class Group; + friend class Queue; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_TASK_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/task_attributes.h b/mtapi_cpp_ext/include/embb/mtapi_ext/task_attributes.h new file mode 100644 index 0000000..6db352a --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/task_attributes.h @@ -0,0 +1,121 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_TASK_ATTRIBUTES_H_ +#define EMBB_MTAPI_TASK_ATTRIBUTES_H_ + +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Contains attributes of a Task. + * + * \ingroup CPP_MTAPI_EXT + */ +class TaskAttributes { +public: + /** + * Constructs a TaskAttributes object. + */ + TaskAttributes() { + mtapi_status_t status; + mtapi_taskattr_init(&attributes_, &status); + internal::CheckStatus(status); + } + + /** + * Sets the detached property of a Task. + * If set to \c true, the started Task will have no handle and cannot be + * waited for. + * + * \returns Reference to this object. + */ + TaskAttributes & SetDetached( + bool state /**< The state to set. */ + ) { + mtapi_status_t status; + mtapi_boolean_t st = state ? MTAPI_TRUE : MTAPI_FALSE; + mtapi_taskattr_set(&attributes_, MTAPI_TASK_DETACHED, + &st, sizeof(st), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the priority of a Task. + * The priority influences the order in which tasks are chosen for execution. + * + * \returns Reference to this object. + */ + TaskAttributes & SetPriority( + mtapi_uint_t priority /**< The priority to set. */ + ) { + mtapi_status_t status; + mtapi_taskattr_set(&attributes_, MTAPI_TASK_PRIORITY, + &priority, sizeof(priority), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Sets the number of instances in a Task. + * The Task will be launched \c instances times. In the action function, + * the number of instances and the current instance can be queried from + * the TaskContext. + * + * \returns Reference to this object. + */ + TaskAttributes & SetInstances( + mtapi_uint_t instances /**< Number of instances to set. */ + ) { + mtapi_status_t status; + mtapi_taskattr_set(&attributes_, MTAPI_TASK_INSTANCES, + &instances, sizeof(instances), &status); + internal::CheckStatus(status); + return *this; + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns A reference to the internal mtapi_task_attributes_t structure. + */ + mtapi_task_attributes_t const & GetInternal() const { + return attributes_; + } + +private: + mtapi_task_attributes_t attributes_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_TASK_ATTRIBUTES_H_ diff --git a/mtapi_cpp_ext/include/embb/mtapi_ext/task_context.h b/mtapi_cpp_ext/include/embb/mtapi_ext/task_context.h new file mode 100644 index 0000000..6a43c6c --- /dev/null +++ b/mtapi_cpp_ext/include/embb/mtapi_ext/task_context.h @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef EMBB_MTAPI_TASK_CONTEXT_H_ +#define EMBB_MTAPI_TASK_CONTEXT_H_ + +#include +#include + +namespace embb { +namespace mtapi { + +/** + * Provides information about the status of the currently running Task. + * + * \ingroup CPP_MTAPI_EXT + */ +class TaskContext { + public: + /** + * Constructs a TaskContext from the given C representation. + */ + explicit TaskContext( + mtapi_task_context_t * task_context + /**< The C task context to wrap. */ + ) + : context_(task_context) { + // empty + } + + /** + * Queries whether the Task running in the TaskContext should finish. + * \return \c true if the Task should finish, otherwise \c false + * \notthreadsafe + */ + bool ShouldCancel() { + return MTAPI_TASK_CANCELLED == GetTaskState(); + } + + /** + * Queries the current Task state. + * \return The current Task state. + * \notthreadsafe + */ + mtapi_task_state_t GetTaskState() { + mtapi_status_t status; + mtapi_task_state_t result = + mtapi_context_taskstate_get(context_, &status); + internal::CheckStatus(status); + return result; + } + + /** + * Queries the index of the worker thread the Task is running on. + * \return The worker thread index the Task is running on + * \notthreadsafe + */ + mtapi_uint_t GetCurrentWorkerNumber() { + mtapi_status_t status; + mtapi_uint_t result = mtapi_context_corenum_get(context_, &status); + internal::CheckStatus(status); + return result; + } + + /** + * Queries the current instance of the currently executing Task. + * \return The current instance number + * \notthreadsafe + */ + mtapi_uint_t GetInstanceNumber() { + mtapi_status_t status; + mtapi_uint_t result = mtapi_context_instnum_get(context_, &status); + internal::CheckStatus(status); + return result; + } + + /** + * Queries the number of instances of the currently executing Task. + * \return The number of instances + * \notthreadsafe + */ + mtapi_uint_t GetNumberOfInstances() { + mtapi_status_t status; + mtapi_uint_t result = mtapi_context_numinst_get(context_, &status); + internal::CheckStatus(status); + return result; + } + + /** + * Sets the return status of the running Task. This will be returned by + * Task::Wait() and is set to \c MTAPI_SUCCESS by default. + * \notthreadsafe + */ + void SetStatus( + mtapi_status_t error_code /**< [in] The status to return by + Task::Wait(), Group::WaitAny(), + Group::WaitAll() */ + ) { + mtapi_status_t status; + mtapi_context_status_set(context_, error_code, &status); + internal::CheckStatus(status); + } + + /** + * Returns the internal representation of this object. + * Allows for interoperability with the C interface. + * + * \returns A pointer to a mtapi_task_context_t. + */ + mtapi_task_context_t * GetInternal() const { + return context_; + } + + private: + // no default constructor + TaskContext(); + + mtapi_task_context_t * context_; +}; + +} // namespace mtapi +} // namespace embb + +#endif // EMBB_MTAPI_TASK_CONTEXT_H_ diff --git a/mtapi_cpp_ext/src/check_status.cc b/mtapi_cpp_ext/src/check_status.cc new file mode 100644 index 0000000..6f20c88 --- /dev/null +++ b/mtapi_cpp_ext/src/check_status.cc @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include +#include +#include + +namespace embb { +namespace mtapi { +namespace internal { + +void CheckStatus(mtapi_status_t status) { + if (MTAPI_SUCCESS != status) { + switch (status) { + case MTAPI_SUCCESS: + case MTAPI_TIMEOUT: + case MTAPI_GROUP_COMPLETED: + // these are no errors + break; + + case MTAPI_ERR_NODE_INITIALIZED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: node already initialized."); + break; + case MTAPI_ERR_NODE_NOTINIT: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: node not initialized."); + break; + case MTAPI_ERR_PARAMETER: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: invalid parameter."); + break; + case MTAPI_ERR_CORE_NUM: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: invalid worker number."); + break; + case MTAPI_ERR_RUNTIME_LOADBALANCING_NOTSUPPORTED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: load balancing not supported."); + break; + case MTAPI_ERR_RUNTIME_REMOTETASKS_NOTSUPPORTED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: remote tasks not supported."); + break; + case MTAPI_ERR_ARG_NOT_IMPLEMENTED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: argument not implemented."); + break; + case MTAPI_ERR_FUNC_NOT_IMPLEMENTED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: function not implemented."); + break; + case MTAPI_ERR_WAIT_PENDING: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: wait pending."); + break; + case MTAPI_ERR_ARG_SIZE: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: argument size mismatch."); + break; + case MTAPI_ERR_RESULT_SIZE: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: result buffer size mismatch."); + break; + case MTAPI_ERR_BUFFER_SIZE: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: buffer size mismatch."); + break; + case MTAPI_ERR_GROUP_LIMIT: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: group limit exceeded."); + break; + case MTAPI_ERR_GROUP_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: group invalid."); + break; + case MTAPI_ERR_QUEUE_LIMIT: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: queue limit exceeded."); + break; + case MTAPI_ERR_QUEUE_DISABLED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: queue disabled."); + break; + case MTAPI_ERR_QUEUE_DELETED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: queue deleted."); + break; + case MTAPI_ERR_QUEUE_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: queue invalid."); + break; + case MTAPI_ERR_JOB_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: job invalid."); + break; + case MTAPI_ERR_TASK_LIMIT: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: task limit exceeded."); + break; + case MTAPI_ERR_TASK_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: task invalid."); + break; + case MTAPI_ERR_CONTEXT_OUTOFCONTEXT: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: task context used outside of worker thread."); + break; + case MTAPI_ERR_CONTEXT_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: task context invalid."); + break; + case MTAPI_ERR_ACTION_DISABLED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: action disabled."); + break; + case MTAPI_ERR_ACTION_DELETED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: action deleted."); + break; + case MTAPI_ERR_ACTION_CANCELLED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: action canceled."); + break; + case MTAPI_ERR_ACTION_FAILED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: action failed."); + break; + case MTAPI_ERR_ACTION_LIMIT: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: action limit exceeded."); + break; + case MTAPI_ERR_ACTION_EXISTS: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: action already exists."); + break; + case MTAPI_ERR_ACTION_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: action invalid."); + break; + case MTAPI_ERR_DOMAIN_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: domain invalid."); + break; + case MTAPI_ERR_NODE_INVALID: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: node invalid."); + break; + case MTAPI_ERR_NODE_INITFAILED: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: node initialization failed."); + break; + case MTAPI_ERR_ATTR_SIZE: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: attribute size mismatch."); + break; + case MTAPI_ERR_ATTR_NUM: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: invalid attribute."); + break; + case MTAPI_ERR_ATTR_READONLY: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: attribute is read only."); + break; + + case MTAPI_ERR_ACTION_NUM_INVALID: + case MTAPI_ERR_UNKNOWN: + default: + EMBB_THROW(embb::mtapi::StatusException, + "MTAPI: unknown error."); + break; + } + } +} + +} // namespace internal +} // namespace mtapi +} // namespace embb diff --git a/mtapi_cpp_ext/src/node.cc b/mtapi_cpp_ext/src/node.cc new file mode 100644 index 0000000..1b04b5a --- /dev/null +++ b/mtapi_cpp_ext/src/node.cc @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +namespace embb { +namespace mtapi { + +embb::mtapi::Node * embb::mtapi::Node::node_instance_ = NULL; + +} // namespace mtapi +} // namespace embb diff --git a/mtapi_cpp_ext/test/main.cc b/mtapi_cpp_ext/test/main.cc new file mode 100644 index 0000000..4cd6b97 --- /dev/null +++ b/mtapi_cpp_ext/test/main.cc @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include + +#include +#include +#include + + +PT_MAIN("MTAPI C++") { + embb_thread_set_max_count(1024); + + PT_RUN(TaskTest); + PT_RUN(GroupTest); + PT_RUN(QueueTest); +} diff --git a/mtapi_cpp_ext/test/mtapi_cpp_test_config.h b/mtapi_cpp_ext/test/mtapi_cpp_test_config.h new file mode 100644 index 0000000..cde1a1a --- /dev/null +++ b/mtapi_cpp_ext/test/mtapi_cpp_test_config.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MTAPI_CPP_TEST_MTAPI_CPP_TEST_CONFIG_H_ +#define MTAPI_CPP_TEST_MTAPI_CPP_TEST_CONFIG_H_ + +#include +#include + +#define THIS_DOMAIN_ID 1 +#define THIS_NODE_ID 1 + +#endif // MTAPI_CPP_TEST_MTAPI_CPP_TEST_CONFIG_H_ diff --git a/mtapi_cpp_ext/test/mtapi_cpp_test_group.cc b/mtapi_cpp_ext/test/mtapi_cpp_test_group.cc new file mode 100644 index 0000000..610d58d --- /dev/null +++ b/mtapi_cpp_ext/test/mtapi_cpp_test_group.cc @@ -0,0 +1,113 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include + +#define JOB_TEST_GROUP 5 +#define TASK_COUNT 4 + +struct result_example_struct { + int value1; + int value2; +}; + +typedef struct result_example_struct result_example_t; + +static void testGroupAction( + const void* args, + mtapi_size_t /*args_size*/, + void* results, + mtapi_size_t /*results_size*/, + const void* /*node_local_data*/, + mtapi_size_t /*node_local_data_size*/, + mtapi_task_context_t * /*context*/) { + result_example_t const * in = static_cast(args); + result_example_t * out = static_cast(results); + out->value2 = in->value1; +} + +static void testDoSomethingElse() { +} + +GroupTest::GroupTest() { + CreateUnit("mtapi_cpp group test").Add(&GroupTest::TestBasic, this); +} + +void GroupTest::TestBasic() { + embb::mtapi::Node::Initialize(THIS_DOMAIN_ID, THIS_NODE_ID); + + embb::mtapi::Job job(JOB_TEST_GROUP, THIS_DOMAIN_ID); + embb::mtapi::Action action(JOB_TEST_GROUP, testGroupAction); + + { + embb::mtapi::Group group; + + result_example_t buffer[TASK_COUNT]; + for (int ii = 0; ii < TASK_COUNT; ii++) { + buffer[ii].value1 = ii; + buffer[ii].value2 = -1; + group.Start(job, &buffer[ii], &buffer[ii]); + } + + testDoSomethingElse(); + + group.WaitAll(); + + for (int ii = 0; ii < TASK_COUNT; ii++) { + PT_EXPECT_EQ(buffer[ii].value1, ii); + PT_EXPECT_EQ(buffer[ii].value2, ii); + } + } + + { + embb::mtapi::Group group; + + result_example_t buffer[TASK_COUNT]; + for (int ii = 0; ii < 4; ii++) { + buffer[ii].value1 = ii; + buffer[ii].value2 = -1; + group.Start(job, &buffer[ii], &buffer[ii]); + } + + testDoSomethingElse(); + + mtapi_status_t status; + result_example_t* result; + while (MTAPI_SUCCESS == + (status = group.WaitAny(reinterpret_cast(&result)))) { + PT_EXPECT(result != MTAPI_NULL); + PT_EXPECT_EQ(result->value1, result->value2); + } + PT_EXPECT_EQ(status, MTAPI_GROUP_COMPLETED); + } + + embb::mtapi::Node::Finalize(); + + PT_EXPECT_EQ(embb_get_bytes_allocated(), 0u); +} diff --git a/mtapi_cpp_ext/test/mtapi_cpp_test_group.h b/mtapi_cpp_ext/test/mtapi_cpp_test_group.h new file mode 100644 index 0000000..7a1cf2c --- /dev/null +++ b/mtapi_cpp_ext/test/mtapi_cpp_test_group.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MTAPI_CPP_TEST_MTAPI_CPP_TEST_GROUP_H_ +#define MTAPI_CPP_TEST_MTAPI_CPP_TEST_GROUP_H_ + +#include + +class GroupTest : public partest::TestCase { + public: + GroupTest(); + + private: + void TestBasic(); +}; + +#endif // MTAPI_CPP_TEST_MTAPI_CPP_TEST_GROUP_H_ diff --git a/mtapi_cpp_ext/test/mtapi_cpp_test_queue.cc b/mtapi_cpp_ext/test/mtapi_cpp_test_queue.cc new file mode 100644 index 0000000..91a8884 --- /dev/null +++ b/mtapi_cpp_ext/test/mtapi_cpp_test_queue.cc @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include + +#define JOB_TEST_QUEUE 42 + +static void testQueueAction( + const void* /*args*/, + mtapi_size_t /*args_size*/, + void* results, + mtapi_size_t /*results_size*/, + const void* /*node_local_data*/, + mtapi_size_t /*node_local_data_size*/, + mtapi_task_context_t * context) { + embb::mtapi::TaskContext ctx(context); + int * out = reinterpret_cast(results); + *out = 1; + ctx.SetStatus(MTAPI_ERR_ACTION_CANCELLED); +} + +static void testDoSomethingElse() { +} + +QueueTest::QueueTest() { + CreateUnit("mtapi_cpp queue test").Add(&QueueTest::TestBasic, this); +} + +void QueueTest::TestBasic() { + embb::mtapi::Node::Initialize(THIS_DOMAIN_ID, THIS_NODE_ID); + + embb::mtapi::Job job(JOB_TEST_QUEUE, THIS_DOMAIN_ID); + embb::mtapi::Action action(JOB_TEST_QUEUE, testQueueAction, + MTAPI_NULL, 0); + + { + embb::mtapi::Queue queue(job); + + int result = 0; + embb::mtapi::Task task = queue.Enqueue(MTAPI_NULL, &result); + + testDoSomethingElse(); + + mtapi_status_t status = task.Wait(); + PT_EXPECT_EQ(status, MTAPI_ERR_ACTION_CANCELLED); + PT_EXPECT_EQ(result, 1); + } + + embb::mtapi::Node::Finalize(); + + PT_EXPECT_EQ(embb_get_bytes_allocated(), 0u); +} diff --git a/mtapi_cpp_ext/test/mtapi_cpp_test_queue.h b/mtapi_cpp_ext/test/mtapi_cpp_test_queue.h new file mode 100644 index 0000000..9659ae2 --- /dev/null +++ b/mtapi_cpp_ext/test/mtapi_cpp_test_queue.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MTAPI_CPP_TEST_MTAPI_CPP_TEST_QUEUE_H_ +#define MTAPI_CPP_TEST_MTAPI_CPP_TEST_QUEUE_H_ + +#include + +class QueueTest : public partest::TestCase { + public: + QueueTest(); + + private: + void TestBasic(); +}; + +#endif // MTAPI_CPP_TEST_MTAPI_CPP_TEST_QUEUE_H_ diff --git a/mtapi_cpp_ext/test/mtapi_cpp_test_task.cc b/mtapi_cpp_ext/test/mtapi_cpp_test_task.cc new file mode 100644 index 0000000..695f038 --- /dev/null +++ b/mtapi_cpp_ext/test/mtapi_cpp_test_task.cc @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include +#include + +#include + +#define JOB_TEST_TASK 42 +#define JOB_TEST_ERROR 17 + +static void testTaskAction( + const void* args, + mtapi_size_t /*args_size*/, + void* results, + mtapi_size_t /*results_size*/, + const void* /*node_local_data*/, + mtapi_size_t /*node_local_data_size*/, + mtapi_task_context_t * context) { + embb::mtapi::TaskContext ctx(context); + const char * msg = static_cast(args); + std::string* out = static_cast(results); + *out = msg; +} + +static void testErrorAction( + const void* /*args*/, + mtapi_size_t /*args_size*/, + void* /*results*/, + mtapi_size_t /*results_size*/, + const void* /*node_local_data*/, + mtapi_size_t /*node_local_data_size*/, + mtapi_task_context_t * context) { + embb::mtapi::TaskContext ctx(context); + ctx.SetStatus(MTAPI_ERR_ACTION_FAILED); +} + +static void testDoSomethingElse() { +} + +TaskTest::TaskTest() { + CreateUnit("mtapi_cpp task test").Add(&TaskTest::TestBasic, this); +} + +void TaskTest::TestBasic() { + embb::mtapi::Node::Initialize(THIS_DOMAIN_ID, THIS_NODE_ID); + + embb::mtapi::Node & node = embb::mtapi::Node::GetInstance(); + + { + embb::mtapi::NodeAttributes attr; + attr + .SetMaxActions(1024) + .SetMaxActionsPerJob(2) + .SetMaxPriorities(4); + } + + { + embb::mtapi::Affinity affinity(false); + PT_EXPECT_EQ(affinity.GetInternal(), 0u); + affinity.Set(0u, true); + PT_EXPECT_EQ(affinity.GetInternal(), 1u); + affinity.Set(1u, true); + PT_EXPECT_EQ(affinity.GetInternal(), 3u); + affinity.Set(0u, false); + PT_EXPECT_EQ(affinity.GetInternal(), 2u); + PT_EXPECT_EQ(affinity.Get(0), false); + PT_EXPECT_EQ(affinity.Get(1), true); + } + + { + embb::mtapi::Job job_task(JOB_TEST_TASK, THIS_DOMAIN_ID); + + embb::mtapi::Action action_task(JOB_TEST_TASK, testTaskAction); + + std::string test; + embb::mtapi::Task task = node.Start(job_task, "simple", &test); + testDoSomethingElse(); + mtapi_status_t status = task.Wait(); + PT_EXPECT_EQ(status, MTAPI_SUCCESS); + PT_EXPECT(test == "simple"); + } + + { + embb::mtapi::Job job_error(JOB_TEST_ERROR, THIS_DOMAIN_ID); + + embb::mtapi::Action action_error(JOB_TEST_ERROR, testErrorAction, embb::mtapi::ActionAttributes()); + + std::string test; + embb::mtapi::Task task = node.Start(job_error, "simple", &test); + testDoSomethingElse(); + mtapi_status_t status = task.Wait(); + PT_EXPECT_EQ(status, MTAPI_ERR_ACTION_FAILED); + } + + embb::mtapi::Node::Finalize(); + + PT_EXPECT_EQ(embb_get_bytes_allocated(), 0u); +} diff --git a/mtapi_cpp_ext/test/mtapi_cpp_test_task.h b/mtapi_cpp_ext/test/mtapi_cpp_test_task.h new file mode 100644 index 0000000..f86b6fe --- /dev/null +++ b/mtapi_cpp_ext/test/mtapi_cpp_test_task.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2014-2015, Siemens AG. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef MTAPI_CPP_TEST_MTAPI_CPP_TEST_TASK_H_ +#define MTAPI_CPP_TEST_MTAPI_CPP_TEST_TASK_H_ + +#include + +class TaskTest : public partest::TestCase { + public: + TaskTest(); + + private: + void TestBasic(); +}; + +#endif // MTAPI_CPP_TEST_MTAPI_CPP_TEST_TASK_H_ -- libgit2 0.26.0