Fix the boiler plate codes
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_EventDrivenThreadImpl.cpp
1 //
2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
3 //
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
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
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.
15 //
16
17 /**
18  * @file        FBaseRt_EventDrivenThreadImpl.cpp
19  * @brief       This is the implementation file for the _EventDrivenThreadImpl class.
20  *
21  */
22
23 #include <new>
24 #include <FBaseSysLog.h>
25 #include "FBaseRt_EventDrivenThreadImpl.h"
26
27 using namespace Tizen::Base::Collection;
28
29 namespace Tizen { namespace Base { namespace Runtime
30 {
31
32 _EventDrivenThreadImpl::_EventDrivenThreadImpl(Thread& thread, const String& name, long stackSize, ThreadPriority priority)
33         : _ThreadImpl(thread, name, stackSize, priority, THREAD_TYPE_EVENT_DRIVEN)
34         , __pGMainLoop(null)
35         , __pEventDispatcher(null)
36         , __pEventManager(null)
37         , __pEvent(null)
38         , __pGmainContext(null)
39 {
40
41 }
42
43 _EventDrivenThreadImpl::~_EventDrivenThreadImpl(void)
44 {
45         delete __pEventDispatcher;
46         delete __pEventManager;
47         delete __pEvent;
48         g_main_context_unref(__pGmainContext);
49 }
50
51 result
52 _EventDrivenThreadImpl::Stop(void)
53 {
54         result r = E_SUCCESS;
55
56         _EventDrivenThreadEventArg* pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_STOP
57                                                                                                                                                                                   , 0, null);
58         if (__pEvent)
59         {
60                 r = __pEvent->Fire(*pEventArg);
61                 SysTryReturn(NID_BASE_RT, !IsFailed(r), r, r, "[%s] Failed to send a stop event", GetErrorMessage(r));
62         }
63         else
64         {
65                 __pendingEvents.Add(pEventArg);
66         }
67
68
69         return E_SUCCESS;
70 }
71
72 Tizen::Base::Object*
73 _EventDrivenThreadImpl::Run(void)
74 {
75         result r = E_SUCCESS;
76         _EventDrivenThreadEventArg* pArg = null;
77
78         for (int i = 0; i < __pendingEvents.GetCount(); i++)
79         {
80                 __pendingEvents.GetAt(i, pArg);
81
82                 r = __pEvent->FireAsync(*pArg);
83                 if (IsFailed(r))
84                 {
85                         continue;
86                 }
87         }
88
89         g_main_loop_run(__pGMainLoop);
90
91         return null;
92 }
93
94 result
95 _EventDrivenThreadImpl::Initialize(void)
96 {
97         result r = E_SUCCESS;
98         GMainContext* pGMainContext = null;
99         _EventManager* pEventManager = null;
100         _EventDrivenThreadEvent* pEvent = null;
101
102         _ThreadImpl::Initialize();
103
104         pGMainContext = g_main_context_new();
105         g_main_context_push_thread_default(pGMainContext);
106
107         pEventManager = new (std::nothrow) _EventManager;
108         SysTryReturn(NID_BASE_RT, pEventManager != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
109
110         r = pEventManager->Construct(pGMainContext);
111         SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event manager.", GetErrorMessage(r));
112
113         _ThreadImpl::SetEventManager(pEventManager);
114
115         // Initialize event driven thread
116         // This should be done after initialzing event manager has finished.
117         pEvent = new (std::nothrow) _EventDrivenThreadEvent;
118         SysTryReturn(NID_BASE_RT, pEvent != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
119
120         r = pEvent->Construct(*this);
121         SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event driven thread event.", GetErrorMessage(r));
122
123         __pEventManager = pEventManager;
124         __pEvent = pEvent;
125
126         __pGMainLoop = g_main_loop_new(pGMainContext, FALSE);
127
128         __pEventDispatcher = new (std::nothrow) _EventDispatcher();
129         __pEventDispatcher->Construct(pGMainContext);
130
131         __pGmainContext = pGMainContext;
132         return E_SUCCESS;
133
134 CATCH:
135         g_main_context_unref(pGMainContext);
136
137         delete pEvent;
138
139         delete pEventManager;
140
141         _ThreadImpl::Finalize();
142
143         return r;
144 }
145
146 result
147 _EventDrivenThreadImpl::SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs)
148 {
149         result r = E_SUCCESS;
150         _EventDrivenThreadEventArg* pEventArg = null;
151
152         pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_USER_EVENT, requestId, pArgs);
153
154         if (__pEvent)
155         {
156                 r = __pEvent->FireAsync(*pEventArg);
157         }
158         else
159         {
160                 __pendingEvents.Add(pEventArg);
161         }
162
163         return r;
164 }
165
166 void
167 _EventDrivenThreadImpl::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
168 {
169         if (_pThread != null)
170         {
171                 _pThread->OnUserEventReceivedN(reqId, pArgs);
172         }
173 }
174
175 void
176 _EventDrivenThreadImpl::OnStop(void)
177 {
178         if (__pGMainLoop != null)
179         {
180                 g_main_loop_quit(__pGMainLoop);
181         }
182 }
183
184 } } } // Tizen::Base::Runtime