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 #if defined(__clang__)
33 #pragma clang diagnostic push
34 #pragma clang diagnostic ignored "-Wdeprecated-register"
35 #endif
36 
37 #include <glib.h>
38 
39 #if defined(__clang__)
40 #pragma clang diagnostic pop
41 #endif
42 
43 namespace mir
44 {
45 class LockableCallback;
46 namespace detail
47 {
48 
50 {
51 public:
52  GSourceHandle();
53  GSourceHandle(GSource* gsource, std::function<void(GSource*)> const& terminate_dispatch);
57 
59 
60  operator GSource*() const;
61 
62 private:
63  GSource* gsource;
64  std::function<void(GSource*)> terminate_dispatch;
65 };
66 
67 void add_idle_gsource(
68  GMainContext* main_context, int priority, std::function<void()> const& callback);
69 
71  GMainContext* main_context,
72  void const* owner,
73  std::function<void()> const& action,
74  std::function<bool(void const*)> const& should_dispatch);
75 
77  GMainContext* main_context,
78  std::shared_ptr<time::Clock> const& clock,
79  std::shared_ptr<LockableCallback> const& handler,
80  std::function<void()> const& exception_handler,
81  time::Timestamp target_time);
82 
83 class FdSources
84 {
85 public:
86  FdSources(GMainContext* main_context);
87  ~FdSources();
88 
89  void add(int fd, void const* owner, std::function<void(int)> const& handler);
90  void remove_all_owned_by(void const* owner);
91 
92 private:
93  struct FdContext;
94  struct FdSource;
95 
96  GMainContext* const main_context;
97  std::mutex sources_mutex;
98  std::vector<std::unique_ptr<FdSource>> sources;
99 };
100 
102 {
103 public:
104  SignalSources(FdSources& fd_sources);
105  ~SignalSources();
106 
107  void add(std::vector<int> const& sigs, std::function<void(int)> const& handler);
108 
109 private:
110  class SourceRegistration;
111  struct HandlerElement
112  {
113  operator bool() const { return !!handler; }
114  std::vector<int> sigs;
115  std::function<void(int)> handler;
116  };
117 
118  void dispatch_pending_signal();
119  void ensure_signal_is_handled(int sig);
120  int read_pending_signal();
121  void dispatch_signal(int sig);
122 
123  FdSources& fd_sources;
124  mir::Fd signal_read_fd;
125  mir::Fd signal_write_fd;
127  std::mutex handled_signals_mutex;
128  std::unordered_map<int, struct sigaction> handled_signals;
129  std::unique_ptr<SourceRegistration> source_registration;
130 };
131 
132 }
133 }
134 
135 #endif
All things Mir.
Definition: atomic_callback.h:25
SignalSources(FdSources &fd_sources)
GSourceHandle()
Definition: glib_main_loop_sources.cpp:76
~GSourceHandle()
Definition: glib_main_loop_sources.cpp:104
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)
void ensure_no_further_dispatch()
Definition: glib_main_loop_sources.cpp:139
GSourceHandle & operator=(GSourceHandle other)
Definition: glib_main_loop_sources.cpp:97
Definition: glib_main_loop_sources.h:49
Definition: glib_main_loop_sources.h:83
Definition: glib_main_loop_sources.h:101
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)
GSourceHandle add_timer_gsource(GMainContext *main_context, std::shared_ptr< time::Clock > const &clock, std::shared_ptr< LockableCallback > const &handler, std::function< void()> const &exception_handler, time::Timestamp target_time)
FdSources(GMainContext *main_context)

Copyright © 2012-2015 Canonical Ltd.
Generated on Thu Oct 8 16:20:16 UTC 2015