2021-06-24 17:04:04 +08:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
Copyright (c) 2021 Bytedance Inc.
|
|
|
|
|
2022-01-04 12:36:20 +08:00
|
|
|
https://adxeproject.github.io/
|
2021-06-24 17:04:04 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <deque>
|
|
|
|
#include <mutex>
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace cocos2d
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
template <typename _Ty>
|
2021-12-25 10:04:45 +08:00
|
|
|
class ConcurrentDeque
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
public:
|
|
|
|
/** Iterator, can be used to loop the Vector. */
|
|
|
|
using iterator = typename std::deque<_Ty>::iterator;
|
|
|
|
/** Const iterator, can be used to loop the Vector. */
|
|
|
|
using const_iterator = typename std::deque<_Ty>::const_iterator;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void push_back(_Ty&& value)
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
queue_.push_back(std::forward<_Ty>(value));
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
void push_back(const _Ty& value)
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
queue_.push_back(value);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_Ty& front()
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
return queue_.front();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
void pop_front()
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
queue_.pop_front();
|
|
|
|
}
|
2021-08-03 14:41:30 +08:00
|
|
|
void push_front(_Ty&& value)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
queue_.push_front(std::forward<_Ty>(value));
|
|
|
|
}
|
|
|
|
void push_front(const _Ty& value)
|
|
|
|
{
|
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
queue_.push_front(value);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
size_t size() const
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
return this->queue_.size();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
bool empty() const
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
return this->queue_.empty();
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
void clear()
|
|
|
|
{
|
2021-06-24 17:04:04 +08:00
|
|
|
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
|
|
|
|
this->queue_.clear();
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
std::unique_lock<std::recursive_mutex> get_lock() { return std::unique_lock<std::recursive_mutex>{this->mtx_}; }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void lock() { this->mtx_.lock(); }
|
|
|
|
void unlock() { this->mtx_.unlock(); }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void unsafe_push_back(_Ty&& value) { queue_.push_back(std::forward<_Ty>(value)); }
|
|
|
|
void unsafe_push_back(const _Ty& value) { queue_.push_back(value); }
|
|
|
|
_Ty& unsafe_front() { return queue_.front(); }
|
|
|
|
void unsafe_pop_front() { queue_.pop_front(); }
|
2021-08-03 14:41:30 +08:00
|
|
|
void unsafe_push_front(_Ty&& value) { queue_.push_font(std::forward<_Ty>(value)); }
|
|
|
|
void unsafe_push_front(const _Ty& value) { queue_.push_font(value); }
|
2021-12-25 10:04:45 +08:00
|
|
|
bool unsafe_empty() const { return this->queue_.empty(); }
|
|
|
|
size_t unsafe_size() const { return this->queue_.size(); }
|
|
|
|
void unsafe_clear() { this->queue_.clear(); }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
iterator unsafe_begin() { return this->queue_.begin(); }
|
|
|
|
iterator unsafe_end() { return this->queue_.end(); }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const_iterator unsafe_begin() const { return this->queue_.begin(); }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const_iterator unsafe_end() const { return this->queue_.end(); }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
iterator unsafe_erase(iterator iter) { return this->queue_.erase(iter); }
|
2021-06-24 17:04:04 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::deque<_Ty> queue_;
|
|
|
|
mutable std::recursive_mutex mtx_;
|
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace cocos2d
|