[2.2.1] Change FBase_String.h to FBaseUtil_AtomicOperations.h
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRtThread.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        FBaseRtThreadThread.cpp
19  * @brief       This is the implementation file for the Thread class.
20  *
21  */
22
23 #include <new>
24 #include <FBaseRtThread.h>
25 #include <FBaseSysLog.h>
26 #include "FBaseRt_ThreadImpl.h"
27 #include "FBaseRt_WorkerThreadImpl.h"
28 #include "FBaseRt_EventDrivenThreadImpl.h"
29 #include "FBaseRt_MainThreadImpl.h"
30
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
33
34 namespace Tizen { namespace Base { namespace Runtime
35 {
36
37 result
38 Thread::Sleep(long milliSeconds)
39 {
40         SysTryReturnResult(NID_BASE_RT, milliSeconds >= 0, E_INVALID_ARG, "milliSeconds is less than zero.");
41
42         result r = _ThreadImpl::Sleep(milliSeconds);
43         SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_INVALID_ARG, "milliSeconds is less than zero.");
44
45         return E_SUCCESS;
46 }
47
48 result
49 Thread::Yield(void)
50 {
51         result r = _ThreadImpl::Yield();
52         SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Yield failed due to a system error.");
53
54         return E_SUCCESS;
55 }
56
57 result
58 Thread::Exit(int exitCode)
59 {
60         _ThreadImpl::Exit(exitCode);
61
62         return E_SUCCESS;
63 }
64
65 Thread*
66 Thread::GetCurrentThread(void)
67 {
68         Thread* pThread = _ThreadImpl::GetCurrentThread();
69         SysTryReturn(NID_BASE_RT, pThread != null, null, E_SYSTEM, "Getting current thread failed due to a system error.");
70
71         return pThread;
72 }
73
74 Thread::Thread(void)
75         : __pThreadImpl(null)
76 {
77
78 }
79
80 Thread::~Thread(void)
81 {
82         delete __pThreadImpl;
83 }
84
85 result
86 Thread::Construct(ThreadType threadType, long stackSize, ThreadPriority priority)
87 {
88         return Construct(L"thread", threadType, stackSize, priority);
89 }
90
91 result
92 Thread::Construct(long stackSize, ThreadPriority priority)
93 {
94         return Construct(L"thread", THREAD_TYPE_WORKER, stackSize, priority);
95 }
96
97
98 result
99 Thread::Construct(const String& name, ThreadType threadType, long stackSize, ThreadPriority priority)
100 {
101         // Object is not allowed to construct twice
102         SysAssertf(__pThreadImpl == null,
103                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
104
105         SysTryReturnResult(NID_BASE_RT, !name.IsEmpty(), E_INVALID_ARG, "Thread name is empty string.");
106         SysTryReturnResult(NID_BASE_RT, stackSize > 0, E_INVALID_ARG, "Stack size must be greater than zero.");
107
108         if (threadType == THREAD_TYPE_EVENT_DRIVEN)
109         {
110                 __pThreadImpl = new (std::nothrow) _EventDrivenThreadImpl(*this, name, stackSize, priority);
111         }
112         else if (threadType == THREAD_TYPE_WORKER)
113         {
114                 __pThreadImpl = new (std::nothrow) _WorkerThreadImpl(*this, null, name, stackSize, priority);
115         }
116         else
117         {
118                 __pThreadImpl = new (std::nothrow) _MainThreadImpl(*this, name);
119         }
120
121         SysTryReturnResult(NID_BASE_RT, __pThreadImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
122
123         return E_SUCCESS;
124 }
125
126 result
127 Thread::Construct(const String& name, long stackSize, ThreadPriority priority)
128 {
129         return Construct(name, THREAD_TYPE_WORKER, stackSize, priority);
130 }
131
132 result
133 Thread::Construct(IRunnable& target, long stackSize, ThreadPriority priority)
134 {
135         return Construct(L"thread", target, stackSize, priority);
136 }
137
138 result
139 Thread::Construct(const String& name, IRunnable& target, long stackSize, ThreadPriority priority)
140 {
141         // Object is not allowed to construct twice
142         SysAssertf(__pThreadImpl == null,
143                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
144
145         SysTryReturnResult(NID_BASE_RT, !name.IsEmpty(), E_INVALID_ARG, "Thread name is empty string.");
146         SysTryReturnResult(NID_BASE_RT, stackSize > 0, E_INVALID_ARG, "Stack size must be greater than zero.");
147
148         __pThreadImpl = new (std::nothrow) _WorkerThreadImpl(*this, &target, name, stackSize, priority);
149         SysTryReturnResult(NID_BASE_RT, __pThreadImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
150
151         return E_SUCCESS;
152 }
153
154 result
155 Thread::Join(void)
156 {
157         SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
158
159         result r = __pThreadImpl->Join();
160         SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Join failed due to a system error.");
161
162         return E_SUCCESS;
163 }
164
165 result
166 Thread::Start(void)
167 {
168         SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
169
170         result r = __pThreadImpl->Start();
171         SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Start a thread has failed due to a system error.");
172
173         return E_SUCCESS;
174 }
175
176 result
177 Thread::Stop(void)
178 {
179         SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
180
181         result r = __pThreadImpl->Stop();
182         SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Stop a thread has failed due to a system error.");
183
184         return E_SUCCESS;
185 }
186
187 result
188 Thread::GetExitCode(int& exitCode) const
189 {
190         SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
191
192         result r = __pThreadImpl->GetExitCode(exitCode);
193         SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_INVALID_STATE, "Thread is not terminated.");
194
195         return E_SUCCESS;
196 }
197
198 const String&
199 Thread::GetName(void) const
200 {
201         SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
202         return __pThreadImpl->GetName();
203 }
204
205 result
206 Thread::SendUserEvent(RequestId requestId, const IList* pArgs)
207 {
208         SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
209         return __pThreadImpl->SendUserEvent(requestId, pArgs);
210 }
211
212 Object*
213 Thread::Run(void)
214 {
215         return null;
216 }
217
218 bool
219 Thread::OnStart(void)
220 {
221
222         return true;
223 }
224
225 void
226 Thread::OnStop(void)
227 {
228
229 }
230
231 void
232 Thread::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
233 {
234
235 }
236
237 } } } // Tizen::Base::Runtime