-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds concurrent_queue, thread_pool, parallel_for, function_ref, move_only_function implementations. Removes the dependency on the parallel stl algorithms (and thus on TBB with stdlibc++).
- Loading branch information
Showing
18 changed files
with
346 additions
and
78 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
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
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
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
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
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
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
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
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
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,63 @@ | ||
/* Copyright (c) 2023 Krisztián Rugási. Subject to the MIT License. */ | ||
|
||
#ifndef GA_UTILITY_CONCURRENT_QUEUE_HPP | ||
#define GA_UTILITY_CONCURRENT_QUEUE_HPP | ||
|
||
#include "spinlock.hpp" | ||
#include <condition_variable> | ||
#include <mutex> | ||
#include <deque> | ||
#include <optional> | ||
#include <type_traits> | ||
|
||
namespace gapp::detail | ||
{ | ||
template<typename T> | ||
class concurrent_queue | ||
{ | ||
public: | ||
template<typename... Args> | ||
[[nodiscard]] bool emplace(Args&&... args) | ||
{ | ||
std::scoped_lock lock{ queue_lock_ }; | ||
if (is_closed_) return false; | ||
queue_.emplace_back(std::forward<Args>(args)...); | ||
queue_cv_.notify_one(); | ||
return true; | ||
} | ||
|
||
[[nodiscard]] std::optional<T> take() noexcept(std::is_nothrow_move_constructible_v<T>) | ||
{ | ||
std::unique_lock lock{ queue_lock_ }; | ||
queue_cv_.wait(lock, [&]() noexcept { return !queue_.empty() || is_closed_; }); | ||
|
||
if (is_closed_ && queue_.empty()) return {}; | ||
|
||
T elem = std::move(queue_.front()); | ||
queue_.pop_front(); | ||
return elem; | ||
} | ||
|
||
void close() noexcept | ||
{ | ||
std::scoped_lock lock{ queue_lock_ }; | ||
is_closed_ = true; | ||
queue_cv_.notify_all(); | ||
} | ||
|
||
[[nodiscard]] bool closed() const noexcept | ||
{ | ||
std::scoped_lock lock{ queue_lock_ }; | ||
return is_closed_; | ||
} | ||
|
||
private: | ||
std::deque<T> queue_; | ||
mutable detail::spinlock queue_lock_; | ||
std::condition_variable_any queue_cv_; | ||
bool is_closed_ = false; | ||
}; | ||
|
||
} // namespace gapp::detail | ||
|
||
#endif // !GA_UTILITY_CONCURRENT_QUEUE_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
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
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
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.