2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Jan Olszak <j.olszak@samsung.com>
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
21 * @author Jan Olszak (j.olszak@samsung.com)
22 * @brief Class for creating a dedicated GSource
25 #ifndef COMMON_IPC_IPC_GSOURCE_HPP
26 #define COMMON_IPC_IPC_GSOURCE_HPP
29 #include "ipc/types.hpp"
30 #include "utils/callback-guard.hpp"
41 * Class for connecting to the glib's loop.
42 * Creates a dedicated GSource.
44 * It's supposed to be constructed ONLY with the static create method
45 * and destructed in a glib callback.
48 * - waiting till the managed object (Client or Service) is destroyed
49 * before IPCGSource stops operating. For now programmer has to ensure this.
53 typedef std::function<void(FileDescriptor fd, short pollEvent)> HandlerCallback;
54 typedef std::shared_ptr<IPCGSource> Pointer;
58 IPCGSource() = delete;
59 IPCGSource(const IPCGSource&) = delete;
60 IPCGSource& operator=(const IPCGSource&) = delete;
63 * New file descriptor to listen on.
65 * @param peerFD file descriptor
67 void addFD(const FileDescriptor peerFD);
70 * Removes the file descriptor from the GSource
72 * @param peerFD file descriptor
74 void removeFD(const FileDescriptor peerFD);
77 * Attach to the glib's GMainContext
79 * @param context where to connect
80 * @return result of the g_source_attach call
82 guint attach(GMainContext* context = nullptr);
85 * After this method quits handlerCallback will not be called
90 * Creates the IPCGSource class in the memory allocated by glib.
91 * Calls IPCGSource's constructor
93 * @param handlerCallback event handling callback
95 * @return pointer to the IPCGSource
97 static Pointer create(const HandlerCallback& handlerCallback);
100 * Callback for the dispatch function
102 static gboolean onHandlerCall(gpointer userData);
105 * Locks the internal state mutex and calls the handler callback for each fd
110 typedef std::unique_lock<std::recursive_mutex> Lock;
113 * GSourceFuncs' callback
115 static gboolean prepare(GSource* source, gint* timeout);
118 * GSourceFuncs' callback
120 static gboolean check(GSource* source);
123 * GSourceFuncs' callback
125 static gboolean dispatch(GSource* source,
126 GSourceFunc callbacks,
130 * GSourceFuncs' callback
132 static void finalize(GSource* source);
134 // Called only from IPCGSource::create
135 IPCGSource(const HandlerCallback& handlerCallback);
138 HandlerCallback mHandlerCallback;
139 std::list<GPollFD> mGPollFDs;
140 utils::CallbackGuard mGuard;
141 std::recursive_mutex mStateMutex;
148 #endif // COMMON_IPC_IPC_GSOURCE_HPP