CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Common/Data/Collections/ThreadSafeList.h
Views: 1401
1
// Copyright (c) 2015- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#pragma once
19
20
#include <list>
21
22
template < typename T, class Alloc = std::allocator<T> >
23
class ThreadSafeList {
24
public:
25
explicit ThreadSafeList(const Alloc &a = Alloc()) : list(a) {}
26
explicit ThreadSafeList(std::size_t n, const T &v = T(), const Alloc &a = Alloc()) : list(n, v, a) {}
27
ThreadSafeList(const std::list<T, Alloc> &other) : list(other) {}
28
ThreadSafeList(const ThreadSafeList &other) {
29
std::lock_guard<std::mutex> guard(other.lock);
30
list.assign(other.list);
31
}
32
33
template <class Iter>
34
ThreadSafeList(Iter first, Iter last, const Alloc &a = Alloc()) : list(first, last, a) {}
35
36
inline T front() const {
37
std::lock_guard<std::mutex> guard(lock);
38
return list.front();
39
}
40
41
inline void pop_front() {
42
std::lock_guard<std::mutex> guard(lock);
43
return list.pop_front();
44
}
45
46
inline void push_front(const T &v) {
47
std::lock_guard<std::mutex> guard(lock);
48
return list.push_front(v);
49
}
50
51
inline T back() const {
52
std::lock_guard<std::mutex> guard(lock);
53
return list.back();
54
}
55
56
inline void pop_back() {
57
std::lock_guard<std::mutex> guard(lock);
58
return list.pop_back();
59
}
60
61
inline void push_back(const T &v) {
62
std::lock_guard<std::mutex> guard(lock);
63
return list.push_back(v);
64
}
65
66
bool empty() const {
67
std::lock_guard<std::mutex> guard(lock);
68
return list.empty();
69
}
70
71
inline void clear() {
72
std::lock_guard<std::mutex> guard(lock);
73
return list.clear();
74
}
75
76
void DoState(PointerWrap &p) {
77
std::lock_guard<std::mutex> guard(lock);
78
Do(p, list);
79
}
80
81
private:
82
mutable std::mutex lock;
83
std::list<T, Alloc> list;
84
};
85
86
87