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_ThreadImpl.cpp
20 * @brief This is the implementation file for the _ThreadImpl class.
27 #include <FBaseRtThread.h>
28 #include <FBaseRtSemaphore.h>
29 #include <FBaseInteger.h>
31 #include "FBase_NativeError.h"
32 #include <FBaseSysLog.h>
33 #include "FBaseRt_ThreadImpl.h"
34 #include "FBaseRt_EventManager.h"
35 #include "FBaseRt_ThreadManager.h"
37 namespace Tizen { namespace Base { namespace Runtime
40 __thread Thread* pCurrentThread = null;
43 _ThreadImpl::GetCurrentThreadImpl(void)
45 if (pCurrentThread != null)
47 return pCurrentThread->__pThreadImpl;
54 _ThreadImpl::Exit(int exitCode)
56 _ThreadImpl* pThreadImpl = GetCurrentThreadImpl();
57 if (pThreadImpl != null)
59 pThreadImpl->__exitCode = exitCode;
66 _ThreadImpl::Sleep(long milliSeconds)
70 struct timespec tspec = { milliSeconds / 1000, (milliSeconds % 1000) * (1000 * 1000) };
72 while (nanosleep(&tspec, &tspec) == -1)
78 else if (errno == EINVAL)
80 return E_INVALID_ARG;}
89 else if (milliSeconds == 0)
100 _ThreadImpl::Yield(void)
102 int ret = pthread_yield();
103 SysTryReturnResult(NID_BASE_RT, ret == 0, E_SYSTEM, "Yield has failed due to a system error.");
109 _ThreadImpl::GetCurrentThread(void)
113 if (pCurrentThread == null)
118 return pCurrentThread;
121 _ThreadImpl::_ThreadImpl(Thread& thread, const String& name, long stackSize, ThreadPriority priority, ThreadType threadType)
124 , __stackSize(stackSize)
125 , __priority(priority)
126 , __threadType(threadType)
129 , __pEventManager(null)
134 _ThreadImpl::~_ThreadImpl(void)
140 _ThreadImpl::Join(void)
142 int ret = pthread_join(__nativeThread, null);
143 SysTryReturnResult(NID_BASE_RT, ret == 0, E_SYSTEM, "Thread join operation failed");
149 _ThreadImpl::GetExitCode(int& exitCode) const
151 exitCode = __exitCode;
156 const Tizen::Base::String&
157 _ThreadImpl::GetName(void) const
163 _ThreadImpl::Start(void)
166 struct sched_param schedParam;
168 int ret = pthread_attr_init(&attr);
169 SysTryReturnResult(NID_BASE_RT, ret == 0, E_OUT_OF_MEMORY, "Not enough memory.");
171 int maxPriority = sched_get_priority_max(SCHED_FIFO);
172 int minPriority = sched_get_priority_min(SCHED_FIFO);
174 schedParam.sched_priority = (3 - (int) __priority) * ((maxPriority - minPriority + 1) / 3);
176 ret = pthread_create(&__nativeThread, &attr, _ThreadImpl::ThreadProc, this);
177 SysTryReturnResult(NID_BASE_RT, ret == 0, E_SYSTEM, "Create a thread has failed due to system error.");
179 pthread_attr_destroy(&attr);
185 _ThreadImpl::Stop(void)
191 _ThreadImpl::Run(void)
197 _ThreadImpl::Initialize(void)
199 pCurrentThread = _pThread;
205 _ThreadImpl::Finalize(void)
207 pCurrentThread = null;
213 _ThreadImpl::SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs)
215 return E_INVALID_OPERATION;
219 _ThreadImpl::GetThread(void) const
225 _ThreadImpl::SetThread(Thread* pThread)
231 _ThreadImpl::SetNativeThread(pthread_t nativeThread)
233 __nativeThread = nativeThread;
237 _ThreadImpl::GetPriority(void) const
243 _ThreadImpl::GetThreadType(void) const
249 _ThreadImpl::ThreadProc(void* params)
251 _ThreadImpl* pSelf = (_ThreadImpl*) params;
257 Thread* pThread = pSelf->_pThread;
263 result r = pSelf->Initialize();
269 if (pThread->OnStart())
282 _ThreadImpl::SetEventManager(_EventManager* pEventManager)
284 __pEventManager = pEventManager;
288 _ThreadImpl::GetEventManager(void)
290 return __pEventManager;
294 } } } // Tizen::Base::Runtime