CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/Common/Data/Collections/ThreadSafeList.h
Views: 1401
// Copyright (c) 2015- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#pragma once1819#include <list>2021template < typename T, class Alloc = std::allocator<T> >22class ThreadSafeList {23public:24explicit ThreadSafeList(const Alloc &a = Alloc()) : list(a) {}25explicit ThreadSafeList(std::size_t n, const T &v = T(), const Alloc &a = Alloc()) : list(n, v, a) {}26ThreadSafeList(const std::list<T, Alloc> &other) : list(other) {}27ThreadSafeList(const ThreadSafeList &other) {28std::lock_guard<std::mutex> guard(other.lock);29list.assign(other.list);30}3132template <class Iter>33ThreadSafeList(Iter first, Iter last, const Alloc &a = Alloc()) : list(first, last, a) {}3435inline T front() const {36std::lock_guard<std::mutex> guard(lock);37return list.front();38}3940inline void pop_front() {41std::lock_guard<std::mutex> guard(lock);42return list.pop_front();43}4445inline void push_front(const T &v) {46std::lock_guard<std::mutex> guard(lock);47return list.push_front(v);48}4950inline T back() const {51std::lock_guard<std::mutex> guard(lock);52return list.back();53}5455inline void pop_back() {56std::lock_guard<std::mutex> guard(lock);57return list.pop_back();58}5960inline void push_back(const T &v) {61std::lock_guard<std::mutex> guard(lock);62return list.push_back(v);63}6465bool empty() const {66std::lock_guard<std::mutex> guard(lock);67return list.empty();68}6970inline void clear() {71std::lock_guard<std::mutex> guard(lock);72return list.clear();73}7475void DoState(PointerWrap &p) {76std::lock_guard<std::mutex> guard(lock);77Do(p, list);78}7980private:81mutable std::mutex lock;82std::list<T, Alloc> list;83};84858687