-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocklessQueue.hpp
More file actions
41 lines (35 loc) · 1008 Bytes
/
LocklessQueue.hpp
File metadata and controls
41 lines (35 loc) · 1008 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef LOCKLESS_QUEUE_HPP
#define LOCKLESS_QUEUE_HPP
#include <atomic>
#include <vector>
#include <mutex>
#include <utility>
#include <exception>
#include "LocklessSemaphore.hpp"
#include "Queue.hpp"
template <class T>
class LocklessQueue : public Queue<T> {
LocklessSemaphore count_sem;
LocklessSemaphore empty_sem;
std::atomic<bool> dequeue_atom;
std::atomic<bool> enqueue_atom;
std::vector<std::atomic<T>> *items;
size_t capacity;
size_t max_size;
size_t head; // Items queued on head. (back of vector)
size_t tail; // Items dequeued off tail. (front of vector)
std::atomic<bool> is_shutdown;
std::mutex expand_lock;
std::pair<long, long> drain_semaphores();
void fill_semaphores(std::pair<long, long> &p);
public:
LocklessQueue(size_t n);
LocklessQueue(size_t n, size_t max);
virtual ~LocklessQueue();
void enqueue(T x);
T dequeue();
void expand();
void shutdown();
};
#include "LocklessQueue.cpp"
#endif