File: FifoQueue.h

package info (click to toggle)
dolphin-emu 5.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 29,052 kB
  • sloc: cpp: 213,146; java: 6,252; asm: 2,277; xml: 1,998; ansic: 1,514; python: 462; sh: 279; pascal: 247; makefile: 124; perl: 97
file content (125 lines) | stat: -rw-r--r-- 2,258 bytes parent folder | download | duplicates (2)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2010 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.

#pragma once

// a simple lockless thread-safe,
// single reader, single writer queue

#include <algorithm>
#include <atomic>
#include <cstddef>

#include "Common/CommonTypes.h"

namespace Common
{

template <typename T, bool NeedSize = true>
class FifoQueue
{
public:
	FifoQueue() : m_size(0)
	{
		 m_write_ptr = m_read_ptr = new ElementPtr();
	}

	~FifoQueue()
	{
		// this will empty out the whole queue
		delete m_read_ptr;
	}

	u32 Size() const
	{
		static_assert(NeedSize, "using Size() on FifoQueue without NeedSize");
		return m_size.load();
	}

	bool Empty() const
	{
		return !m_read_ptr->next.load();
	}

	T& Front() const
	{
		return m_read_ptr->current;
	}

	template <typename Arg>
	void Push(Arg&& t)
	{
		// create the element, add it to the queue
		m_write_ptr->current = std::forward<Arg>(t);
		// set the next pointer to a new element ptr
		// then advance the write pointer
		ElementPtr* new_ptr = new ElementPtr();
		m_write_ptr->next.store(new_ptr, std::memory_order_release);
		m_write_ptr = new_ptr;
		if (NeedSize)
			m_size++;
	}

	void Pop()
	{
		if (NeedSize)
			m_size--;
		ElementPtr* tmpptr = m_read_ptr;
		// advance the read pointer
		m_read_ptr = tmpptr->next.load();
		// set the next element to nullptr to stop the recursive deletion
		tmpptr->next.store(nullptr);
		delete tmpptr; // this also deletes the element
	}

	bool Pop(T& t)
	{
		if (Empty())
			return false;

		if (NeedSize)
			m_size--;

		ElementPtr* tmpptr = m_read_ptr;
		m_read_ptr = tmpptr->next.load(std::memory_order_acquire);
		t = std::move(tmpptr->current);
		tmpptr->next.store(nullptr);
		delete tmpptr;
		return true;
	}

	// not thread-safe
	void Clear()
	{
		m_size.store(0);
		delete m_read_ptr;
		m_write_ptr = m_read_ptr = new ElementPtr();
	}

private:
	// stores a pointer to element
	// and a pointer to the next ElementPtr
	class ElementPtr
	{
	public:
		ElementPtr() : next(nullptr) {}

		~ElementPtr()
		{
			ElementPtr* next_ptr = next.load();

			if (next_ptr)
				delete next_ptr;
		}

		T current;
		std::atomic<ElementPtr*> next;
	};

	ElementPtr* m_write_ptr;
	ElementPtr* m_read_ptr;
	std::atomic<u32> m_size;
};

}