Skip to content

Commit c8fd30a

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

37 files changed

+1812
-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,48 @@
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+
static void execute(IManager* tp, const std::function<void(Args...)>& callback, Args... args);
38+
static void execute(IManager* tp, std::function<void(Args...)>&& callback, Args... args);
39+
};
40+
using SimpleOneShotConnector = OneShotConnector<>;
41+
42+
} /* namespace thread */
43+
} /* namespace utils */
44+
} /* namespace ddsrouter */
45+
} /* namespace eprosima */
46+
47+
// Include implementation template file
48+
#include <ddsrouter_utils/thread/connector/impl/OneShotConnector.ipp>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
#ifndef _DDSROUTERTHREAD_TASK_TASK_HPP_
22+
#define _DDSROUTERTHREAD_TASK_TASK_HPP_
23+
24+
#include <functional>
25+
26+
#include <ddsrouter_utils/thread/task/ITask.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+
ConnectorSlotArgs(const IThreadPool& tp, const std::function<void(Args...)>& callback);
38+
ConnectorSlotArgs(const IThreadPool& tp, std::function<void<(Args...)>&& callback);
39+
void execute(Args...);
40+
};
41+
using SimpleSlotConnector = SlotConnector<>;
42+
43+
} /* namespace thread */
44+
} /* namespace utils */
45+
} /* namespace ddsrouter */
46+
} /* namespace eprosima */
47+
48+
#endif /* _DDSROUTERTHREAD_TASK_TASK_HPP_ */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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* tp,
33+
const std::function<void(Args...)>& callback,
34+
Args... args)
35+
{
36+
tp->execute(
37+
std::make_unique<ArgsOwnedTask<>>(
38+
callback,
39+
args...
40+
)
41+
);
42+
}
43+
44+
template <typename ... Args>
45+
void OneShotConnector<Args...>::execute(
46+
IManager* tp,
47+
std::function<void(Args...)>&& callback,
48+
Args... args)
49+
{
50+
tp->execute(
51+
std::make_unique<ArgsOwnedTask<>>(
52+
std::move(callback),
53+
args...
54+
)
55+
);
56+
}
57+
58+
} /* namespace thread */
59+
} /* namespace event */
60+
} /* namespace ddsrouter */
61+
} /* namespace eprosima */
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 AsyncManager.hpp
17+
*
18+
* This file contains class AsyncManager definition.
19+
*/
20+
21+
#pragma once
22+
23+
#include <map>
24+
#include <mutex>
25+
#include <thread>
26+
#include <vector>
27+
28+
#include <ddsrouter_utils/library/library_dll.h>
29+
#include <ddsrouter_utils/thread/manager/IManager.hpp>
30+
#include <ddsrouter_utils/thread/thread/CustomThread.hpp>
31+
#include <ddsrouter_utils/types/Atomicable.hpp>
32+
33+
namespace eprosima {
34+
namespace ddsrouter {
35+
namespace utils {
36+
namespace thread {
37+
38+
using TasksCollectionType =
39+
Atomicable<
40+
std::vector<
41+
std::pair<
42+
std::unique_ptr<CustomThread>,
43+
std::unique_ptr<ITask>>>>;
44+
45+
/**
46+
* TODO
47+
*/
48+
class AsyncManager : public IManager
49+
{
50+
public:
51+
52+
AsyncManager() = default;
53+
54+
~AsyncManager();
55+
56+
virtual void execute(std::unique_ptr<ITask>&& task) override;
57+
58+
void clean_threads();
59+
60+
protected:
61+
62+
TasksCollectionType tasks_running_;
63+
};
64+
65+
} /* namespace thread */
66+
} /* namespace utils */
67+
} /* namespace ddsrouter */
68+
} /* namespace eprosima */

0 commit comments

Comments
 (0)