Skip to content

Commit

Permalink
Changed std::execution to std::async in Us4OEM initialization, remove…
Browse files Browse the repository at this point in the history
…d TBB dependency.
  • Loading branch information
pjarosik committed Oct 6, 2021
1 parent 10487b0 commit 479d5eb
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 38 deletions.
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ find_package(Eigen3 REQUIRED)
if("${ARRUS_BUILD_PLATFORM}" STREQUAL "windows")
set(ARRUS_OS_DEPS "")
else("${ARRUS_BUILD_PLATFORM}" STREQUAL "linux")
find_package(TBB REQUIRED)
set(ARRUS_OS_DEPS tbb)
set(ARRUS_OS_DEPS "")
endif()
################################################################################
# Sub-projects
Expand Down
33 changes: 10 additions & 23 deletions arrus/core/devices/us4r/external/ius4oem/IUs4OEMInitializerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
#define ARRUS_CORE_DEVICES_US4R_EXTERNAL_IUS4OEM_IUS4OEMINITIALIZERIMPL_H

#include <algorithm>
#include <execution>
#include <mutex>
#include <iostream>
#include <future>

#include "IUs4OEMInitializer.h"

Expand Down Expand Up @@ -32,30 +32,17 @@ class IUs4OEMInitializerImpl : public IUs4OEMInitializer {
// Us4OEMs are initialized here.
}
private:

void initializeUs4oems(std::vector<IUs4OEMHandle> &ius4oems, int level) {
std::vector<std::exception> exceptions;
std::mutex exceptions_mutex;
std::vector<std::future<void>> results;

std::for_each(
std::execution::par_unseq, std::begin(ius4oems), std::end(ius4oems),
[&](IUs4OEMHandle &u) {
try {
u->Initialize(level);
}
catch(const std::exception &e) {
std::lock_guard guard(exceptions_mutex);
exceptions.push_back(e);
}
catch(...) {
std::lock_guard guard(exceptions_mutex);
exceptions.push_back(std::runtime_error("Unknown exception during Us4OEM initialization."));
}
}
);
if(!exceptions.empty()) {
// TODO create a complete list of error messages
// now throw any of the exception
throw exceptions.front();
for(IUs4OEMHandle &ius4oem : ius4oems) {
std::future<void> result = std::async(std::launch::async, [&ius4oem, level]() {ius4oem->Initialize(level);});
results.push_back(std::move(result));
}
for(auto &result: results) {
result.wait();
result.get(); // wait and throw exception if necessary.
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace {
using namespace arrus::devices;
using ::testing::Return;
using ::testing::InSequence;
using ::testing::Sequence;

// Test if the input array is approprietaly sorted
TEST(IUs4OEMInitializerImplTest, Us4OEMsSortedApproprietaly) {
Expand Down Expand Up @@ -45,24 +46,24 @@ TEST(IUs4OEMInitializerImplTest, Us4OEMsInitializedProperly) {
ON_CALL(GET_MOCK_PTR(ius4oems[1]), GetID)
.WillByDefault(Return(0));

// The actual order: 1, 0
{
InSequence seq;
Sequence us4oem0Seq;
Sequence us4oem1Seq;

EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(1));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(1));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(1)).InSequence(us4oem1Seq);
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(1)).InSequence(us4oem0Seq);

EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Synchronize());
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(2));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(2));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Synchronize()).InSequence(us4oem1Seq, us4oem0Seq);
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(2)).InSequence(us4oem1Seq);
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(2)).InSequence(us4oem0Seq);

EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Synchronize());
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(3));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(3));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Synchronize()).InSequence(us4oem1Seq, us4oem0Seq);
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(3)).InSequence(us4oem1Seq);
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(3)).InSequence(us4oem0Seq);

EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Synchronize());
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(4));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(4));
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Synchronize()).InSequence(us4oem1Seq, us4oem0Seq);
EXPECT_CALL(GET_MOCK_PTR(ius4oems[1]), Initialize(4)).InSequence(us4oem1Seq);
EXPECT_CALL(GET_MOCK_PTR(ius4oems[0]), Initialize(4)).InSequence(us4oem0Seq);
}

IUs4OEMInitializerImpl initializer;
Expand Down

0 comments on commit 479d5eb

Please sign in to comment.