2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBaseRt_EventDispatcher.cpp
19 * @brief This is the implementation file for the _EventDispatcher class.
27 #include <sys/eventfd.h>
29 #include <FBaseObject.h>
30 #include <FBaseRtIRunnable.h>
31 #include <FBaseSysLog.h>
32 #include "FBaseRt_ITask.h"
33 #include "FBaseRt_EventDispatcher.h"
34 #include "FBaseRt_ThreadManager.h"
36 using namespace Tizen::Base;
37 using namespace Tizen::Base::Runtime;
39 namespace Tizen { namespace Base { namespace Runtime
41 __thread _EventDispatcher* pCurrentEventDispatcher = null;
43 _EventDispatcher::_EventDispatcher(void)
45 , __pGMainContext(null)
47 , __pEventFdSource(null)
52 _EventDispatcher::~_EventDispatcher(void)
56 g_source_destroy(__pEventFdSource);
57 g_source_unref(__pEventFdSource);
58 __pEventFdSource = null;
63 g_io_channel_unref(__pEventFdIo);
72 g_main_context_unref(__pGMainContext);
76 _EventDispatcher::Construct(GMainContext* pGMainContext)
79 GError* pGError = null;
82 __pGMainContext = pGMainContext;
84 eventFd = eventfd(0, 0);
85 SysTryCatch(NID_BASE_RT, -1 != eventFd, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to open eventfd.");
87 __pEventFdIo = g_io_channel_unix_new(eventFd);
88 SysTryCatch(NID_BASE_RT, __pEventFdIo != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to create a channel for eventfd.");
89 g_io_channel_set_encoding(__pEventFdIo, null, &pGError);
90 g_io_channel_set_flags(__pEventFdIo, G_IO_FLAG_NONBLOCK, &pGError);
92 g_io_channel_set_close_on_unref(__pEventFdIo, TRUE);
95 __pEventFdSource = g_io_create_watch(__pEventFdIo, G_IO_IN);
96 SysTryCatch(NID_BASE_RT, __pEventFdSource != null, r = E_SYSTEM, E_SYSTEM,
97 "[E_SYSTEM] Failed to create a gsource for a channel.");
99 g_source_set_callback(__pEventFdSource, (GSourceFunc) _EventDispatcher::OnEventReceived, this, null);
100 g_source_attach(__pEventFdSource, __pGMainContext);
102 __pMutex = new (std::nothrow) Mutex();
103 SysTryCatch(NID_BASE_RT, __pMutex != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
105 r = __pMutex->Create();
106 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to create mutex.", GetErrorMessage(r));
108 pCurrentEventDispatcher = this;
119 if (__pEventFdSource)
121 g_source_destroy(__pEventFdSource);
122 g_source_unref(__pEventFdSource);
123 __pEventFdSource = null;
128 g_io_channel_unref(__pEventFdIo);
141 _EventDispatcher::PutTask(_ITask* pITask, void* pParam)
143 gsize writtenSize = 0;
145 _EventMessage* pMessage = null;
147 SysTryReturn(NID_BASE_RT, __pMutex != null, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Not constructed.");
149 pMessage = new (std::nothrow) _EventMessage;
151 SysTryReturn(NID_BASE_RT, pMessage != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
153 pMessage->pITask = pITask;
154 pMessage->pParam = pParam;
158 __events.Add(pMessage);
162 g_io_channel_write(__pEventFdIo, (const gchar*) &tmp, sizeof(tmp), &writtenSize);
168 _EventDispatcher::OnEventReceived(GIOChannel* pGIOChannel, GIOCondition condition, gpointer data)
170 _EventMessage* pMessage = null;
171 _EventDispatcher* pEventDispatcher = (_EventDispatcher*) data;
175 if (condition & G_IO_IN)
177 g_io_channel_read(pGIOChannel, (gchar*) &tmp, sizeof(tmp), &readSize);
186 pEventDispatcher->__pMutex->Acquire();
189 if (pEventDispatcher->__events.GetAt(0, pMessage) == E_SUCCESS)
191 pEventDispatcher->__events.RemoveAt(0);
194 pEventDispatcher->__pMutex->Release();
198 pMessage->pITask->DoTask(pMessage->pParam);
213 _EventDispatcher::GetCurrentEventDispatcher(void)
215 return pCurrentEventDispatcher;
219 _EventDispatcher::GetGMainContext(void)
221 SysTryReturn(NID_BASE_RT, __pMutex != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Not constructed.");
223 return __pGMainContext;
226 } } } // Tizen::Base::Runtime