Mir
glib_main_loop_sources.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2014-2015 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
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 General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
17  */
18 
19 #ifndef MIR_GLIB_MAIN_LOOP_SOURCES_H_
20 #define MIR_GLIB_MAIN_LOOP_SOURCES_H_
21 
22 #include "mir/time/clock.h"
23 #include "mir/thread_safe_list.h"
24 #include "mir/fd.h"
25 
26 #include <functional>
27 #include <vector>
28 #include <mutex>
29 #include <memory>
30 #include <unordered_map>
31 
32 #include <glib.h>
33 
34 namespace mir
35 {
36 namespace detail
37 {
38 
40 {
41 public:
42  GSourceHandle();
43  GSourceHandle(GSource* gsource, std::function<void(GSource*)> const& pre_destruction_hook);
47 
48  operator GSource*() const;
49 
50 private:
51  GSource* gsource;
52  std::function<void(GSource*)> pre_destruction_hook;
53 };
54 
55 void add_idle_gsource(
56  GMainContext* main_context, int priority, std::function<void()> const& callback);
57 
59  GMainContext* main_context,
60  void const* owner,
61  std::function<void()> const& action,
62  std::function<bool(void const*)> const& should_dispatch);
63 
65  GMainContext* main_context,
66  std::shared_ptr<time::Clock> const& clock,
67  std::function<void()> const& handler,
68  std::function<void()> const& lock,
69  std::function<void()> const& unlock,
70  time::Timestamp target_time);
71 
72 class FdSources
73 {
74 public:
75  FdSources(GMainContext* main_context);
76  ~FdSources();
77 
78  void add(int fd, void const* owner, std::function<void(int)> const& handler);
79  void remove_all_owned_by(void const* owner);
80 
81 private:
82  struct FdContext;
83  struct FdSource;
84 
85  GMainContext* const main_context;
86  std::mutex sources_mutex;
87  std::vector<std::unique_ptr<FdSource>> sources;
88 };
89 
91 {
92 public:
93  SignalSources(FdSources& fd_sources);
95 
96  void add(std::vector<int> const& sigs, std::function<void(int)> const& handler);
97 
98 private:
99  class SourceRegistration;
100  struct HandlerElement
101  {
102  operator bool() const { return !!handler; }
103  std::vector<int> sigs;
104  std::function<void(int)> handler;
105  };
106 
107  void dispatch_pending_signal();
108  void ensure_signal_is_handled(int sig);
109  int read_pending_signal();
110  void dispatch_signal(int sig);
111 
112  FdSources& fd_sources;
113  mir::Fd signal_read_fd;
114  mir::Fd signal_write_fd;
116  std::mutex handled_signals_mutex;
117  std::unordered_map<int, struct sigaction> handled_signals;
118  std::unique_ptr<SourceRegistration> source_registration;
119 };
120 
121 }
122 }
123 
124 #endif
All things Mir.
Definition: buffer_stream.h:37
SignalSources(FdSources &fd_sources)
GSourceHandle()
Definition: glib_main_loop_sources.cpp:77
~GSourceHandle()
Definition: glib_main_loop_sources.cpp:105
Definition: fd.h:33
void add(std::vector< int > const &sigs, std::function< void(int)> const &handler)
std::chrono::steady_clock::time_point Timestamp
Definition: types.h:29
void add_idle_gsource(GMainContext *main_context, int priority, std::function< void()> const &callback)
GSourceHandle add_timer_gsource(GMainContext *main_context, std::shared_ptr< time::Clock > const &clock, std::function< void()> const &handler, std::function< void()> const &lock, std::function< void()> const &unlock, time::Timestamp target_time)
GSourceHandle & operator=(GSourceHandle other)
Definition: glib_main_loop_sources.cpp:98
Definition: glib_main_loop_sources.h:39
Definition: glib_main_loop_sources.h:72
Definition: glib_main_loop_sources.h:90
void add(int fd, void const *owner, std::function< void(int)> const &handler)
void add_server_action_gsource(GMainContext *main_context, void const *owner, std::function< void()> const &action, std::function< bool(void const *)> const &should_dispatch)
void remove_all_owned_by(void const *owner)
FdSources(GMainContext *main_context)

Copyright © 2012,2013 Canonical Ltd.
Generated on Tue Mar 24 16:15:19 UTC 2015