mirror of
https://github.com/J-D-K/JKSV.git
synced 2026-09-07 01:55:19 -05:00
Code revisions.
This commit is contained in:
@@ -22,52 +22,30 @@ class BufferQueue final
|
||||
|
||||
/// @brief Creates a new BufferQueue.
|
||||
/// @param bufferLimit Number of buffers before the queue is considered full.
|
||||
BufferQueue(int bufferLimit, size_t bufferSize) noexcept
|
||||
: m_bufferLimit(bufferLimit)
|
||||
, m_bufferSize(bufferSize) {};
|
||||
BufferQueue(int bufferLimit) noexcept
|
||||
: m_bufferLimit(bufferLimit) {};
|
||||
|
||||
/// @brief Gets an returns a scoped lock_guard for the internal mutex.
|
||||
inline BufferGuard lock_queue() noexcept { return BufferGuard{m_queueMutex}; }
|
||||
|
||||
/// @brief Returns if the buffer queue is "full", or, has reached its limit.
|
||||
inline bool is_full() const noexcept
|
||||
inline bool try_push(Buffer &buffer, size_t bufferSize)
|
||||
{
|
||||
if (static_cast<int>(m_queue.size()) < m_bufferLimit) { return false; }
|
||||
std::lock_guard queueGuard{m_queueMutex};
|
||||
if (m_bufferLimit != NO_LIMIT && static_cast<int>(m_queue.size()) >= m_bufferLimit) { return false; }
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(10));
|
||||
m_queue.emplace(std::move(buffer), bufferSize);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @brief Returns if the buffer queue is empty.
|
||||
inline bool is_empty() const noexcept
|
||||
inline bool get_front(QueuePair &pairOut)
|
||||
{
|
||||
if (!m_queue.empty()) { return false; }
|
||||
std::lock_guard queueGuard{m_queueMutex};
|
||||
if (m_queue.empty()) { return false; }
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::microseconds(10));
|
||||
return true;
|
||||
}
|
||||
|
||||
/// @brief Creates and returns a buffer of bufferSize.
|
||||
inline Buffer allocate_buffer() { return std::make_unique<sys::Byte[]>(m_bufferSize); }
|
||||
|
||||
/// @brief Same as above, but with the size passed here.
|
||||
inline Buffer allocate_buffer(size_t bufferSize) { return std::make_unique<sys::Byte[]>(bufferSize); }
|
||||
|
||||
/// @brief Pushes a new buffer to the queue.
|
||||
inline void push_to_queue(Buffer &buffer, size_t bufferSize)
|
||||
{
|
||||
auto queuePair = std::make_pair(std::move(buffer), bufferSize);
|
||||
m_queue.push(std::move(queuePair));
|
||||
}
|
||||
|
||||
/// @brief Returns the front of the queue and pops it.
|
||||
inline QueuePair get_front()
|
||||
{
|
||||
auto front = std::move(m_queue.front());
|
||||
pairOut = std::move(m_queue.front());
|
||||
m_queue.pop();
|
||||
return front;
|
||||
return true;
|
||||
}
|
||||
|
||||
static inline void default_delay() noexcept { std::this_thread::sleep_for(std::chrono::microseconds(10)); }
|
||||
|
||||
/// @brief This can be passed to the constructor to make is_full() return false all of the time.
|
||||
static inline int NO_LIMIT = -1;
|
||||
|
||||
|
||||
@@ -10,12 +10,13 @@
|
||||
|
||||
namespace curl
|
||||
{
|
||||
inline constexpr int DOWNLOAD_QUEUE_LIMIT = 128;
|
||||
|
||||
// clang-format off
|
||||
struct DownloadStruct : sys::threadpool::DataStruct
|
||||
{
|
||||
/// @brief Buffer queue for downloads.
|
||||
BufferQueue bufferQueue{BufferQueue::NO_LIMIT, 0};
|
||||
BufferQueue bufferQueue{BufferQueue::NO_LIMIT};
|
||||
|
||||
/// @brief Destination file to write to.
|
||||
fslib::File *dest{};
|
||||
|
||||
Reference in New Issue
Block a user