Skip to content

Commit

Permalink
Replace base::concurrent_queue for std::deque
Browse files Browse the repository at this point in the history
  • Loading branch information
martincapello committed Jun 7, 2024
1 parent 9357593 commit 7aa8fc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
6 changes: 4 additions & 2 deletions os/osx/event_queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
#define OS_OSX_EVENT_QUEUE_INCLUDED
#pragma once

#include "base/concurrent_queue.h"
#include "os/event.h"
#include "os/event_queue.h"

#include <mutex>
#include <queue>

namespace os {

class EventQueueOSX : public EventQueue {
Expand All @@ -26,7 +28,7 @@ class EventQueueOSX : public EventQueue {
private:
void wakeUpQueue();

base::concurrent_queue<Event> m_events;
std::deque<Event> m_events;
mutable std::mutex m_mutex;
bool m_sleeping;
};
Expand Down
46 changes: 27 additions & 19 deletions os/osx/event_queue.mm
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,38 @@
}
} while (event);

m_mutex.lock();
if (!m_events.try_pop(ev)) {
{
// Note that we don't use the try_lock because we can wait for
// the lock, since there is no long running functions that might get the
// lock for a long time.
const std::lock_guard lock(m_mutex);
if (!m_events.empty()) {
ev = m_events.front();
m_events.pop_front();
return;
}

if (timeout == kWithoutTimeout)
EV_TRACE("EV: Waiting for events\n");

// Wait until there is a Cocoa event in queue
m_sleeping = true;
m_mutex.unlock();
event = [app nextEventMatchingMask:NSEventMaskAny
untilDate:untilDate
inMode:NSDefaultRunLoopMode
dequeue:YES];
m_mutex.lock();
m_sleeping = false;
m_mutex.unlock();

if (event) {
EV_TRACE("EV: Event received!\n");
goto retry;
}
else {
EV_TRACE("EV: Timeout!");
}
}
event = [app nextEventMatchingMask:NSEventMaskAny
untilDate:untilDate
inMode:NSDefaultRunLoopMode
dequeue:YES];
m_mutex.lock();
m_sleeping = false;
m_mutex.unlock();

if (event) {
EV_TRACE("EV: Event received!\n");
goto retry;
}
else {
EV_TRACE("EV: Timeout!");
}
}
}

Expand All @@ -117,7 +124,7 @@
wakeUpQueue();
m_sleeping = false;
}
m_events.push(ev);
m_events.push_back(ev);
}

void EventQueueOSX::wakeUpQueue()
Expand All @@ -142,6 +149,7 @@

void EventQueueOSX::clearEvents()
{
const std::lock_guard lock(m_mutex);
m_events.clear();
}

Expand Down

0 comments on commit 7aa8fc8

Please sign in to comment.