Skip to content

Commit 80f71cb

Browse files
author
jparisu
committed
New Enchanced Thread Pool Implementation
Signed-off-by: jparisu <[email protected]>
1 parent 9310b98 commit 80f71cb

39 files changed

+2517
-595
lines changed

Diff for: ddsrouter_cmake/cmake/test/test_target.cmake

+18-9
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,24 @@ function(add_test_executable TEST_EXECUTABLE_NAME TEST_SOURCES TEST_NAME TEST_LI
5858

5959
get_win32_path_dependencies(${TEST_EXECUTABLE_NAME} TEST_FRIENDLY_PATH)
6060

61-
foreach(test_name ${TEST_LIST})
62-
add_test(NAME ${TEST_NAME}.${test_name}
63-
COMMAND ${TEST_EXECUTABLE_NAME}
64-
--gtest_filter=${TEST_NAME}.${test_name}:**/${TEST_NAME}.${test_name}/**)
65-
66-
if(TEST_FRIENDLY_PATH)
67-
set_tests_properties(${TEST_NAME}.${test_name} PROPERTIES ENVIRONMENT "PATH=${TEST_FRIENDLY_PATH}")
68-
endif(TEST_FRIENDLY_PATH)
69-
endforeach()
61+
if( TEST_LIST )
62+
# If list of tests is not empty, add each test separatly
63+
foreach(test_name ${TEST_LIST})
64+
add_test(NAME ${TEST_NAME}.${test_name}
65+
COMMAND ${TEST_EXECUTABLE_NAME}
66+
--gtest_filter=${TEST_NAME}**.${test_name}:**/${TEST_NAME}**.${test_name}/**)
67+
68+
if(TEST_FRIENDLY_PATH)
69+
set_tests_properties(${TEST_NAME}.${test_name} PROPERTIES ENVIRONMENT "PATH=${TEST_FRIENDLY_PATH}")
70+
endif(TEST_FRIENDLY_PATH)
71+
endforeach()
72+
else()
73+
# If no tests are provided, create a single test
74+
message(STATUS "Creating general test ${TEST_NAME}.")
75+
add_test(NAME ${TEST_NAME}
76+
COMMAND ${TEST_EXECUTABLE_NAME})
77+
endif( TEST_LIST )
78+
7079

7180
target_compile_definitions(${TEST_EXECUTABLE_NAME}
7281
PRIVATE FASTDDS_ENFORCE_LOG_INFO
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file SlotThreadPool.hpp
17+
*
18+
* This file contains class SlotThreadPool definition.
19+
*/
20+
21+
#ifndef _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_
22+
#define _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_
23+
24+
#include <functional>
25+
#include <memory>
26+
27+
namespace eprosima {
28+
namespace ddsrouter {
29+
namespace utils {
30+
31+
class IThreadPool
32+
{
33+
public:
34+
void execute(std::unique_ptr<ITask>&& task){}
35+
};
36+
37+
class ITask
38+
{
39+
public:
40+
void operator() () noexcept;
41+
};
42+
43+
class BasicTask : ITask
44+
{
45+
public:
46+
BasicTask(const std::function<void()>* callback_ptr){}
47+
void operator() () noexcept;
48+
};
49+
50+
class OwnedTask : ITask
51+
{
52+
public:
53+
OwnedTask(const std::function<void()>& callback_){}
54+
OwnedTask(std::function<void()>&& callback_){}
55+
void operator() () noexcept;
56+
};
57+
58+
template <typename ... Args>
59+
class ConnectorOneShotArgs
60+
{
61+
public:
62+
static void execute(const IThreadPool& tp, const std::function<void(Args...)>& callback, Args... args){}
63+
static void execute(const IThreadPool& tp, std::function<void(Args...)>&& callback, Args... args){}
64+
};
65+
using ConnectorOneShot = ConnectorOneShotArgs<>;
66+
67+
template <typename ... Args>
68+
class ConnectorSlotArgs
69+
{
70+
public:
71+
ConnectorSlotArgs(const IThreadPool& tp, const std::function<void(Args...)>& callback){}
72+
// ConnectorSlotArgs(const IThreadPool& tp, std::function<void<(Args...)>&& callback){}
73+
void execute(Args...){}
74+
};
75+
using ConnectorSlot = ConnectorSlotArgs<>;
76+
77+
int main()
78+
{
79+
IThreadPool pool;
80+
81+
ConnectorOneShot::execute(pool, [](){ /* do something */ });
82+
ConnectorOneShotArgs<int>::execute(pool, [](int x){ /* do something */ }, 3);
83+
ConnectorOneShotArgs<std::string, bool>::execute(pool, [](std::string s, bool b){ /* do something */ }, std::string("hello"), true);
84+
85+
ConnectorSlot slot_connector(pool, [](){ /* do something */ });
86+
slot_connector.execute();
87+
88+
ConnectorSlotArgs<int> slot_args(pool, [](int x){});
89+
slot_args.execute(2);
90+
91+
ConnectorSlotArgs<std::string, bool> slot_args_2(pool, std::function<void(std::string, bool)>([](std::string s, bool b){}));
92+
slot_args_2.execute(std::string("hello"), true);
93+
94+
}
95+
96+
} /* namespace utils */
97+
} /* namespace ddsrouter */
98+
} /* namespace eprosima */
99+
100+
#endif /* _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file OwnedTask.hpp
17+
*
18+
* This file contains class Task definition.
19+
*/
20+
21+
#pragma once
22+
23+
#include <functional>
24+
25+
#include <ddsrouter_utils/thread/task/ITask.hpp>
26+
#include <ddsrouter_utils/thread/manager/IManager.hpp>
27+
28+
namespace eprosima {
29+
namespace ddsrouter {
30+
namespace utils {
31+
namespace thread {
32+
33+
template <typename ... Args>
34+
class OneShotConnector
35+
{
36+
public:
37+
38+
static void execute(IManager* tp, const std::function<void(Args...)>& callback, Args... args);
39+
40+
static void execute(IManager* tp, std::function<void(Args...)>&& callback, Args... args);
41+
42+
};
43+
using SimpleOneShotConnector = OneShotConnector<>;
44+
45+
} /* namespace thread */
46+
} /* namespace utils */
47+
} /* namespace ddsrouter */
48+
} /* namespace eprosima */
49+
50+
// Include implementation template file
51+
#include <ddsrouter_utils/thread/connector/impl/OneShotConnector.ipp>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file SlotConnector.hpp
17+
*
18+
* This file contains class SlotConnector definition.
19+
*/
20+
21+
#pragma once
22+
23+
#include <functional>
24+
25+
#include <ddsrouter_utils/thread/task/ITask.hpp>
26+
#include <ddsrouter_utils/thread/manager/IManager.hpp>
27+
28+
namespace eprosima {
29+
namespace ddsrouter {
30+
namespace utils {
31+
namespace thread {
32+
33+
template <typename ... Args>
34+
class SlotConnector
35+
{
36+
public:
37+
38+
SlotConnector(
39+
IManager* manager,
40+
const std::function<void(Args...)>& callback);
41+
42+
SlotConnector(
43+
IManager* manager,
44+
std::function<void(Args...)>&& callback);
45+
46+
~SlotConnector() = default;
47+
48+
void execute(Args...);
49+
50+
protected:
51+
52+
IManager* manager_;
53+
54+
std::function<void (Args...)> callback_;
55+
56+
};
57+
using SimpleSlotConnector = SlotConnector<>;
58+
59+
} /* namespace thread */
60+
} /* namespace utils */
61+
} /* namespace ddsrouter */
62+
} /* namespace eprosima */
63+
64+
// Include implementation template file
65+
#include <ddsrouter_utils/thread/connector/impl/SlotConnector.ipp>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file OneShotConnector.hpp
17+
*
18+
* This file contains class OneShotConnector implementation.
19+
*/
20+
21+
#pragma once
22+
23+
#include <ddsrouter_utils/thread/task/ArgsOwnedTask.hpp>
24+
25+
namespace eprosima {
26+
namespace ddsrouter {
27+
namespace utils {
28+
namespace thread {
29+
30+
template <typename ... Args>
31+
void OneShotConnector<Args...>::execute(
32+
IManager* manager,
33+
const std::function<void(Args...)>& callback,
34+
Args... args)
35+
{
36+
manager->execute(
37+
std::make_unique<ArgsOwnedTask<Args...>>(
38+
callback,
39+
args...
40+
)
41+
);
42+
}
43+
44+
template <typename ... Args>
45+
void OneShotConnector<Args...>::execute(
46+
IManager* manager,
47+
std::function<void(Args...)>&& callback,
48+
Args... args)
49+
{
50+
manager->execute(
51+
std::make_unique<ArgsOwnedTask<Args...>>(
52+
std::move(callback),
53+
args...
54+
)
55+
);
56+
}
57+
58+
// template <typename ... Args>
59+
// void OneShotConnector<Args...>::execute(
60+
// IManager* manager,
61+
// std::function<void(Args...)> callback,
62+
// Args... args)
63+
// {
64+
// manager->execute(
65+
// std::make_unique<ArgsOwnedTask<Args...>>(
66+
// callback,
67+
// args...
68+
// )
69+
// );
70+
// }
71+
72+
} /* namespace thread */
73+
} /* namespace event */
74+
} /* namespace ddsrouter */
75+
} /* namespace eprosima */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
/**
16+
* @file SlotConnector.hpp
17+
*
18+
* This file contains class SlotConnector implementation.
19+
*/
20+
21+
#pragma once
22+
23+
#include <ddsrouter_utils/thread/task/ArgsOwnedTask.hpp>
24+
25+
namespace eprosima {
26+
namespace ddsrouter {
27+
namespace utils {
28+
namespace thread {
29+
30+
template <typename ... Args>
31+
SlotConnector<Args...>::SlotConnector(
32+
IManager* manager,
33+
const std::function<void(Args...)>& callback)
34+
: manager_(manager)
35+
, callback_(callback)
36+
{
37+
}
38+
39+
template <typename ... Args>
40+
SlotConnector<Args...>::SlotConnector(
41+
IManager* manager,
42+
std::function<void(Args...)>&& callback)
43+
: manager_(manager)
44+
, callback_(std::move(callback))
45+
{
46+
}
47+
48+
template <typename ... Args>
49+
void SlotConnector<Args...>::execute(Args... args)
50+
{
51+
manager_->execute(
52+
std::make_unique<ArgsOwnedTask<Args...>>(
53+
callback_,
54+
args...
55+
)
56+
);
57+
}
58+
59+
} /* namespace thread */
60+
} /* namespace event */
61+
} /* namespace ddsrouter */
62+
} /* namespace eprosima */

0 commit comments

Comments
 (0)