Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ipc-gsource.hpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Contact: Jan Olszak <j.olszak@samsung.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
17 */
18 
25 #ifndef CARGO_IPC_IPC_GSOURCE_HPP
26 #define CARGO_IPC_IPC_GSOURCE_HPP
27 
28 #include <glib.h>
29 #include "cargo-ipc/types.hpp"
30 #include "utils/callback-guard.hpp"
31 
32 #include <memory>
33 #include <mutex>
34 #include <list>
35 
36 
37 namespace cargo {
38 namespace ipc {
39 
51 struct IPCGSource {
52 public:
53  typedef std::function<void(FileDescriptor fd, short pollEvent)> HandlerCallback;
54  typedef std::shared_ptr<IPCGSource> Pointer;
55 
56  ~IPCGSource();
57 
58  IPCGSource() = delete;
59  IPCGSource(const IPCGSource&) = delete;
60  IPCGSource& operator=(const IPCGSource&) = delete;
61 
67  void addFD(const FileDescriptor peerFD);
68 
74  void removeFD(const FileDescriptor peerFD);
75 
82  guint attach(GMainContext* context = nullptr);
83 
87  void detach();
88 
97  static Pointer create(const HandlerCallback& handlerCallback);
98 
102  static gboolean onHandlerCall(gpointer userData);
103 
107  void callHandler();
108 
109 private:
110  typedef std::unique_lock<std::recursive_mutex> Lock;
111 
115  static gboolean prepare(GSource* source, gint* timeout);
116 
120  static gboolean check(GSource* source);
121 
125  static gboolean dispatch(GSource* source,
126  GSourceFunc callbacks,
127  gpointer userData);
128 
132  static void finalize(GSource* source);
133 
134  // Called only from IPCGSource::create
135  IPCGSource(const HandlerCallback& handlerCallback);
136 
137  GSource mGSource;
139  std::list<GPollFD> mGPollFDs;
141  std::recursive_mutex mStateMutex;
142 
143 };
144 
145 } // namespace ipc
146 } // namespace cargo
147 
148 #endif // CARGO_IPC_IPC_GSOURCE_HPP
std::list< GPollFD > mGPollFDs
Definition: ipc-gsource.hpp:139
static gboolean prepare(GSource *source, gint *timeout)
GSourceFuncs' callback.
Definition: ipc-gsource.cpp:161
GSource mGSource
Definition: ipc-gsource.hpp:137
static void finalize(GSource *source)
GSourceFuncs' callback.
Definition: ipc-gsource.cpp:199
Callback guard.
Class for connecting to the glib's loop.
Definition: ipc-gsource.hpp:51
Types definitions.
~IPCGSource()
Definition: ipc-gsource.cpp:51
Callback guard.
Definition: callback-guard.hpp:38
void callHandler()
Locks the internal state mutex and calls the handler callback for each fd.
Definition: ipc-gsource.cpp:141
static gboolean check(GSource *source)
GSourceFuncs' callback.
Definition: ipc-gsource.cpp:174
static gboolean onHandlerCall(gpointer userData)
Callback for the dispatch function.
Definition: ipc-gsource.cpp:152
void addFD(const FileDescriptor peerFD)
New file descriptor to listen on.
Definition: ipc-gsource.cpp:91
static Pointer create(const HandlerCallback &handlerCallback)
Creates the IPCGSource class in the memory allocated by glib.
Definition: ipc-gsource.cpp:56
utils::CallbackGuard mGuard
Definition: ipc-gsource.hpp:140
void detach()
After this method quits handlerCallback will not be called.
Definition: ipc-gsource.cpp:124
IPCGSource & operator=(const IPCGSource &)=delete
std::unique_lock< std::recursive_mutex > Lock
Definition: ipc-gsource.hpp:110
std::shared_ptr< IPCGSource > Pointer
Definition: ipc-gsource.hpp:54
std::function< void(FileDescriptor fd, short pollEvent)> HandlerCallback
Definition: ipc-gsource.hpp:53
void removeFD(const FileDescriptor peerFD)
Removes the file descriptor from the GSource.
Definition: ipc-gsource.cpp:100
guint attach(GMainContext *context=nullptr)
Attach to the glib's GMainContext.
Definition: ipc-gsource.cpp:117
int FileDescriptor
Definition: types.hpp:42
HandlerCallback mHandlerCallback
Definition: ipc-gsource.hpp:138
std::recursive_mutex mStateMutex
Definition: ipc-gsource.hpp:141
static gboolean dispatch(GSource *source, GSourceFunc callbacks, gpointer userData)
GSourceFuncs' callback.
Definition: ipc-gsource.cpp:183