mirror of
https://github.com/obsproject/obs-studio.git
synced 2024-07-19 09:39:15 +00:00
Code submissions have continually suffered from formatting inconsistencies that constantly have to be addressed. Using clang-format simplifies this by making code formatting more consistent, and allows automation of the code formatting so that maintainers can focus more on the code itself instead of code formatting.
64 lines
1,006 B
C++
64 lines
1,006 B
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <mutex>
|
|
#include <thread>
|
|
|
|
#include <cinttypes>
|
|
|
|
// left_right based on
|
|
// https://github.com/pramalhe/ConcurrencyFreaks/blob/master/papers/left-right-2014.pdf
|
|
// see concurrencyfreaks.com
|
|
|
|
namespace left_right {
|
|
|
|
template<typename T> struct left_right {
|
|
template<typename Func> void update(Func &&f)
|
|
{
|
|
std::lock_guard<std::mutex> lock(write_mutex);
|
|
auto cur = current.load();
|
|
auto next = (cur + 1) & 1;
|
|
|
|
while (readers[next] != 0)
|
|
std::this_thread::yield();
|
|
|
|
f(data[next]);
|
|
current = next;
|
|
|
|
while (readers[cur] != 0)
|
|
std::this_thread::yield();
|
|
|
|
f(data[cur]);
|
|
}
|
|
|
|
T read()
|
|
{
|
|
auto cur = current.load();
|
|
for (;;) {
|
|
readers[cur] += 1;
|
|
|
|
auto cur_ = current.load();
|
|
if (cur_ == cur)
|
|
break;
|
|
|
|
readers[cur] -= 1;
|
|
cur = cur_;
|
|
}
|
|
|
|
auto val = data[cur];
|
|
|
|
readers[cur] -= 1;
|
|
|
|
return val;
|
|
}
|
|
|
|
private:
|
|
std::atomic_uint_fast8_t current;
|
|
std::atomic_long readers[2];
|
|
std::mutex write_mutex;
|
|
|
|
T data[2] = {{}, {}};
|
|
};
|
|
|
|
}
|