axmol/core/base/ConcurrentDeque.h

117 lines
4.0 KiB
C
Raw Normal View History

/****************************************************************************
*
Copyright (c) 2021 Bytedance Inc.
2022-01-04 12:36:20 +08:00
https://adxeproject.github.io/
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
{
template <typename _Ty>
2021-12-25 10:04:45 +08:00
class ConcurrentDeque
{
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)
{
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)
{
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
queue_.push_back(value);
}
2021-12-25 10:04:45 +08:00
_Ty& front()
{
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
return queue_.front();
}
2021-12-25 10:04:45 +08:00
void pop_front()
{
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
queue_.pop_front();
}
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
{
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
return this->queue_.size();
}
2021-12-25 10:04:45 +08:00
bool empty() const
{
std::lock_guard<std::recursive_mutex> lck(this->mtx_);
return this->queue_.empty();
}
2021-12-25 10:04:45 +08:00
void clear()
{
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-12-25 10:04:45 +08:00
void lock() { this->mtx_.lock(); }
void unlock() { this->mtx_.unlock(); }
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(); }
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-12-25 10:04:45 +08:00
iterator unsafe_begin() { return this->queue_.begin(); }
iterator unsafe_end() { return this->queue_.end(); }
2021-12-25 10:04:45 +08:00
const_iterator unsafe_begin() const { return this->queue_.begin(); }
2021-12-25 10:04:45 +08:00
const_iterator unsafe_end() const { return this->queue_.end(); }
2021-12-25 10:04:45 +08:00
iterator unsafe_erase(iterator iter) { return this->queue_.erase(iter); }
private:
std::deque<_Ty> queue_;
mutable std::recursive_mutex mtx_;
};
2021-12-25 10:04:45 +08:00
} // namespace cocos2d