-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Enchanced Thread Pool Implementation
Signed-off-by: jparisu <[email protected]>
- Loading branch information
jparisu
committed
Sep 20, 2022
1 parent
9310b98
commit 80f71cb
Showing
39 changed files
with
2,517 additions
and
595 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
ddsrouter_utils/include/ddsrouter_utils/thread/_ThreadPool.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file SlotThreadPool.hpp | ||
* | ||
* This file contains class SlotThreadPool definition. | ||
*/ | ||
|
||
#ifndef _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_ | ||
#define _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_ | ||
|
||
#include <functional> | ||
#include <memory> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
|
||
class IThreadPool | ||
{ | ||
public: | ||
void execute(std::unique_ptr<ITask>&& task){} | ||
}; | ||
|
||
class ITask | ||
{ | ||
public: | ||
void operator() () noexcept; | ||
}; | ||
|
||
class BasicTask : ITask | ||
{ | ||
public: | ||
BasicTask(const std::function<void()>* callback_ptr){} | ||
void operator() () noexcept; | ||
}; | ||
|
||
class OwnedTask : ITask | ||
{ | ||
public: | ||
OwnedTask(const std::function<void()>& callback_){} | ||
OwnedTask(std::function<void()>&& callback_){} | ||
void operator() () noexcept; | ||
}; | ||
|
||
template <typename ... Args> | ||
class ConnectorOneShotArgs | ||
{ | ||
public: | ||
static void execute(const IThreadPool& tp, const std::function<void(Args...)>& callback, Args... args){} | ||
static void execute(const IThreadPool& tp, std::function<void(Args...)>&& callback, Args... args){} | ||
}; | ||
using ConnectorOneShot = ConnectorOneShotArgs<>; | ||
|
||
template <typename ... Args> | ||
class ConnectorSlotArgs | ||
{ | ||
public: | ||
ConnectorSlotArgs(const IThreadPool& tp, const std::function<void(Args...)>& callback){} | ||
// ConnectorSlotArgs(const IThreadPool& tp, std::function<void<(Args...)>&& callback){} | ||
void execute(Args...){} | ||
}; | ||
using ConnectorSlot = ConnectorSlotArgs<>; | ||
|
||
int main() | ||
{ | ||
IThreadPool pool; | ||
|
||
ConnectorOneShot::execute(pool, [](){ /* do something */ }); | ||
ConnectorOneShotArgs<int>::execute(pool, [](int x){ /* do something */ }, 3); | ||
ConnectorOneShotArgs<std::string, bool>::execute(pool, [](std::string s, bool b){ /* do something */ }, std::string("hello"), true); | ||
|
||
ConnectorSlot slot_connector(pool, [](){ /* do something */ }); | ||
slot_connector.execute(); | ||
|
||
ConnectorSlotArgs<int> slot_args(pool, [](int x){}); | ||
slot_args.execute(2); | ||
|
||
ConnectorSlotArgs<std::string, bool> slot_args_2(pool, std::function<void(std::string, bool)>([](std::string s, bool b){})); | ||
slot_args_2.execute(std::string("hello"), true); | ||
|
||
} | ||
|
||
} /* namespace utils */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ | ||
|
||
#endif /* _DDSROUTERTHREAD__SRC_CPP_POOL_SLOTTHREADPOOL_HPP_ */ |
51 changes: 51 additions & 0 deletions
51
ddsrouter_utils/include/ddsrouter_utils/thread/connector/OneShotConnector.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file OwnedTask.hpp | ||
* | ||
* This file contains class Task definition. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include <ddsrouter_utils/thread/task/ITask.hpp> | ||
#include <ddsrouter_utils/thread/manager/IManager.hpp> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
namespace thread { | ||
|
||
template <typename ... Args> | ||
class OneShotConnector | ||
{ | ||
public: | ||
|
||
static void execute(IManager* tp, const std::function<void(Args...)>& callback, Args... args); | ||
|
||
static void execute(IManager* tp, std::function<void(Args...)>&& callback, Args... args); | ||
|
||
}; | ||
using SimpleOneShotConnector = OneShotConnector<>; | ||
|
||
} /* namespace thread */ | ||
} /* namespace utils */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ | ||
|
||
// Include implementation template file | ||
#include <ddsrouter_utils/thread/connector/impl/OneShotConnector.ipp> |
65 changes: 65 additions & 0 deletions
65
ddsrouter_utils/include/ddsrouter_utils/thread/connector/SlotConnector.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file SlotConnector.hpp | ||
* | ||
* This file contains class SlotConnector definition. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <functional> | ||
|
||
#include <ddsrouter_utils/thread/task/ITask.hpp> | ||
#include <ddsrouter_utils/thread/manager/IManager.hpp> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
namespace thread { | ||
|
||
template <typename ... Args> | ||
class SlotConnector | ||
{ | ||
public: | ||
|
||
SlotConnector( | ||
IManager* manager, | ||
const std::function<void(Args...)>& callback); | ||
|
||
SlotConnector( | ||
IManager* manager, | ||
std::function<void(Args...)>&& callback); | ||
|
||
~SlotConnector() = default; | ||
|
||
void execute(Args...); | ||
|
||
protected: | ||
|
||
IManager* manager_; | ||
|
||
std::function<void (Args...)> callback_; | ||
|
||
}; | ||
using SimpleSlotConnector = SlotConnector<>; | ||
|
||
} /* namespace thread */ | ||
} /* namespace utils */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ | ||
|
||
// Include implementation template file | ||
#include <ddsrouter_utils/thread/connector/impl/SlotConnector.ipp> |
75 changes: 75 additions & 0 deletions
75
ddsrouter_utils/include/ddsrouter_utils/thread/connector/impl/OneShotConnector.ipp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file OneShotConnector.hpp | ||
* | ||
* This file contains class OneShotConnector implementation. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ddsrouter_utils/thread/task/ArgsOwnedTask.hpp> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
namespace thread { | ||
|
||
template <typename ... Args> | ||
void OneShotConnector<Args...>::execute( | ||
IManager* manager, | ||
const std::function<void(Args...)>& callback, | ||
Args... args) | ||
{ | ||
manager->execute( | ||
std::make_unique<ArgsOwnedTask<Args...>>( | ||
callback, | ||
args... | ||
) | ||
); | ||
} | ||
|
||
template <typename ... Args> | ||
void OneShotConnector<Args...>::execute( | ||
IManager* manager, | ||
std::function<void(Args...)>&& callback, | ||
Args... args) | ||
{ | ||
manager->execute( | ||
std::make_unique<ArgsOwnedTask<Args...>>( | ||
std::move(callback), | ||
args... | ||
) | ||
); | ||
} | ||
|
||
// template <typename ... Args> | ||
// void OneShotConnector<Args...>::execute( | ||
// IManager* manager, | ||
// std::function<void(Args...)> callback, | ||
// Args... args) | ||
// { | ||
// manager->execute( | ||
// std::make_unique<ArgsOwnedTask<Args...>>( | ||
// callback, | ||
// args... | ||
// ) | ||
// ); | ||
// } | ||
|
||
} /* namespace thread */ | ||
} /* namespace event */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ |
62 changes: 62 additions & 0 deletions
62
ddsrouter_utils/include/ddsrouter_utils/thread/connector/impl/SlotConnector.ipp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2022 Proyectos y Sistemas de Mantenimiento SL (eProsima). | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
/** | ||
* @file SlotConnector.hpp | ||
* | ||
* This file contains class SlotConnector implementation. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ddsrouter_utils/thread/task/ArgsOwnedTask.hpp> | ||
|
||
namespace eprosima { | ||
namespace ddsrouter { | ||
namespace utils { | ||
namespace thread { | ||
|
||
template <typename ... Args> | ||
SlotConnector<Args...>::SlotConnector( | ||
IManager* manager, | ||
const std::function<void(Args...)>& callback) | ||
: manager_(manager) | ||
, callback_(callback) | ||
{ | ||
} | ||
|
||
template <typename ... Args> | ||
SlotConnector<Args...>::SlotConnector( | ||
IManager* manager, | ||
std::function<void(Args...)>&& callback) | ||
: manager_(manager) | ||
, callback_(std::move(callback)) | ||
{ | ||
} | ||
|
||
template <typename ... Args> | ||
void SlotConnector<Args...>::execute(Args... args) | ||
{ | ||
manager_->execute( | ||
std::make_unique<ArgsOwnedTask<Args...>>( | ||
callback_, | ||
args... | ||
) | ||
); | ||
} | ||
|
||
} /* namespace thread */ | ||
} /* namespace event */ | ||
} /* namespace ddsrouter */ | ||
} /* namespace eprosima */ |
Oops, something went wrong.