~ubuntu-branches/ubuntu/utopic/mir/utopic-proposed

« back to all changes in this revision

Viewing changes to tests/integration-tests/compositor/test_swapping_swappers.cpp

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2014-03-10 19:28:46 UTC
  • mto: This revision was merged to the branch mainline in revision 63.
  • Revision ID: package-import@ubuntu.com-20140310192846-rq9qm3ec26yrelo2
Tags: upstream-0.1.6+14.04.20140310
ImportĀ upstreamĀ versionĀ 0.1.6+14.04.20140310

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
#include <future>
28
28
#include <thread>
29
29
#include <chrono>
 
30
#include <mutex>
 
31
#include <atomic>
30
32
 
31
33
namespace mc = mir::compositor;
32
34
namespace mg = mir::graphics;
49
51
    }
50
52
 
51
53
    std::shared_ptr<mc::SwitchingBundle> switching_bundle;
52
 
    std::atomic<bool> client_thread_done;
 
54
    std::mutex acquire_mutex;  // must live longer than our callback/lambda
 
55
 
 
56
    mg::Buffer* client_acquire_blocking(
 
57
        std::shared_ptr<mc::SwitchingBundle> const& switching_bundle)
 
58
    {
 
59
        std::condition_variable cv;
 
60
        bool acquired = false;
 
61
    
 
62
        mg::Buffer* result;
 
63
        switching_bundle->client_acquire(
 
64
            [&](mg::Buffer* new_buffer)
 
65
             {
 
66
                std::unique_lock<decltype(acquire_mutex)> lock(acquire_mutex);
 
67
    
 
68
                result = new_buffer;
 
69
                acquired = true;
 
70
                cv.notify_one();
 
71
             });
 
72
    
 
73
        std::unique_lock<decltype(acquire_mutex)> lock(acquire_mutex);
 
74
    
 
75
        cv.wait(lock, [&]{ return acquired; });
 
76
    
 
77
        return result;
 
78
    }
53
79
};
54
80
 
55
 
auto client_acquire_blocking(std::shared_ptr<mc::SwitchingBundle> const& switching_bundle)
56
 
-> mg::Buffer*
57
 
{
58
 
    std::mutex mutex;
59
 
    std::condition_variable cv;
60
 
    bool done = false;
61
 
 
62
 
    mg::Buffer* result;
63
 
    switching_bundle->client_acquire(
64
 
        [&](mg::Buffer* new_buffer)
65
 
         {
66
 
            std::unique_lock<decltype(mutex)> lock(mutex);
67
 
 
68
 
            result = new_buffer;
69
 
            done = true;
70
 
            cv.notify_one();
71
 
         });
72
 
 
73
 
    std::unique_lock<decltype(mutex)> lock(mutex);
74
 
 
75
 
    cv.wait(lock, [&]{ return done; });
76
 
 
77
 
    return result;
78
 
}
79
 
}
 
81
} // namespace
80
82
 
81
83
TEST_F(SwapperSwappingStress, swapper)
82
84
{
83
 
    client_thread_done = false;
 
85
    std::atomic_bool done(false);
84
86
 
85
87
    auto f = std::async(std::launch::async,
86
 
                [this]
 
88
                [&]
87
89
                {
88
90
                    for(auto i=0u; i < 400; i++)
89
91
                    {
91
93
                        std::this_thread::yield();
92
94
                        switching_bundle->client_release(b);
93
95
                    }
94
 
                    client_thread_done = true;
 
96
                    done = true;
95
97
                });
96
98
 
97
99
    auto g = std::async(std::launch::async,
98
 
                [this]
 
100
                [&]
99
101
                {
100
102
                    unsigned long count = 0;
101
 
                    while(!client_thread_done)
 
103
                    while (!done)
102
104
                    {
103
105
                        auto b = switching_bundle->compositor_acquire(++count);
104
106
                        std::this_thread::yield();
125
127
 
126
128
TEST_F(SwapperSwappingStress, different_swapper_types)
127
129
{
128
 
    client_thread_done = false;
 
130
    std::atomic_bool done(false);
129
131
 
130
132
    auto f = std::async(std::launch::async,
131
 
                [this]
 
133
                [&]
132
134
                {
133
135
                    for(auto i=0u; i < 400; i++)
134
136
                    {
136
138
                        std::this_thread::yield();
137
139
                        switching_bundle->client_release(b);
138
140
                    }
139
 
                    client_thread_done = true;
 
141
                    done = true;
140
142
                });
141
143
 
142
144
    auto g = std::async(std::launch::async,
143
 
                [this]
 
145
                [&]
144
146
                {
145
147
                    unsigned long count = 0;
146
 
                    while(!client_thread_done)
 
148
                    while (!done)
147
149
                    {
148
150
                        auto b = switching_bundle->compositor_acquire(++count);
149
151
                        std::this_thread::yield();