Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Prioritize thread pause()/resume() calls (Take #2) - #11005

Merged
kkaefer merged 4 commits into
masterfrom
prioritize-thread-pause-resume-2
Feb 6, 2018
Merged

kkaefer merged 4 commits into
masterfrom
prioritize-thread-pause-resume-2

Conversation

@kkaefer

@kkaefer kkaefer commented Jan 24, 2018

Copy link
Copy Markdown
Member

Alternative implementation of #10174 that continues to use the existing model, and just prioritizes pause() calls by adding them to the front of the queue to address @jfirebaugh's concerns in #10174 (review)

Implements part 1 of #10031

@kkaefer kkaefer added the Core The cross-platform C++ core, aka mbgl label Jan 24, 2018
@kkaefer kkaefer changed the title Prioritize thread pause resume 2 Prioritize thread pause()/resume() calls (Take #2) Jan 24, 2018
Comment thread include/mbgl/util/run_loop.hpp Outdated
std::lock_guard<std::mutex> lock(mutex);
fn();
if (priority == Priority::High) {
// We're always placing high priority tasks at the front. This means it's not really a

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in chat, I think it's best to have one set of queue order semantics. For now, it hardly matters as it is only pause/resume, but this might lead to misunderstandings further down the line.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed this by having two separate queues and checking the high priority one first. I chose this implementation over a priority_queue since it removes the need to sort items upon insertion.

std::lock_guard<std::mutex> lock(mutex);
fn();
queue.push(std::move(task));
wake();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could release the lock before calling wake here

@kkaefer
kkaefer force-pushed the prioritize-thread-pause-resume-2 branch from afda6c2 to e993a89 Compare January 26, 2018 01:18
Comment thread include/mbgl/util/run_loop.hpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Update comment to reflect multiple queues

@kkaefer
kkaefer force-pushed the prioritize-thread-pause-resume-2 branch from e993a89 to 9bd5e43 Compare January 26, 2018 20:10

@tmpsantos tmpsantos left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. What about adding an unit test for the priority queue?

@kkaefer

kkaefer commented Feb 5, 2018

Copy link
Copy Markdown
Member Author

@tmpsantos I wanted to do this too, but I couldn't come up with a way to do this without a timing-dependent test which is prone to spurious failures. Do you have any ideas?

@tmpsantos

Copy link
Copy Markdown
Contributor

@tmpsantos I wanted to do this too, but I couldn't come up with a way to do this without a timing-dependent test which is prone to spurious failures. Do you have any ideas?

What about something like this:

  • Have the thread being tested sleep on a std::future::wait()
  • From the main thread, push some default priority tasks to the test thread.
  • From the main thread, push some high priority tasks to the test thread.
  • Release the std::future, this will cause to test thread to reach the main loop and dispatch the task queue.
  • Assert that high priority tasks were dispatched first.

@kkaefer
kkaefer force-pushed the prioritize-thread-pause-resume-2 branch from 9bd5e43 to be41e35 Compare February 6, 2018 17:13
@kkaefer

kkaefer commented Feb 6, 2018 •

Copy link
Copy Markdown
Member Author

Since what we want to test is the RunLoop, and not threading, I ended up writing this test that circumvents any timing issues altogether:

TEST(RunLoop, Priorities) {
std::vector<int> order;
RunLoop loop(RunLoop::Type::New);
loop.invoke([&] { order.push_back(1); });
loop.invoke(RunLoop::Priority::High, [&] { order.push_back(2); });
loop.invoke([&] { order.push_back(3); });
loop.invoke(RunLoop::Priority::High, [&] { order.push_back(4); });
loop.invoke(RunLoop::Priority::Default, [&] { loop.stop(); });
loop.run();
EXPECT_EQ((std::vector<int>{ 2, 4, 1, 3 }), order);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Core The cross-platform C++ core, aka mbgl

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants