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 FBaseRtEventDrivenThread.h
20 * @brief This is the header file for the %EventDrivenThread class.
22 * This header file contains the declarations of the %EventDrivenThread class.
24 #ifndef _FBASE_RT_EVENT_DRIVEN_THREAD_H_
25 #define _FBASE_RT_EVENT_DRIVEN_THREAD_H_
28 #include <FBaseResult.h>
29 #include <FBaseObject.h>
30 #include <FBaseRtThread.h>
32 namespace Tizen { namespace Base { class String; }}
34 namespace Tizen { namespace Base { namespace Runtime
38 * @class EventDrivenThread
39 * @brief This class is the fundamental class for the asynchronous execution of a thread.
43 * The %EventDrivenThread class is the fundamental class for the asynchronous execution of a thread.
44 * A %Tizen native application can execute several threads during its operation from the multi-threading view.
46 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/thread.htm">Thread</a>.
48 * @see Tizen::Base::Runtime::EventDrivenThread
50 * The following example demonstrates how to use the %EventDrivenThread class.
56 * using namespace Tizen::Base;
57 * using namespace Tizen::Base::Runtime;
60 * : public EventDrivenThread
61 * , public ITimerEventListener
70 * virtual ~MyTimerThread(void)
75 * result Construct(void)
77 * return EventDrivenThread::Construct();
82 * __pTimer = new Timer;
84 * __pTimer->Construct(*this);
86 * __pTimer->StartAsRepeatable(1000);
96 * void OnTimerExpired(Timer& timer)
98 * // Do repetitive tasks
99 * AppLog("MyTimerThread: OnTimerExpired: %d", __count++);
108 * MyApplication::StartEventDrivenThreadSample(void)
110 * __pMyTimerThread = new MyThread;
112 * __pMyTimerThread->Construct();
114 * __pMyTimerThread->Start();
119 * MyApplication::StopEventDrivenThreadSample(void)
122 * __pMyTimerThread->Quit();
124 * __pMyTimerThread->Join();
126 * delete __pMyTimerThread;
132 class _OSP_EXPORT_ EventDrivenThread
137 * This is the default constructor for this class.
141 * @remarks After creating an instance of this class, one of the
142 * Construct() methods must be called explicitly to initialize this instance.
144 EventDrivenThread(void);
147 * Initializes this instance of %EventDrivenThread with the specified thread type, stack size, and priority.
151 * @return An error code
152 * @param[in] stackSize The thread stack size
153 * @param[in] priority The thread priority
154 * @exception E_SUCCESS The method is successful.
155 * @exception E_INVALID_ARG An invalid argument is passed.
156 * @exception E_OUT_OF_MEMORY The memory is insufficient.
158 result Construct(long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
161 * Initializes this instance of %EventDrivenThread with the specified name, thread type, stack size, and priority.
165 * @return An error code
166 * @param[in] name The name of the thread
167 * @param[in] stackSize The thread stack size
168 * @param[in] priority The thread priority
169 * @exception E_SUCCESS The method is successful.
170 * @exception E_INVALID_ARG An invalid argument is passed.
171 * @exception E_OUT_OF_MEMORY The memory is insufficient.
173 result Construct(const Tizen::Base::String& name, long stackSize = DEFAULT_STACK_SIZE, ThreadPriority priority = THREAD_PRIORITY_MID);
176 * This is the destructor for this class.
181 virtual ~EventDrivenThread(void);
184 * Sends a termination request to the thread.
188 * @return An error code
189 * @exception E_SUCCESS The method is successful.
190 * @exception E_INVALID_STATE The thread is in an invalid state.
191 * @exception E_SYSTEM A system error has occurred.
193 virtual result Quit(void);
196 * Called when the thread is started and runs a message loop.
200 * @final Although this method is virtual, it must not be overridden.
201 * If overridden, it may not work as expected.
203 * @return It is just ignored because there is nowhere to take the returned object
204 * @remarks The default action of this method returns @c null.
206 virtual Tizen::Base::Object* Run(void);
209 * Called before the message loop is started. @n
210 * If this method returns @c false, the thread is terminated immediately.
214 * @return @c true if this thread can be run, @n
216 * @remarks You can initialize the event or event listener in this method for running this thread.
219 virtual bool OnStart(void);
222 * Called after the message loop is stopped.
226 * @remarks You can finalize the event or event listener in this method for running this thread.
229 virtual void OnStop(void);
232 * Sends a user event to the event-driven thread.
236 * @final Although this method is virtual, it must not be overridden.
237 * If overridden, it may not work as expected.
239 * @return An error code
240 * @param[in] requestId The user-defined event ID
241 * @param[in] pArgs A pointer to a list of arguments
242 * @exception E_SUCCESS The method is successful.
243 * @exception E_INVALID_STATE The thread is in an invalid state.
244 * @exception E_OUT_OF_MEMORY The memory is insufficient.
245 * @exception E_SYSTEM A system error has occurred.
247 * @see OnUserEventReceivedN()
249 virtual result SendUserEvent(RequestId requestId, const Tizen::Base::Collection::IList* pArgs);
252 * Called when the user event is received.
256 * @param[in] requestId The user-defined event ID
257 * @param[in] pArgs A pointer to a list of arguments
259 * @see SendUserEvent()
261 virtual void OnUserEventReceivedN(RequestId requestId, Tizen::Base::Collection::IList* pArgs);
264 EventDrivenThread(const EventDrivenThread& rhs);
265 EventDrivenThread& operator = (const EventDrivenThread& rhs);
268 class _EventDrivenThreadImpl* __pEventDrivenThreadImpl;
269 friend class _EventDrivenThreadImpl;
271 }; // EventDrivenThread
273 } } } // Tizen::Base::Runtime
275 #endif // _FBASE_RT_EVENT_DRIVEN_THREAD_H_