Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
LockedQueue.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef LOCKEDQUEUE_H
7#define LOCKEDQUEUE_H
8
9#include "Debugging/Errors.h"
10#include "Platform/Threading.h"
11#include <assert.h>
12#include <cstddef>
13#include <deque>
14#include <mutex>
15
16namespace Skyfire
17{
18 template <class T, class LockType, typename StorageType = std::deque<T> >
20 {
23
25 StorageType _queue;
26
28 volatile bool _canceled;
29
30 public:
32 LockedQueue() : _canceled(false) { }
33
35 virtual ~LockedQueue() { }
36
38 std::size_t add(const T& item)
39 {
40 std::lock_guard<LockType> g(this->_lock);
41
42 //ASSERT(!this->_canceled);
43 // throw Cancellation_Exception();
44
45 _queue.push_back(item);
46
47 return _queue.size();
48 }
49
51 bool next(T& result)
52 {
53 std::lock_guard<LockType> g(this->_lock);
54
55 if (_queue.empty())
56 return false;
57
58 //ASSERT (!_queue.empty() || !this->_canceled);
59 // throw Cancellation_Exception();
60 result = _queue.front();
61 _queue.pop_front();
62
63 return true;
64 }
65
66 template<class Checker>
67 bool next(T& result, Checker& check)
68 {
69 std::lock_guard<LockType> g(this->_lock);
70
71 if (_queue.empty())
72 return false;
73
74 result = _queue.front();
75 if (!check.Process(result))
76 return false;
77
78 _queue.pop_front();
79 return true;
80 }
81
83 T& peek(bool autoUnlock = false)
84 {
85 lock();
86
87 T& result = _queue.front();
88
89 if (autoUnlock)
90 unlock();
91
92 return result;
93 }
94
96 void cancel()
97 {
98 lock();
99
100 _canceled = true;
101
102 unlock();
103 }
104
107 {
108 std::lock_guard<LockType> g(this->_lock);
109 return _canceled;
110 }
111
113 void lock()
114 {
115 this->_lock.lock();
116 }
117
119 void unlock()
120 {
121 this->_lock.unlock();
122 }
123
126 {
127 std::lock_guard<LockType> g(this->_lock);
128 _queue.pop_front();
129 }
130
132 bool empty()
133 {
134 std::lock_guard<LockType> g(this->_lock);
135 return _queue.empty();
136 }
137
139 std::size_t size()
140 {
141 std::lock_guard<LockType> g(this->_lock);
142 return _queue.size();
143 }
144 };
145}
146
147#endif
LockType
std::size_t add(const T &item)
Adds an item to the queue.
Definition LockedQueue.h:38
bool next(T &result, Checker &check)
Definition LockedQueue.h:67
void lock()
Locks the queue for access.
LockedQueue()
Create a LockedQueue.
Definition LockedQueue.h:32
StorageType _queue
Storage backing the queue.
Definition LockedQueue.h:25
void unlock()
Unlocks the queue.
bool next(T &result)
Gets the next result in the queue, if any.
Definition LockedQueue.h:51
bool cancelled()
Checks if the queue is cancelled.
virtual ~LockedQueue()
Destroy a LockedQueue.
Definition LockedQueue.h:35
void pop_front()
! Calls pop_front of the queue
bool empty()
! Checks if we're empty or not with locks held
LockType _lock
Lock access to the queue.
Definition LockedQueue.h:22
T & peek(bool autoUnlock=false)
Peeks at the top of the queue. Check if the queue is empty before calling! Remember to unlock after u...
Definition LockedQueue.h:83
std::size_t size()
! Returns current queue depth with locks held
void cancel()
Cancels the queue.
Definition LockedQueue.h:96
volatile bool _canceled
Cancellation flag.
Definition LockedQueue.h:28