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 FBaseRtThread.h
20 * @brief This is the header file for the %Thread class.
22 * This header file contains the declarations of the %Thread class.
24 #ifndef _FBASE_RT_THREAD_H_
25 #define _FBASE_RT_THREAD_H_
28 #include <FBaseResult.h>
29 #include <FBaseObject.h>
30 #include <FBaseColIList.h>
31 #include <FBaseColMultiHashMap.h>
32 #include <FBaseColArrayList.h>
34 #include <FBaseRtMutex.h>
35 #include <FBaseRtSemaphore.h>
36 #include <FBaseRtMonitor.h>
37 #include <FBaseRtIRunnable.h>
40 #if defined(Yield) // For preventing compile errors
44 namespace Tizen { namespace Base { class String; }}
46 namespace Tizen { namespace Base { namespace Runtime
54 * Defines the type of thread.
56 * @brief <i> [Deprecated] </i>
57 * @deprecated This enum is deprecated.
65 THREAD_TYPE_WORKER = 0, /**< @if OSPDEPREC The worker thread mode @endif */
66 THREAD_TYPE_EVENT_DRIVEN, /**< @if OSPDEPREC The event-driven thread mode @endif */
67 THREAD_TYPE_MAIN // This enum value is for internal use only. Using this enum value can cause behavioral,
68 // security-related, and consistency-related issues in the application.
69 // The main thread mode
73 * @enum ThreadPriority
75 * Defines the priority of the thread.
82 THREAD_PRIORITY_HIGH, /**< The high priority*/
83 THREAD_PRIORITY_MID, /**< The mid priority*/
84 THREAD_PRIORITY_LOW, /**< The low priority*/
89 * @brief This class is the fundamental class for the asynchronous execution of a thread.
93 * @remarks This class supports only worker threads. For event-driven threads, use the EventDrivenThread class.
95 * The %Thread class is the fundamental class for the asynchronous execution of a thread.
96 * A Tizen native application can execute several threads during its operation from the multi-threading view.
98 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/thread.htm">Thread</a>.
100 * @see Tizen::Base::Runtime::EventDrivenThread
102 * The following example demonstrates how to use the %Thread class.
106 * using namespace Tizen::Base;
107 * using namespace Tizen::Base::Runtime;
117 * virtual ~MyThread(void)
121 * result Construct(void)
123 * return Thread::Construct();
134 * MyApplication::ThreadSample(void)
136 * MyThread* pMyThread = new MyThread;
138 * pMyThread->Construct();
140 * pMyThread->Start();
142 * pMyThread->Join(); // Waits until the thread finished the task
151 class _OSP_EXPORT_ Thread
153 , public Tizen::Base::Runtime::IRunnable
158 * Default stack size of the thread.
163 const static unsigned long DEFAULT_STACK_SIZE = 64 * 1024;
167 * Suspends the execution of the current thread for the specified interval.
171 * @return An error code
172 * @param[in] milliSeconds The time, in milliseconds, for which to suspend the execution @n
173 * A value of zero causes the thread to relinquish the remainder of its time
174 * slice to any other thread that is ready to run. The time cannot be negative.
175 * @exception E_SUCCESS The method is successful.
176 * @exception E_INVALID_ARG A negative time value is passed.
178 static result Sleep(long milliSeconds);
181 * Causes the current thread context to be temporarily paused and allows other threads to execute.
185 * @return An error code
186 * @exception E_SUCCESS The method is successful.
187 * @exception E_SYSTEM A system error has occurred.
189 static result Yield(void);
193 * After this method is called, the thread's execution is stopped.
197 * @return An error code
198 * @param[in] exitCode The exit code for the thread @n
199 * @exception E_SUCCESS The method is successful.
200 * @remarks Use GetExitCode() for getting the exit code set by this method.
202 static result Exit(int exitCode = 0x00);
205 * Gets the pointer of the currently running %Thread instance.
209 * @return A pointer to the currently running thread
211 static Thread* GetCurrentThread(void);
214 * This is the default constructor for this class.
218 * @remarks After creating an instance of this class, one of the
219 * Construct() methods must be called explicitly to initialize this instance.
225 * Initializes this instance of %Thread with the specified thread type, stack size, and priority.
227 * @brief <i> [Deprecated] </i>
228 * @deprecated This method is deprecated because the %Thread class does not support event-driven thread any more. Instead, use the EventDrivenThread class instead.
232 * @return An error code
233 * @param[in] threadType The thread type
234 * @param[in] stackSize The thread stack size
235 * @param[in] priority The thread priority
236 * @exception E_SUCCESS The method is successful.
237 * @exception E_INVALID_ARG An invalid argument is passed.
238 * @exception E_OUT_OF_MEMORY The memory is insufficient.
241 result Construct(ThreadType threadType, long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
245 * Initializes this instance of %Thread with the specified name, thread type, stack size, and priority.
247 * @brief <i> [Deprecated] </i>
248 * @deprecated This method is deprecated because the %Thread class does not support event-driven thread any more. Instead, use the EventDrivenThread class instead.
252 * @return An error code
253 * @param[in] name The name of the thread
254 * @param[in] threadType The thread type
255 * @param[in] stackSize The thread stack size
256 * @param[in] priority The thread priority
257 * @exception E_SUCCESS The method is successful.
258 * @exception E_INVALID_ARG An invalid argument is passed.
259 * @exception E_OUT_OF_MEMORY The memory is insufficient.
262 result Construct(const Tizen::Base::String& name, ThreadType threadType,
263 long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
266 * Initializes this instance of %Thread with the specified stack size, and priority.
270 * @return An error code
271 * @param[in] stackSize The thread stack size
272 * @param[in] priority The thread priority
273 * @exception E_SUCCESS The method is successful.
274 * @exception E_INVALID_ARG An invalid argument is passed.
275 * @exception E_OUT_OF_MEMORY The memory is insufficient.
277 result Construct(long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
280 * Initializes this instance of %Thread with the specified name, stack size, and priority.
284 * @return An error code
285 * @param[in] name The name of the thread
286 * @param[in] stackSize The thread stack size
287 * @param[in] priority The thread priority
288 * @exception E_SUCCESS The method is successful.
289 * @exception E_INVALID_ARG An invalid argument is passed.
290 * @exception E_OUT_OF_MEMORY The memory is insufficient.
292 result Construct(const Tizen::Base::String& name,
293 long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
296 * Initializes this instance of %Thread with the specified IRunnable instance, stack size, and priority.
300 * @return An error code
301 * @param[in] target An instance of %IRunnable
302 * @param[in] stackSize The thread stack size
303 * @param[in] priority The thread priority
304 * @exception E_SUCCESS The method is successful.
305 * @exception E_INVALID_ARG An invalid argument is passed.
306 * @exception E_OUT_OF_MEMORY The memory is insufficient.
308 result Construct(IRunnable& target, long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
311 * Initializes this instance of %Thread with the specified name, IRunnable instance, stack size, and priority.
315 * @return An error code
316 * @param[in] name The name of the thread
317 * @param[in] target An instance of IRunnable
318 * @param[in] stackSize The thread stack size
319 * @param[in] priority The thread priority
320 * @exception E_SUCCESS The method is successful.
321 * @exception E_INVALID_ARG An invalid argument is passed.
322 * @exception E_OUT_OF_MEMORY The memory is insufficient.
324 result Construct(const Tizen::Base::String& name, IRunnable& target,
325 long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
329 * This is the destructor for this class.
334 virtual ~Thread(void);
337 * Waits until the thread execution is terminated.
341 * @return An error code
342 * @exception E_SUCCESS The method is successful.
343 * @exception E_INVALID_OPERATION An other thread is calling this method.
344 * @exception E_SYSTEM A system error has occurred.
349 * Starts the thread. @n
350 * The Run() method is called when the thread starts.
354 * @return An error code
355 * @exception E_SUCCESS The method is successful.
356 * @exception E_SYSTEM A system error has occurred.
362 * Forces the thread to stop executing.
364 * @brief <i> [Deprecated] </i>
365 * @deprecated This method is deprecated.
369 * @return An error code
370 * @exception E_SUCCESS The method is successful.
371 * @exception E_SYSTEM A system error has occurred.
372 * @remarks This is only available for event-driven threads.
378 * Called when the thread is started without the IRunnable instance. @n
379 * The body for thread execution is specified by inheriting the %Thread class and implementing this method.
383 * @return It is just ignored because there is nowhere to take the returned object
384 * @remarks The default action of this method returns @c null.
386 virtual Tizen::Base::Object* Run(void);
389 * Gets an exit code of the thread which is given by calling the Exit() method.
393 * @return An error code
394 * @param[out] exitCode The exit code for the thread
395 * @exception E_SUCCESS The method is successful.
396 * @exception E_INVALID_STATE The thread is in an invalid state.
397 * @exception E_SYSTEM A system error has occurred.
399 result GetExitCode(int& exitCode) const;
402 * Gets the name of the thread.
406 * @return The name of the thread
407 * @exception E_SUCCESS The method is successful.
409 const Tizen::Base::String& GetName(void) const;
414 * Called before the Run() method is executed. @n
415 * The Run() method is executed if this method returns @c true, and @n
416 * if this method returns @c false, the thread is terminated immediately.
418 * @brief <i> [Deprecated] </i>
419 * @deprecated This method is deprecated because the %Thread class does not support event-driven threads.
423 * @return @c true if this thread can be run, @n
425 * @remarks You can initialize the event or event listener in this method for running this
426 * thread in an event-driven mode.
429 virtual bool OnStart(void);
433 * Called after the Run() method is executed.
435 * @brief <i> [Deprecated] </i>
436 * @deprecated This method is deprecated because the %Thread class does not support event-driven threads.
439 * @remarks You can finalize the event or event listener in this method for running this
440 * thread in an event-driven mode.
444 virtual void OnStop(void);
448 * Sends a user event to the event-driven thread.
450 * @brief <i> [Deprecated] </i>
451 * @deprecated This method is deprecated because the %Thread class does not support event-driven threads.
455 * @return An error code
456 * @param[in] requestId The user-defined event Id
457 * @param[in] pArgs A pointer to a list of arguments
458 * @exception E_SUCCESS The method is successful.
459 * @exception E_INVALID_OPERATION The thread is not an event-driven thread.
461 * @remarks This is only available for event-driven threads.
462 * @see OnUserEventReceivedN()
465 virtual result SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs);
469 * Called when the user event is received.
471 * @brief <i> [Deprecated] </i>
472 * @deprecated This method is deprecated because the %Thread class does not support event-driven threads.
476 * @param[in] requestId The user-defined event Id
477 * @param[in] pArgs A pointer to a list of arguments
478 * @see SendUserEvent()
481 virtual void OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs);
484 Thread(const Thread& rhs);
485 Thread& operator =(const Thread& rhs);
488 friend class _ThreadImpl;
489 class _ThreadImpl* __pThreadImpl;
492 } } } // Tizen::Base::Runtime
494 #endif // _FBASE_RT_THREAD_H_