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)
43 _EventDrivenThreadImpl::~_EventDrivenThreadImpl(void)
45 delete __pEventDispatcher;
46 delete __pEventManager;
48 g_main_context_unref(__pGmainContext);
52 _EventDrivenThreadImpl::Stop(void)
56 _EventDrivenThreadEventArg* pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_STOP
60 r = __pEvent->Fire(*pEventArg);
61 SysTryReturn(NID_BASE_RT, !IsFailed(r), r, r, "[%s] Failed to send a stop event", GetErrorMessage(r));
65 __pendingEvents.Add(pEventArg);
73 _EventDrivenThreadImpl::Run(void)
76 _EventDrivenThreadEventArg* pArg = null;
78 for (int i = 0; i < __pendingEvents.GetCount(); i++)
80 __pendingEvents.GetAt(i, pArg);
82 r = __pEvent->FireAsync(*pArg);
89 g_main_loop_run(__pGMainLoop);
95 _EventDrivenThreadImpl::Initialize(void)
98 GMainContext* pGMainContext = null;
99 _EventManager* pEventManager = null;
100 _EventDrivenThreadEvent* pEvent = null;
102 _ThreadImpl::Initialize();
104 pGMainContext = g_main_context_new();
105 g_main_context_push_thread_default(pGMainContext);
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.");
110 r = pEventManager->Construct(pGMainContext);
111 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event manager.", GetErrorMessage(r));
113 _ThreadImpl::SetEventManager(pEventManager);
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.");
120 r = pEvent->Construct(*this);
121 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event driven thread event.", GetErrorMessage(r));
123 __pEventManager = pEventManager;
126 __pGMainLoop = g_main_loop_new(pGMainContext, FALSE);
128 __pEventDispatcher = new (std::nothrow) _EventDispatcher();
129 __pEventDispatcher->Construct(pGMainContext);
131 __pGmainContext = pGMainContext;
135 g_main_context_unref(pGMainContext);
139 delete pEventManager;
141 _ThreadImpl::Finalize();
147 _EventDrivenThreadImpl::SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs)
149 result r = E_SUCCESS;
150 _EventDrivenThreadEventArg* pEventArg = null;
152 pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_USER_EVENT, requestId, pArgs);
156 r = __pEvent->FireAsync(*pEventArg);
160 __pendingEvents.Add(pEventArg);
167 _EventDrivenThreadImpl::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
169 if (_pThread != null)
171 _pThread->OnUserEventReceivedN(reqId, pArgs);
176 _EventDrivenThreadImpl::OnStop(void)
178 if (__pGMainLoop != null)
180 g_main_loop_quit(__pGMainLoop);
184 } } } // Tizen::Base::Runtime