Added support for Event creation in worker thread
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_MainThreadImpl.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_MainThreadImpl.cpp
19  * @brief       This is the implementation file for the _MainThreadImpl class.
20  *
21  */
22
23 #include <sched.h>
24 #include <errno.h>
25 #include <new>
26
27 #include <FBaseSysLog.h>
28 #include "FBaseRt_EventDispatcher.h"
29 #include "FBase_NativeError.h"
30 #include "FBaseRt_MainThreadImpl.h"
31 #include "FBaseRt_EventManager.h"
32
33
34 namespace Tizen { namespace Base { namespace Runtime
35 {
36
37 _MainThreadImpl::_MainThreadImpl(Thread& thread, const String& name)
38         : _ThreadImpl(thread, name, Thread::DEFAULT_STACK_SIZE, THREAD_PRIORITY_HIGH, THREAD_TYPE_MAIN)
39         , __pEventDispatcher(null)
40         , __pEventManager(null)
41 {
42
43 }
44
45 _MainThreadImpl::~_MainThreadImpl(void)
46 {
47         delete __pEventDispatcher;
48         delete __pEventManager;
49 }
50
51 result
52 _MainThreadImpl::Start(void)
53 {
54         Run();
55
56         return E_SUCCESS;
57 }
58
59 Object*
60 _MainThreadImpl::Run(void)
61 {
62         SetNativeThread(pthread_self());
63
64         Initialize();
65
66         return null;
67 }
68
69 result
70 _MainThreadImpl::Initialize(void)
71 {
72         result r = E_SUCCESS;
73         _EventManager* pEventManager = null;
74         _EventDispatcher* pEventDispatcher = null;
75
76         GMainContext* pGMainContext = g_main_context_default();
77         SysTryReturnResult(NID_BASE_RT, pGMainContext != null, E_INVALID_STATE, "Failed to get GMainContext.");
78
79         pEventManager = new (std::nothrow) _EventManager;
80         SysTryReturnResult(NID_BASE_RT, pEventManager != null, E_OUT_OF_MEMORY, "Not enough memory.");
81
82         r = pEventManager->Construct(pGMainContext);
83         SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Failed to initialize event manager.", GetErrorMessage(r));
84
85         _ThreadImpl::SetEventManager(pEventManager);
86         __pEventManager = pEventManager;
87
88         pEventDispatcher = new (std::nothrow) _EventDispatcher;
89         SysTryCatch(NID_BASE_RT, pEventDispatcher!= null, , E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Not enough memory.");
90
91         r = pEventDispatcher->Construct(pGMainContext);
92         SysTryCatch(NID_BASE_RT, !IsFailed(r), , r, "[%s] Propagating.", GetErrorMessage(r));
93
94         _ThreadImpl::Initialize();
95
96         SetMainThread();
97         __pEventDispatcher = pEventDispatcher;
98
99         return E_SUCCESS;
100
101 CATCH:
102         delete pEventDispatcher;
103
104         delete pEventManager;
105
106         return r;
107 }
108
109 } } } // Tizen::Base::Runtime