mirror of
https://github.com/lubosz/overte.git
synced 2025-04-29 20:43:12 +02:00
Refactoring escrow due to long unsignaled syncs reported by Philip
This commit is contained in:
parent
64dd36d140
commit
04b8251a83
1 changed files with 57 additions and 39 deletions
|
@ -49,35 +49,32 @@ template <
|
||||||
>
|
>
|
||||||
class GLEscrow {
|
class GLEscrow {
|
||||||
public:
|
public:
|
||||||
|
static const uint64_t MAX_UNSIGNALED_TIME = USECS_PER_SECOND / 2;
|
||||||
|
|
||||||
struct Item {
|
struct Item {
|
||||||
T _value;
|
const T _value;
|
||||||
GLsync _sync;
|
GLsync _sync;
|
||||||
uint64_t _created;
|
const uint64_t _created;
|
||||||
|
|
||||||
Item(T value, GLsync sync) :
|
Item(T value, GLsync sync) :
|
||||||
_value(value), _sync(sync), _created(usecTimestampNow())
|
_value(value), _sync(sync), _created(usecTimestampNow())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t age() {
|
uint64_t age() const {
|
||||||
return usecTimestampNow() - _created;
|
return usecTimestampNow() - _created;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool signaled() {
|
bool signaled() const {
|
||||||
auto result = glClientWaitSync(_sync, 0, 0);
|
auto result = glClientWaitSync(_sync, 0, 0);
|
||||||
if (GL_TIMEOUT_EXPIRED != result && GL_WAIT_FAILED != result) {
|
if (GL_TIMEOUT_EXPIRED != result && GL_WAIT_FAILED != result) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (age() > (USECS_PER_SECOND / 2)) {
|
|
||||||
qWarning() << "Long unsignaled sync";
|
|
||||||
}
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
using Mutex = std::recursive_mutex;
|
using Mutex = std::mutex;
|
||||||
using Lock = std::unique_lock<Mutex>;
|
|
||||||
using Recycler = std::function<void(T t)>;
|
using Recycler = std::function<void(T t)>;
|
||||||
// deque gives us random access, double ended push & pop and size, all in constant time
|
// deque gives us random access, double ended push & pop and size, all in constant time
|
||||||
using Deque = std::deque<Item>;
|
using Deque = std::deque<Item>;
|
||||||
|
@ -87,9 +84,32 @@ public:
|
||||||
_recycler = recycler;
|
_recycler = recycler;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t depth() {
|
template <typename F>
|
||||||
|
void withLock(F f) {
|
||||||
|
using Lock = std::unique_lock<Mutex>;
|
||||||
Lock lock(_mutex);
|
Lock lock(_mutex);
|
||||||
return _submits.size();
|
f();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename F>
|
||||||
|
bool tryLock(F f) {
|
||||||
|
using Lock = std::unique_lock<Mutex>;
|
||||||
|
bool result = false;
|
||||||
|
Lock lock(_mutex, std::try_to_lock_t());
|
||||||
|
if (lock.owns_lock()) {
|
||||||
|
f();
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
size_t depth() {
|
||||||
|
size_t result{ 0 };
|
||||||
|
withLock([&]{
|
||||||
|
result = _submits.size();
|
||||||
|
});
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Submit a new resource from the producer context
|
// Submit a new resource from the producer context
|
||||||
|
@ -104,11 +124,9 @@ public:
|
||||||
glFlush();
|
glFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
withLock([&]{
|
||||||
Lock lock(_mutex);
|
|
||||||
_submits.push_back(Item(t, writeSync));
|
_submits.push_back(Item(t, writeSync));
|
||||||
}
|
});
|
||||||
|
|
||||||
return cleanTrash();
|
return cleanTrash();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -120,13 +138,13 @@ public:
|
||||||
// On the one hand using try_lock() reduces the chance of blocking the consumer thread,
|
// On the one hand using try_lock() reduces the chance of blocking the consumer thread,
|
||||||
// but if the produce thread is going fast enough, it could effectively
|
// but if the produce thread is going fast enough, it could effectively
|
||||||
// starve the consumer out of ever actually getting resources.
|
// starve the consumer out of ever actually getting resources.
|
||||||
if (_mutex.try_lock()) {
|
tryLock([&]{
|
||||||
|
// May be called on any thread, but must be inside a locked section
|
||||||
if (signaled(_submits, 0)) {
|
if (signaled(_submits, 0)) {
|
||||||
result = _submits.at(0)._value;
|
result = _submits.at(0)._value;
|
||||||
_submits.pop_front();
|
_submits.pop_front();
|
||||||
}
|
}
|
||||||
_mutex.unlock();
|
});
|
||||||
}
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -154,36 +172,44 @@ public:
|
||||||
glFlush();
|
glFlush();
|
||||||
}
|
}
|
||||||
|
|
||||||
Lock lock(_mutex);
|
withLock([&]{
|
||||||
_releases.push_back(Item(t, readSync));
|
_releases.push_back(Item(t, readSync));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t cleanTrash() {
|
size_t cleanTrash() {
|
||||||
size_t wastedWork{ 0 };
|
size_t wastedWork{ 0 };
|
||||||
List trash;
|
List trash;
|
||||||
{
|
tryLock([&]{
|
||||||
|
while (!_submits.empty()) {
|
||||||
|
const auto& item = _submits.front();
|
||||||
|
if (!item._sync || item.age() < MAX_UNSIGNALED_TIME) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
qWarning() << "Long unsignaled sync " << item._sync << " unsignaled for " << item.age();
|
||||||
|
_trash.push_front(item);
|
||||||
|
_submits.pop_front();
|
||||||
|
}
|
||||||
|
|
||||||
// We only ever need one ready item available in the list, so if the
|
// We only ever need one ready item available in the list, so if the
|
||||||
// second item is signaled (implying the first is as well, remove the first
|
// second item is signaled (implying the first is as well, remove the first
|
||||||
// item. Iterate until the SECOND item in the list is not in the ready state
|
// item. Iterate until the SECOND item in the list is not in the ready state
|
||||||
// The signaled function takes care of checking against the deque size
|
// The signaled function takes care of checking against the deque size
|
||||||
while (signaled(_submits, 1)) {
|
while (signaled(_submits, 1)) {
|
||||||
pop(_submits);
|
_trash.push_front(_submits.front());
|
||||||
|
_submits.pop_front();
|
||||||
++wastedWork;
|
++wastedWork;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stuff in the release queue can be cleared out as soon as it's signaled
|
// Stuff in the release queue can be cleared out as soon as it's signaled
|
||||||
while (signaled(_releases, 0)) {
|
while (signaled(_releases, 0)) {
|
||||||
pop(_releases);
|
_trash.push_front(_releases.front());
|
||||||
|
_releases.pop_front();
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
|
||||||
// FIXME I don't think this lock should be necessary, only the submitting thread
|
|
||||||
// touches the trash
|
|
||||||
Lock lock(_mutex);
|
|
||||||
trash.swap(_trash);
|
trash.swap(_trash);
|
||||||
}
|
});
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME maybe doing a timing on the deleters and warn if it's taking excessive time?
|
// FIXME maybe doing a timing on the deleters and warn if it's taking excessive time?
|
||||||
// although we are out of the lock, so it shouldn't be blocking anything
|
// although we are out of the lock, so it shouldn't be blocking anything
|
||||||
|
@ -198,14 +224,6 @@ private:
|
||||||
return wastedWork;
|
return wastedWork;
|
||||||
}
|
}
|
||||||
|
|
||||||
// May be called on any thread, but must be inside a locked section
|
|
||||||
void pop(Deque& deque) {
|
|
||||||
Lock lock(_mutex);
|
|
||||||
auto& item = deque.front();
|
|
||||||
_trash.push_front(item);
|
|
||||||
deque.pop_front();
|
|
||||||
}
|
|
||||||
|
|
||||||
// May be called on any thread, but must be inside a locked section
|
// May be called on any thread, but must be inside a locked section
|
||||||
bool signaled(Deque& deque, size_t i) {
|
bool signaled(Deque& deque, size_t i) {
|
||||||
if (i >= deque.size()) {
|
if (i >= deque.size()) {
|
||||||
|
|
Loading…
Reference in a new issue