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 FBaseRtThreadThread.cpp
19 * @brief This is the implementation file for the Thread class.
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"
31 using namespace Tizen::Base;
32 using namespace Tizen::Base::Collection;
34 namespace Tizen { namespace Base { namespace Runtime
38 Thread::Sleep(long milliSeconds)
40 SysTryReturnResult(NID_BASE_RT, milliSeconds >= 0, E_INVALID_ARG, "milliSeconds is less than zero.");
42 result r = _ThreadImpl::Sleep(milliSeconds);
43 SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_INVALID_ARG, "milliSeconds is less than zero.");
51 result r = _ThreadImpl::Yield();
52 SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Yield failed due to a system error.");
58 Thread::Exit(int exitCode)
60 _ThreadImpl::Exit(exitCode);
66 Thread::GetCurrentThread(void)
68 Thread* pThread = _ThreadImpl::GetCurrentThread();
69 SysTryReturn(NID_BASE_RT, pThread != null, null, E_SYSTEM, "Getting current thread failed due to a system error.");
86 Thread::Construct(ThreadType threadType, long stackSize, ThreadPriority priority)
88 return Construct(L"thread", threadType, stackSize, priority);
92 Thread::Construct(long stackSize, ThreadPriority priority)
94 return Construct(L"thread", THREAD_TYPE_WORKER, stackSize, priority);
99 Thread::Construct(const String& name, ThreadType threadType, long stackSize, ThreadPriority priority)
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");
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.");
108 if (threadType == THREAD_TYPE_EVENT_DRIVEN)
110 __pThreadImpl = new (std::nothrow) _EventDrivenThreadImpl(*this, name, stackSize, priority);
112 else if (threadType == THREAD_TYPE_WORKER)
114 __pThreadImpl = new (std::nothrow) _WorkerThreadImpl(*this, null, name, stackSize, priority);
118 __pThreadImpl = new (std::nothrow) _MainThreadImpl(*this, name);
121 SysTryReturnResult(NID_BASE_RT, __pThreadImpl != null, E_OUT_OF_MEMORY, "Not enough memory.");
127 Thread::Construct(const String& name, long stackSize, ThreadPriority priority)
129 return Construct(name, THREAD_TYPE_WORKER, stackSize, priority);
133 Thread::Construct(IRunnable& target, long stackSize, ThreadPriority priority)
135 return Construct(L"thread", target, stackSize, priority);
139 Thread::Construct(const String& name, IRunnable& target, long stackSize, ThreadPriority priority)
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");
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.");
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.");
157 SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
159 result r = __pThreadImpl->Join();
160 SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Join failed due to a system error.");
168 SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
170 result r = __pThreadImpl->Start();
171 SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Start a thread has failed due to a system error.");
179 SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
181 result r = __pThreadImpl->Stop();
182 SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_SYSTEM, "Stop a thread has failed due to a system error.");
188 Thread::GetExitCode(int& exitCode) const
190 SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
192 result r = __pThreadImpl->GetExitCode(exitCode);
193 SysTryReturnResult(NID_BASE_RT, !IsFailed(r), E_INVALID_STATE, "Thread is not terminated.");
199 Thread::GetName(void) const
201 SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
202 return __pThreadImpl->GetName();
206 Thread::SendUserEvent(RequestId requestId, const IList* pArgs)
208 SysAssertf(__pThreadImpl != null, "Not yet constructed! Construct() should be called before use.");
209 return __pThreadImpl->SendUserEvent(requestId, pArgs);
219 Thread::OnStart(void)
232 Thread::OnUserEventReceivedN(RequestId requestId, IList* pArgs)
237 } } } // Tizen::Base::Runtime