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_EventDrivenThreadImpl.cpp
19 * @brief This is the implementation file for the _EventDrivenThreadImpl class.
24 #include <FBaseSysLog.h>
25 #include "FBaseRt_EventDrivenThreadImpl.h"
27 using namespace Tizen::Base::Collection;
29 namespace Tizen { namespace Base { namespace Runtime
32 _EventDrivenThreadImpl::_EventDrivenThreadImpl(Thread& thread, const String& name, long stackSize, ThreadPriority priority)
33 : _ThreadImpl(thread, name, stackSize, priority, THREAD_TYPE_EVENT_DRIVEN)
35 , __pEventDispatcher(null)
36 , __pEventManager(null)
38 , __pGmainContext(null)
41 __pLock = new (std::nothrow) Tizen::Base::Runtime::Mutex();
42 SysTryLog(NID_BASE, __pLock != null, "Failed to allocate the mutex instance");
44 result r = __pLock->Create();
45 SysTryLog(NID_BASE, !IsFailed(r), "Failed to create the mutex instance");
48 _EventDrivenThreadImpl::~_EventDrivenThreadImpl(void)
50 delete __pEventDispatcher;
51 delete __pEventManager;
54 g_main_loop_unref(__pGMainLoop);
58 _EventDrivenThreadImpl::Stop(void)
62 _EventDrivenThreadEventArg* pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_STOP, 0, null);
63 SysTryReturnResult(NID_BASE, pEventArg != null, E_OUT_OF_MEMORY, "Not enough memory.");
65 r = __pLock->Acquire();
66 SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
70 r = __pEvent->Fire(*pEventArg);
71 SysTryLog(NID_BASE_RT, !IsFailed(r), "[%s] Failed to send a stop event", GetErrorMessage(r));
75 __pendingEvents.Add(pEventArg);
78 r = __pLock->Release();
79 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
85 _EventDrivenThreadImpl::Run(void)
88 _EventDrivenThreadEventArg* pArg = null;
90 r = __pLock->Acquire();
91 SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
93 for (int i = 0; i < __pendingEvents.GetCount(); i++)
95 __pendingEvents.GetAt(i, pArg);
97 r = __pEvent->FireAsync(*pArg);
103 r = __pLock->Release();
104 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
106 g_main_loop_run(__pGMainLoop);
112 _EventDrivenThreadImpl::Initialize(void)
114 result r = E_SUCCESS;
115 GMainContext* pGMainContext = null;
116 _EventManager* pEventManager = null;
117 _EventDrivenThreadEvent* pEvent = null;
119 _ThreadImpl::Initialize();
121 pGMainContext = g_main_context_new();
122 g_main_context_push_thread_default(pGMainContext);
124 pEventManager = new (std::nothrow) _EventManager;
125 SysTryReturn(NID_BASE_RT, pEventManager != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
127 r = pEventManager->Construct(pGMainContext);
128 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event manager.", GetErrorMessage(r));
130 _ThreadImpl::SetEventManager(pEventManager);
132 // Initialize event driven thread
133 // This should be done after initialzing event manager has finished.
134 pEvent = new (std::nothrow) _EventDrivenThreadEvent;
135 SysTryReturn(NID_BASE_RT, pEvent != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
137 r = pEvent->Construct(*this);
138 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event driven thread event.", GetErrorMessage(r));
140 __pEventManager = pEventManager;
143 __pGMainLoop = g_main_loop_new(pGMainContext, FALSE);
145 __pEventDispatcher = new (std::nothrow) _EventDispatcher();
146 __pEventDispatcher->Construct(pGMainContext);
148 __pGmainContext = pGMainContext;
152 g_main_context_unref(pGMainContext);
156 delete pEventManager;
158 _ThreadImpl::Finalize();
164 _EventDrivenThreadImpl::SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs)
166 result r = __pLock->Acquire();
167 SysTryLog(NID_BASE, !IsFailed(r), "Failed to acquire mutex");
169 _EventDrivenThreadEventArg* pEventArg = null;
171 pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_USER_EVENT, requestId, pArgs);
175 r = __pEvent->FireAsync(*pEventArg);
179 __pendingEvents.Add(pEventArg);
181 r = __pLock->Release();
182 SysTryLog(NID_BASE, !IsFailed(r), "Failed to release mutex");
188 _EventDrivenThreadImpl::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
190 if (_pThread != null)
192 _pThread->OnUserEventReceivedN(reqId, pArgs);
197 _EventDrivenThreadImpl::OnStop(void)
199 if (__pGMainLoop != null)
201 g_main_loop_quit(__pGMainLoop);
205 } } } // Tizen::Base::Runtime