sync with tizen_2.0
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_EventDispatcher.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        FBaseRt_EventDispatcher.cpp
20  * @brief       This is the implementation file for the _EventDispatcher class.
21  *
22  */
23
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <memory.h>
28 #include <sys/eventfd.h>
29 #include <new>
30 #include <FBaseObject.h>
31 #include <FBaseRtIRunnable.h>
32 #include <FBaseSysLog.h>
33 #include "FBaseRt_ITask.h"
34 #include "FBaseRt_EventDispatcher.h"
35 #include "FBaseRt_ThreadManager.h"
36
37 using namespace Tizen::Base;
38 using namespace Tizen::Base::Runtime;
39
40 namespace Tizen { namespace Base { namespace Runtime
41 {
42 __thread _EventDispatcher* pCurrentEventDispatcher = null;
43
44 _EventDispatcher::_EventDispatcher(void)
45         : __pMutex(null)
46         , __pGMainContext(null)
47         , __pEventFdIo(null)
48         , __pEventFdSource(null)
49 {
50
51 }
52
53 _EventDispatcher::~_EventDispatcher(void)
54 {
55         if (__pEventFdSource)
56         {
57                 g_source_destroy(__pEventFdSource);
58                 g_source_unref(__pEventFdSource);
59                 __pEventFdSource = null;
60         }
61
62         if (__pEventFdIo)
63         {
64                 g_io_channel_unref(__pEventFdIo);
65                 __pEventFdIo = null;
66         }
67
68         if (__pMutex)
69         {
70                 delete __pMutex;
71                 __pMutex = null;
72         }
73 }
74
75 result
76 _EventDispatcher::Construct(GMainContext* pGMainContext)
77 {
78         result r = E_SUCCESS;
79         GError* pGError = null;
80         int eventFd = -1;
81
82         __pGMainContext = pGMainContext;
83         g_main_context_ref(__pGMainContext);
84
85         eventFd = eventfd(0, 0);
86         SysTryCatch(NID_BASE_RT, -1 != eventFd, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to open eventfd.");
87
88         __pEventFdIo = g_io_channel_unix_new(eventFd);
89         SysTryCatch(NID_BASE_RT, __pEventFdIo != null, r = E_SYSTEM, E_SYSTEM, "[E_SYSTEM] Failed to create a channel for eventfd.");
90         g_io_channel_set_encoding(__pEventFdIo, null, &pGError);
91         g_io_channel_set_flags(__pEventFdIo, G_IO_FLAG_NONBLOCK, &pGError);
92
93         __pEventFdSource = g_io_create_watch(__pEventFdIo, G_IO_IN);
94         SysTryCatch(NID_BASE_RT, __pEventFdSource != null, r = E_SYSTEM, E_SYSTEM,
95                            "[E_SYSTEM] Failed to create a gsource for a channel.");
96
97         g_source_set_callback(__pEventFdSource, (GSourceFunc) _EventDispatcher::OnEventReceived, this, null);
98         g_source_attach(__pEventFdSource, __pGMainContext);
99
100         __pMutex = new (std::nothrow) Mutex();
101         SysTryCatch(NID_BASE_RT, __pMutex != null, r = E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
102
103         r = __pMutex->Create();
104         SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to create mutex.", GetErrorMessage(r));
105
106         pCurrentEventDispatcher = this;
107
108         return E_SUCCESS;
109
110 CATCH:
111         if (__pMutex)
112         {
113                 delete __pMutex;
114                 __pMutex = null;
115         }
116
117         if (__pEventFdSource)
118         {
119                 g_source_unref(__pEventFdSource);
120                 __pEventFdSource = null;
121         }
122
123         if (__pEventFdIo)
124         {
125                 g_io_channel_unref(__pEventFdIo);
126                 __pEventFdIo = null;
127         }
128
129         return r;
130 }
131
132 result
133 _EventDispatcher::PutTask(_ITask* pITask, void* pParam)
134 {
135         gsize writtenSize = 0;
136         uint64_t tmp = 1;
137         _EventMessage* pMessage = null;
138
139         SysTryReturn(NID_BASE_RT, __pMutex != null, E_INVALID_STATE, E_INVALID_STATE, "[E_INVALID_STATE] Not constructed.");
140
141         pMessage = new (std::nothrow) _EventMessage;
142         ;
143         SysTryReturn(NID_BASE_RT, pMessage != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
144
145         pMessage->pITask = pITask;
146         pMessage->pParam = pParam;
147
148         __pMutex->Acquire();
149
150         __events.Add(pMessage);
151
152         __pMutex->Release();
153
154         g_io_channel_write(__pEventFdIo, (const gchar*) &tmp, sizeof(tmp), &writtenSize);
155
156         return E_SUCCESS;
157 }
158
159 gboolean
160 _EventDispatcher::OnEventReceived(GIOChannel* pGIOChannel, GIOCondition condition, gpointer data)
161 {
162         _EventMessage* pMessage = null;
163         _EventDispatcher* pEventDispatcher = (_EventDispatcher*) data;
164         uint64_t tmp = 0;
165         gsize readSize = 0;
166
167         if (condition & G_IO_IN)
168         {
169                 g_io_channel_read(pGIOChannel, (gchar*) &tmp, sizeof(tmp), &readSize);
170
171                 if (readSize == 0)
172                 {
173                         return TRUE;
174                 }
175
176                 while (true)
177                 {
178                         pEventDispatcher->__pMutex->Acquire();
179
180                         pMessage = null;
181                         if (pEventDispatcher->__events.GetAt(0, pMessage) == E_SUCCESS)
182                         {
183                                 pEventDispatcher->__events.RemoveAt(0);
184                         }
185
186                         pEventDispatcher->__pMutex->Release();
187
188                         if (pMessage)
189                         {
190                                 pMessage->pITask->DoTask(pMessage->pParam);
191
192                                 delete pMessage;
193                         }
194                         else
195                         {
196                                 break;
197                         }
198                 }
199         }
200
201         return TRUE;
202 }
203
204 _EventDispatcher*
205 _EventDispatcher::GetCurrentEventDispatcher(void)
206 {
207         return pCurrentEventDispatcher;
208 }
209
210 GMainContext*
211 _EventDispatcher::GetGMainContext(void)
212 {
213         SysTryReturn(NID_BASE_RT, __pMutex != null, null, E_INVALID_STATE, "[E_INVALID_STATE] Not constructed.");
214
215         return __pGMainContext;
216 }
217
218 } } } // Tizen::Base::Runtime