OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
lockfree_queue.hpp
1#pragma once
2
3// TSAN seems to have some trouble with ReaderWriterQueue...
4#if !defined(OSSIA_FREESTANDING)
5
6#include <blockingconcurrentqueue.h>
7#include <concurrentqueue.h>
8
9// SPSC
10#if defined(__has_feature)
11#if __has_feature(thread_sanitizer)
12namespace ossia
13{
14template <typename T, size_t MAX_BLOCK_SIZE = 512>
15using spsc_queue = moodycamel::ConcurrentQueue<T>;
16}
17#else
18#include <readerwriterqueue.h>
19namespace ossia
20{
21template <typename T, size_t MAX_BLOCK_SIZE = 512>
22using spsc_queue = moodycamel::ReaderWriterQueue<T, MAX_BLOCK_SIZE>;
23}
24#endif
25#else
26#include <readerwriterqueue.h>
27namespace ossia
28{
29template <typename T, size_t MAX_BLOCK_SIZE = 512>
30using spsc_queue = moodycamel::ReaderWriterQueue<T, MAX_BLOCK_SIZE>;
31}
32#endif
33
34// MPMC
35namespace ossia
36{
37template <typename T>
38using mpmc_queue = moodycamel::ConcurrentQueue<T>;
39template <typename T>
40using blocking_mpmc_queue = moodycamel::BlockingConcurrentQueue<T>;
41}
42
43#else
44// Will only be used on one thread anyways
45#include <vector>
46namespace ossia
47{
48template <typename T>
49struct minimal_queue
50{
51public:
52 bool try_dequeue(T& t)
53 {
54 if(impl.empty())
55 return false;
56 t = std::move(impl.back());
57 impl.pop_back();
58 return true;
59 }
60
61 void enqueue(T&& t) { impl.insert(impl.begin(), std::move(t)); }
62 int size_approx() const noexcept { return impl.size(); }
63
64private:
65 std::vector<T> impl;
66};
67template <typename T>
68using spsc_queue = ossia::minimal_queue<T>;
69template <typename T>
70using mpmc_queue = ossia::minimal_queue<T>;
71template <typename T>
72using blocking_mpmc_queue = ossia::minimal_queue<T>;
73}
74#endif
Definition git_info.h:7