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 FBaseRtEventDrivenThread.h
19 * @brief This is the header file for the %EventDrivenThread class.
21 * This header file contains the declarations of the %EventDrivenThread class.
23 #ifndef _FBASE_RT_EVENT_DRIVEN_THREAD_H_
24 #define _FBASE_RT_EVENT_DRIVEN_THREAD_H_
27 #include <FBaseResult.h>
28 #include <FBaseObject.h>
29 #include <FBaseRtThread.h>
31 namespace Tizen { namespace Base { class String; }}
33 namespace Tizen { namespace Base { namespace Runtime
37 * @class EventDrivenThread
38 * @brief This class is the fundamental class for the asynchronous execution of a thread.
42 * The %EventDrivenThread class is the fundamental class for the asynchronous execution of a thread.
43 * A %Tizen native application can execute several threads during its operation from the multi-threading view.
45 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/thread.htm">Thread</a>.
47 * @see Tizen::Base::Runtime::EventDrivenThread
49 * The following example demonstrates how to use the %EventDrivenThread class.
55 * using namespace Tizen::Base;
56 * using namespace Tizen::Base::Runtime;
59 * : public EventDrivenThread
60 * , public ITimerEventListener
69 * virtual ~MyTimerThread(void)
74 * result Construct(void)
76 * return EventDrivenThread::Construct();
81 * __pTimer = new Timer;
83 * __pTimer->Construct(*this);
85 * __pTimer->StartAsRepeatable(1000);
95 * void OnTimerExpired(Timer& timer)
97 * // Do repetitive tasks
98 * AppLog("MyTimerThread: OnTimerExpired: %d", __count++);
107 * MyApplication::StartEventDrivenThreadSample(void)
109 * __pMyTimerThread = new MyThread;
111 * __pMyTimerThread->Construct();
113 * __pMyTimerThread->Start();
118 * MyApplication::StopEventDrivenThreadSample(void)
121 * __pMyTimerThread->Quit();
123 * __pMyTimerThread->Join();
125 * delete __pMyTimerThread;
131 class _OSP_EXPORT_ EventDrivenThread
136 * This is the default constructor for this class.
140 * @remarks After creating an instance of this class, one of the
141 * Construct() methods must be called explicitly to initialize this instance.
143 EventDrivenThread(void);
146 * Initializes this instance of %EventDrivenThread with the specified thread type, stack size, and priority.
150 * @return An error code
151 * @param[in] stackSize The thread stack size
152 * @param[in] priority The thread priority
153 * @exception E_SUCCESS The method is successful.
154 * @exception E_INVALID_ARG An invalid argument is passed.
155 * @exception E_OUT_OF_MEMORY The memory is insufficient.
157 result Construct(long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
160 * Initializes this instance of %EventDrivenThread with the specified name, thread type, stack size, and priority.
164 * @return An error code
165 * @param[in] name The name of the thread
166 * @param[in] stackSize The thread stack size
167 * @param[in] priority The thread priority
168 * @exception E_SUCCESS The method is successful.
169 * @exception E_INVALID_ARG An invalid argument is passed.
170 * @exception E_OUT_OF_MEMORY The memory is insufficient.
172 result Construct(const Tizen::Base::String& name, long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
175 * This is the destructor for this class.
180 virtual ~EventDrivenThread(void);
183 * Sends a termination request to the thread.
187 * @return An error code
188 * @exception E_SUCCESS The method is successful.
189 * @exception E_INVALID_STATE The thread is in an invalid state.
190 * @exception E_SYSTEM A system error has occurred.
192 virtual result Quit(void);
195 * Called when the thread is started and runs a message loop.
199 * @final Although this method is virtual, it must not be overridden.
200 * If overridden, it may not work as expected.
202 * @return It is just ignored because there is nowhere to take the returned object
203 * @remarks The default action of this method returns @c null.
205 virtual Tizen::Base::Object* Run(void);
208 * Called before the message loop is started. @n
209 * If this method returns @c false, the thread is terminated immediately.
213 * @return @c true if this thread can be run, @n
215 * @remarks You can initialize the event or event listener in this method for running this thread.
218 virtual bool OnStart(void);
221 * Called after the message loop is stopped.
225 * @remarks You can finalize the event or event listener in this method for running this thread.
228 virtual void OnStop(void);
231 * Sends a user event to the event-driven thread.
235 * @final Although this method is virtual, it must not be overridden.
236 * If overridden, it may not work as expected.
238 * @return An error code
239 * @param[in] requestId The user-defined event ID
240 * @param[in] pArgs A pointer to a list of arguments
241 * @exception E_SUCCESS The method is successful.
242 * @exception E_INVALID_STATE The thread is in an invalid state.
243 * @exception E_OUT_OF_MEMORY The memory is insufficient.
244 * @exception E_SYSTEM A system error has occurred.
246 * @see OnUserEventReceivedN()
248 virtual result SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs);
251 * Called when the user event is received.
255 * @param[in] requestId The user-defined event ID
256 * @param[in] pArgs A pointer to a list of arguments
258 * @see SendUserEvent()
260 virtual void OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs);
263 EventDrivenThread(const EventDrivenThread& rhs);
264 EventDrivenThread& operator = (const EventDrivenThread& rhs);
267 class _EventDrivenThreadImpl* __pEventDrivenThreadImpl;
268 friend class _EventDrivenThreadImpl;
270 }; // EventDrivenThread
272 } } } // Tizen::Base::Runtime
274 #endif // _FBASE_RT_EVENT_DRIVEN_THREAD_H_