2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @file FBaseRt_EventDrivenThreadImpl.cpp
20 * @brief This is the implementation file for the _EventDrivenThreadImpl class.
25 #include <FBaseSysLog.h>
26 #include "FBaseRt_EventDrivenThreadImpl.h"
28 using namespace Tizen::Base::Collection;
30 namespace Tizen { namespace Base { namespace Runtime
33 _EventDrivenThreadImpl::_EventDrivenThreadImpl(Thread& thread, const String& name, long stackSize, ThreadPriority priority)
34 : _ThreadImpl(thread, name, stackSize, priority, THREAD_TYPE_EVENT_DRIVEN)
36 , __pEventDispatcher(null)
37 , __pEventManager(null)
39 , __pGmainContext(null)
44 _EventDrivenThreadImpl::~_EventDrivenThreadImpl(void)
46 delete __pEventDispatcher;
47 delete __pEventManager;
49 g_main_context_unref(__pGmainContext);
53 _EventDrivenThreadImpl::Stop(void)
57 _EventDrivenThreadEventArg* pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_STOP
61 r = __pEvent->Fire(*pEventArg);
62 SysTryReturn(NID_BASE_RT, !IsFailed(r), r, r, "[%s] Failed to send a stop event", GetErrorMessage(r));
66 __pendingEvents.Add(pEventArg);
74 _EventDrivenThreadImpl::Run(void)
77 _EventDrivenThreadEventArg* pArg = null;
79 for (int i = 0; i < __pendingEvents.GetCount(); i++)
81 __pendingEvents.GetAt(i, pArg);
83 r = __pEvent->FireAsync(*pArg);
90 g_main_loop_run(__pGMainLoop);
96 _EventDrivenThreadImpl::Initialize(void)
99 GMainContext* pGMainContext = null;
100 _EventManager* pEventManager = null;
101 _EventDrivenThreadEvent* pEvent = null;
103 _ThreadImpl::Initialize();
105 pGMainContext = g_main_context_new();
106 g_main_context_push_thread_default(pGMainContext);
108 pEventManager = new (std::nothrow) _EventManager;
109 SysTryReturn(NID_BASE_RT, pEventManager != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
111 r = pEventManager->Construct(pGMainContext);
112 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event manager.", GetErrorMessage(r));
114 _ThreadImpl::SetEventManager(pEventManager);
116 // Initialize event driven thread
117 // This should be done after initialzing event manager has finished.
118 pEvent = new (std::nothrow) _EventDrivenThreadEvent;
119 SysTryReturn(NID_BASE_RT, pEvent != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
121 r = pEvent->Construct(*this);
122 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event driven thread event.", GetErrorMessage(r));
124 __pEventManager = pEventManager;
127 __pGMainLoop = g_main_loop_new(pGMainContext, FALSE);
129 __pEventDispatcher = new (std::nothrow) _EventDispatcher();
130 __pEventDispatcher->Construct(pGMainContext);
132 __pGmainContext = pGMainContext;
136 g_main_context_unref(pGMainContext);
140 delete pEventManager;
142 _ThreadImpl::Finalize();
148 _EventDrivenThreadImpl::SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs)
150 result r = E_SUCCESS;
151 _EventDrivenThreadEventArg* pEventArg = null;
153 pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_USER_EVENT, requestId, pArgs);
157 r = __pEvent->FireAsync(*pEventArg);
161 __pendingEvents.Add(pEventArg);
168 _EventDrivenThreadImpl::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
170 if (_pThread != null)
172 _pThread->OnUserEventReceivedN(reqId, pArgs);
177 _EventDrivenThreadImpl::OnStop(void)
179 if (__pGMainLoop != null)
181 g_main_loop_quit(__pGMainLoop);
185 } } } // Tizen::Base::Runtime