Mir

Merge lp:~vanvugt/mir/less-like-infinite-loops into lp:mir

Proposed by Daniel van Vugt
Status: Rejected
Rejected by: Daniel van Vugt
Proposed branch: lp:~vanvugt/mir/less-like-infinite-loops
Merge into: lp:mir
Diff against target: 36 lines (+7/-4)
1 file modified
src/server/compositor/stream.cpp (+7/-4)
To merge this branch: bzr merge lp:~vanvugt/mir/less-like-infinite-loops
Reviewer Review Type Date Requested Status
Chris Halse Rogers Abstain
Alan Griffiths Disapprove
Cemil Azizoglu (community) Needs Information
Mir CI Bot continuous-integration Approve
Review via email: mp+313753@code.launchpad.net

Commit message

Reword some while loops in compositor::Stream that look worryingly
like they could potentially be infinite loops. Only looking farther
do you realise that they are finite by virtue of scheduling always
occurring under the same lock as the loop.

So this isn't required, but it will cause less concern for future
readers and is more efficient anyway.

To post a comment you must log in.
Revision history for this message
Mir CI Bot (mir-ci-bot) wrote :

PASSED: Continuous integration, rev:3898
https://mir-jenkins.ubuntu.com/job/mir-ci/2480/
Executed test runs:
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-mir/3234
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-0-fetch/3301
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-1-sourcepkg/release=vivid+overlay/3293
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-1-sourcepkg/release=xenial+overlay/3293
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-1-sourcepkg/release=yakkety/3293
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=clang,platform=mesa,release=yakkety/3263
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=clang,platform=mesa,release=yakkety/3263/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=xenial+overlay/3263
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=xenial+overlay/3263/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=yakkety/3263
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=amd64,compiler=gcc,platform=mesa,release=yakkety/3263/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=cross-armhf,compiler=gcc,platform=android,release=vivid+overlay/3263
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=cross-armhf,compiler=gcc,platform=android,release=vivid+overlay/3263/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=i386,compiler=gcc,platform=android,release=vivid+overlay/3263
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=i386,compiler=gcc,platform=android,release=vivid+overlay/3263/artifact/output/*zip*/output.zip
    SUCCESS: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=i386,compiler=gcc,platform=mesa,release=xenial+overlay/3263
        deb: https://mir-jenkins.ubuntu.com/job/build-2-binpkg-mir/arch=i386,compiler=gcc,platform=mesa,release=xenial+overlay/3263/artifact/output/*zip*/output.zip

Click here to trigger a rebuild:
https://mir-jenkins.ubuntu.com/job/mir-ci/2480/rebuild

review: Approve (continuous-integration)
Revision history for this message
Cemil Azizoglu (cemil-azizoglu) wrote :

The while-loop and the for-loop now don't have the same semantics, depending on whether or not schedule->num_scheduled() could change during iterations.

- while(schedule->num_scheduled())
9 + unsigned n = schedule->num_scheduled();
10 + for (unsigned i = 0; i < n; ++i)

review: Needs Information
Revision history for this message
Daniel van Vugt (vanvugt) wrote :

Correct, but what I was trying to say in the commit message was that the locking around the loops stops num_sheduled from ever changing during the loops so for and while are the same here.

It's not a guarantee that can survive all future accidental code changes, but it is guaranteed for now. This change aims to stop the loops from becoming infinite/indefinite loops if anyone ever accidentally changes the locking structure. And this change is a bit more efficient too.

Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

The "after" code is less clear than the "before".

review: Disapprove
Revision history for this message
Chris Halse Rogers (raof) wrote :

Weak disapprove; this changes the implied intent. I believe the behaviour we intend is “while there are scheduled buffers, do something” rather than “for each buffer scheduled before the loop starts, do something”.

review: Abstain

Unmerged revisions

3898. By Daniel van Vugt

Prototype

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/server/compositor/stream.cpp'
2--- src/server/compositor/stream.cpp 2016-11-17 08:45:49 +0000
3+++ src/server/compositor/stream.cpp 2016-12-22 10:08:26 +0000
4@@ -76,7 +76,8 @@
5
6 mc::Stream::~Stream()
7 {
8- while(schedule->num_scheduled())
9+ unsigned n = schedule->num_scheduled();
10+ for (unsigned i = 0; i < n; ++i)
11 buffers->send_buffer(schedule->next_buffer()->id());
12 }
13
14@@ -173,9 +174,10 @@
15 std::shared_ptr<mc::Schedule>&& new_schedule, std::lock_guard<std::mutex> const&)
16 {
17 std::vector<std::shared_ptr<mg::Buffer>> transferred_buffers;
18- while(schedule->num_scheduled())
19+ unsigned n = schedule->num_scheduled();
20+ for (unsigned i = 0; i < n; ++i)
21 transferred_buffers.emplace_back(schedule->next_buffer());
22- for(auto& buffer : transferred_buffers)
23+ for (auto& buffer : transferred_buffers)
24 new_schedule->schedule(buffer);
25 schedule = new_schedule;
26 arbiter->set_schedule(schedule);
27@@ -198,7 +200,8 @@
28 {
29 std::lock_guard<decltype(mutex)> lk(mutex);
30 std::vector<std::shared_ptr<mg::Buffer>> transferred_buffers;
31- while(schedule->num_scheduled())
32+ unsigned n = schedule->num_scheduled();
33+ for (unsigned i = 0; i < n; ++i)
34 transferred_buffers.emplace_back(schedule->next_buffer());
35
36 if (!transferred_buffers.empty())

Subscribers

People subscribed via source and target branches