MCPcopy Create free account
hub / github.com/celtera/libremidi / basic_queue

Class basic_queue

examples/qmidiin.cpp:33–85  ·  view source on GitHub ↗

Example implementation of a queue NOTE: do not use, instead use a proper lock-free queue! e.g. https://github.com/max0x7ba/atomic_queue or https://github.com/cameron314/readerwriterqueue

Source from the content-addressed store, hash-verified

31// e.g. https://github.com/max0x7ba/atomic_queue or
32// https://github.com/cameron314/readerwriterqueue
33struct basic_queue
34{
35 unsigned int front{};
36 unsigned int back{};
37 unsigned int ringSize{};
38 std::unique_ptr<message[]> ring{};
39
40 bool push(const message& msg)
41 {
42 auto [sz, _, b] = get_dimensions();
43
44 if (sz < ringSize - 1)
45 {
46 ring[b] = msg;
47 back = (back + 1) % ringSize;
48 return true;
49 }
50
51 return false;
52 }
53
54 bool pop(message& msg)
55 {
56 auto [sz, f, _] = get_dimensions();
57
58 if (sz == 0)
59 {
60 return false;
61 }
62
63 // Copy queued message to the vector pointer argument and then "pop" it.
64 using namespace std;
65 swap(msg, ring[f]);
66
67 // Update front
68 front = (front + 1) % ringSize;
69 return true;
70 }
71
72 struct dimensions
73 {
74 unsigned int size, front, back;
75 };
76
77 dimensions get_dimensions() const
78 {
79 // Access back/front members exactly once and make stack copies for
80 // size calculation ==> completely unneccessary
81 // https://godbolt.org/g/HPu9LA
82
83 return {(back >= front) ? back - front : ringSize - front + back, front, back};
84 }
85};
86
87// Example of how to get back the old queue-based API
88struct queued_midi_in

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected