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)
43 _EventDrivenThreadImpl::~_EventDrivenThreadImpl(void)
45 delete __pEventDispatcher;
46 delete __pEventManager;
51 _EventDrivenThreadImpl::Stop(void)
55 _EventDrivenThreadEventArg* pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_STOP
59 r = __pEvent->Fire(*pEventArg);
60 SysTryReturn(NID_BASE_RT, !IsFailed(r), r, r, "[%s] Failed to send a stop event", GetErrorMessage(r));
64 __pendingEvents.Add(pEventArg);
72 _EventDrivenThreadImpl::Run(void)
75 _EventDrivenThreadEventArg* pArg = null;
77 for (int i = 0; i < __pendingEvents.GetCount(); i++)
79 __pendingEvents.GetAt(i, pArg);
81 r = __pEvent->FireAsync(*pArg);
88 g_main_loop_run(__pGMainLoop);
94 _EventDrivenThreadImpl::Initialize(void)
97 GMainContext* pGMainContext = null;
98 _EventManager* pEventManager = null;
99 _EventDrivenThreadEvent* pEvent = null;
101 _ThreadImpl::Initialize();
103 pGMainContext = g_main_context_new();
104 g_main_context_push_thread_default(pGMainContext);
106 pEventManager = new (std::nothrow) _EventManager;
107 SysTryReturn(NID_BASE_RT, pEventManager != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
109 r = pEventManager->Construct(pGMainContext);
110 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event manager.", GetErrorMessage(r));
112 _ThreadImpl::SetEventManager(pEventManager);
114 // Initialize event driven thread
115 // This should be done after initialzing event manager has finished.
116 pEvent = new (std::nothrow) _EventDrivenThreadEvent;
117 SysTryReturn(NID_BASE_RT, pEvent != null, E_OUT_OF_MEMORY, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
119 r = pEvent->Construct(*this);
120 SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event driven thread event.", GetErrorMessage(r));
122 __pEventManager = pEventManager;
125 __pGMainLoop = g_main_loop_new(pGMainContext, FALSE);
127 __pEventDispatcher = new (std::nothrow) _EventDispatcher();
128 __pEventDispatcher->Construct(pGMainContext);
133 g_main_context_unref(pGMainContext);
137 delete pEventManager;
139 _ThreadImpl::Finalize();
145 _EventDrivenThreadImpl::SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs)
147 result r = E_SUCCESS;
148 _EventDrivenThreadEventArg* pEventArg = null;
150 pEventArg = new (std::nothrow) _EventDrivenThreadEventArg(EVENT_DRIVEN_THREAD_EVENT_TYPE_USER_EVENT, requestId, pArgs);
154 r = __pEvent->FireAsync(*pEventArg);
158 __pendingEvents.Add(pEventArg);
165 _EventDrivenThreadImpl::OnUserEventReceivedN(RequestId reqId, IList* pArgs)
167 if (_pThread != null)
169 _pThread->OnUserEventReceivedN(reqId, pArgs);
174 _EventDrivenThreadImpl::OnStop(void)
176 if (__pGMainLoop != null)
178 g_main_loop_quit(__pGMainLoop);
182 } } } // Tizen::Base::Runtime