1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/*
* Copyright © Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 or 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "mir/test/doubles/triggered_main_loop.h"
#include "mir/test/doubles/stub_alarm.h"
#include <algorithm>
#include <sys/select.h>
namespace mtd = mir::test::doubles;
using base = ::testing::NiceMock<mtd::MockMainLoop>;
void mtd::TriggeredMainLoop::register_fd_handler(std::initializer_list<int> fds, void const* owner, fd_callback const& handler)
{
base::register_fd_handler(fds, owner, handler);
for (int fd : fds)
{
fd_callbacks.emplace_back(Item{fd, owner, handler});
}
}
void mtd::TriggeredMainLoop::unregister_fd_handler(void const* owner)
{
base::unregister_fd_handler(owner);
fd_callbacks.erase(
remove_if(
begin(fd_callbacks),
end(fd_callbacks),
[owner](Item const& item)
{
return item.owner == owner;
}),
end(fd_callbacks)
);
}
void mtd::TriggeredMainLoop::trigger_pending_fds()
{
fd_set read_fds;
FD_ZERO(&read_fds);
int max_fd = 0;
for (auto const & item : fd_callbacks)
{
FD_SET(item.fd, &read_fds);
max_fd = std::max(item.fd, max_fd);
}
struct timeval do_not_wait{0, 0};
if (select(max_fd+1, &read_fds, nullptr, nullptr, &do_not_wait))
{
for (auto const& item : fd_callbacks)
{
if (FD_ISSET(item.fd, &read_fds))
{
item.callback(item.fd);
}
}
}
}
void mtd::TriggeredMainLoop::enqueue(void const* owner, ServerAction const& action)
{
base::enqueue(owner, action);
actions.push_back(action);
}
void mtd::TriggeredMainLoop::enqueue_with_guaranteed_execution(ServerAction const& action)
{
base::enqueue(nullptr, action);
actions.push_back(action);
}
void mtd::TriggeredMainLoop::trigger_server_actions()
{
for (auto const& action : actions)
action();
actions.clear();
}
std::unique_ptr<mir::time::Alarm>
mtd::TriggeredMainLoop::create_alarm(callback const& call)
{
base::create_alarm(call);
timeout_callbacks.push_back(call);
return std::unique_ptr<mir::time::Alarm>{new mtd::StubAlarm};
}
void mtd::TriggeredMainLoop::fire_all_alarms()
{
for(auto const& callback : timeout_callbacks)
callback();
}
void mtd::TriggeredMainLoop::spawn(std::function<void()>&& work)
{
base::spawn(std::function<void()>{work});
this->work.emplace_back(std::move(work));
}
void mtd::TriggeredMainLoop::trigger_spawned_work()
{
for (auto const& action : work)
{
action();
}
work.clear();
}
|