2
* Copyright © 2015 Canonical Ltd.
4
* This program is free software: you can redistribute it and/or modify it
5
* under the terms of the GNU Lesser General Public License version 3,
6
* as published by the Free Software Foundation.
8
* This program is distributed in the hope that it will be useful,
9
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
* GNU Lesser General Public License for more details.
13
* You should have received a copy of the GNU Lesser General Public License
14
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16
* Authored by: Andreas Pokorny <andreas.pokorny@canonical.com>
19
#include "mir/dispatch/action_queue.h"
21
#include <boost/throw_exception.hpp>
22
#include <sys/eventfd.h>
24
mir::dispatch::ActionQueue::ActionQueue()
25
: event_fd{eventfd(0, EFD_CLOEXEC|EFD_NONBLOCK)}
28
BOOST_THROW_EXCEPTION((std::system_error{errno,
29
std::system_category(),
30
"Failed to create event fd for action queue"}));
33
mir::Fd mir::dispatch::ActionQueue::watch_fd() const
38
void mir::dispatch::ActionQueue::enqueue(std::function<void()> const& action)
40
std::unique_lock<std::mutex> lock(list_lock);
41
actions.push_back(action);
45
bool mir::dispatch::ActionQueue::dispatch(FdEvents events)
47
if (events&FdEvent::error)
50
std::list<std::function<void()>> actions_to_process;
53
std::unique_lock<std::mutex> lock(list_lock);
55
std::swap(actions_to_process, actions);
58
while(!actions_to_process.empty())
60
actions_to_process.front()();
61
actions_to_process.pop_front();
66
mir::dispatch::FdEvents mir::dispatch::ActionQueue::relevant_events() const
68
return FdEvent::readable;
71
void mir::dispatch::ActionQueue::consume()
74
if (read(event_fd, &num_actions, sizeof num_actions) != sizeof num_actions)
75
BOOST_THROW_EXCEPTION((std::system_error{errno,
76
std::system_category(),
77
"Failed to consume action queue notification"}));
80
void mir::dispatch::ActionQueue::wake()
83
if (write(event_fd, &one_more, sizeof one_more) != sizeof one_more)
84
BOOST_THROW_EXCEPTION((std::system_error{errno,
85
std::system_category(),
86
"Failed to wake action queue"}));