56da6d5ac4985930b61a46de629715cb60ac2b50
[platform/framework/native/appfw.git] / src / base / runtime / FBaseRt_WaitingLoopImpl.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file        FBaseRt_WaitingLoopImpl.h
20  * @brief       This is the header file for the %_WaitingLoopImpl class.
21  *
22  * This file contains the declarations of the %_WaitingLoopImpl class.
23  */
24
25 #ifndef _FBASE_RT_INTERNAL_WAITING_LOOP_IMPL_H_
26 #define _FBASE_RT_INTERNAL_WAITING_LOOP_IMPL_H_
27
28 #include <glib.h>
29 #include <FBaseObject.h>
30 #include <FBaseRtThread.h>
31 #include <FBaseRtTimer.h>
32 #include <FBaseRtITimerEventListener.h>
33
34 namespace Tizen { namespace Base { namespace Runtime
35 {
36
37 class WaitingLoop;
38 class IWaitingLoopCondition;
39 /*
40  * @class _WaitingLoopImpl
41  * @brief This class allows developers to make the current thread wait for callback events. This class should be used only for test codes and don't use this class for commercial applications
42  * @since 2.0
43  *
44  * @final This class is not intended for extension.
45  */
46 class _OSP_EXPORT_ _WaitingLoopImpl
47         : public Tizen::Base::Object
48         , public Tizen::Base::Runtime::ITimerEventListener
49 {
50 public:
51         /*
52          * This is the destructor for this class.
53          *
54          * @since 2.0
55          */
56         virtual ~_WaitingLoopImpl(void);
57
58         /*
59          * Gets the WaitingLoop instance.
60          *
61          * @since 2.0
62          *
63          * @return      A pointer to the WaitingLoop instance, @n
64          *                              else @c null if it fails   
65          */
66         static WaitingLoop* GetInstanceN(void);
67
68         /*
69          * Starts the waiting loop and Waits until the time is expired.
70          *
71          * @since 2.0
72          *
73          * @param[in] timeout   The timeout period in milliseconds  
74          * @exception E_SUCCESS    The method is successful.
75          * @exception E_TIMEOUT    The time is expired.
76          * @exception E_INVALID_STATE    The waiting loop has already been waiting on a callback event. In order to wait again, you must quit the waiting loop.
77          * @remarks The timeout set to min(timeout, maximum), where the maximum is set using SetMaxTimeoutForWaiting().
78          * @see Notify(), SetMaxTimoutForWaiting()
79          */
80         result Wait(int timeout);
81
82         /*
83          * Starts the waiting loop and Waits until the expiring condition occurs.
84          *
85          * @since 2.0
86          *
87          * @param[in] condition The expiring condition  
88          * @exception E_SUCCESS    The method is successful.
89          * @exception E_INVALID_STATE    The waiting loop has already been waiting on a callback event. In order to wait again, you must quit the waiting loop.
90          * @remarks In order to use this method correctly, you have to implement a class inheriting from %IWaitingLoopCondition. 
91          */
92         result Wait(IWaitingLoopCondition& condition);
93
94         /*
95          * Starts the waiting loop and Waits until either the expiring condition occurs or the time is expired.
96          *
97          * @since 2.0
98          *
99          * @param[in] timeout   The timeout period in milliseconds  
100          * @param[in] condition The expiring condition  
101          * @exception E_TIMEOUT    The time is expired.
102          * @exception E_INVALID_STATE    The waiting loop is waiting on a thread. In order to wait again, you must quit the waiting loop.
103          * @remarks If the timeout is over the maximum, the expired time goes together with the maximum timeout. In order to use this method correctly, you have to implement a class inheriting from %IWaitingLoopCondition interface
104          */
105         result Wait(int timeout, IWaitingLoopCondition& condition);
106
107         /*
108          * Notify the waiting thread that the related callback has completed the job.
109          *
110          * @since 2.0
111          *
112          * @remarks This method is recommended to use when the waiting condition is not explicitly specified with Wait().
113          */
114         void Notify(void);
115
116         /*
117          * Called when the timer is expired.
118          *
119          * @since 2.0
120          *
121          * @param[in] timer The expired timer instance
122          */
123         virtual void OnTimerExpired(Timer& timer);
124
125         /*
126          * This default constructor is intentionally declared as private to implement the %Singleton semantic.
127          *
128          * @since 2.0
129          *
130          * @return A boolean value
131          */
132         bool IsSameThread(void);
133
134         /*
135          * Sets the maximum timeout for waiting.
136          *
137          * @since 2.0
138          *
139          * @param[in] timeout    The timeout period in milliseconds
140          *                              else @c false
141          * @remarks If the timeout is considered as a small number, it will be replaced with the specified number provided by platform.
142          */
143         void SetMaxTimeoutForWaiting(int timeout);
144
145 private:
146         //
147         // This default constructor is intentionally declared as private to implement the %Singleton semantic.
148         //
149         // @since 2.0
150         //
151         _WaitingLoopImpl(void);
152
153         //
154         // The implementation of this copy constructor is intentionally blank and declared as private to prohibit copying of objects.
155         //
156         // @since 2.0
157         //
158         _WaitingLoopImpl(const _WaitingLoopImpl& rhs);
159
160         //
161         // The implementation of this copy assignment operator is intentionally blank and delcared as private to prohibit copying of objects.
162         //
163         // @since 2.0
164         //
165         _WaitingLoopImpl& operator =(const _WaitingLoopImpl& rhs);
166
167         //
168         // Constructs the instance of this class.
169         //
170         // @since 2.0
171         //
172         // @return An error code
173         // @exception E_SUCCESS The method is successful.
174         //
175         result Construct(void);
176
177         enum WaitType
178         {
179                 WAIT_DEFAULT = 0,
180                 WAIT_TIMEOUT,
181                 WAIT_CONDITION,
182                 WAIT_TIMEOUT_CONDITION
183         };
184
185         enum WaitStatus
186         {
187                 WAIT_ON = 0,
188                 WAIT_OFF
189         };
190
191         Tizen::Base::Runtime::Timer* __pTimer;
192         bool __timeout; 
193         bool __notifyWait;
194         Tizen::Base::Runtime::ThreadType __threadType;
195         GMainContext* __pGmainContext;
196         WaitType __waitType;
197         WaitStatus __waitStatus;
198         IWaitingLoopCondition* __pWaitingLoopCondition;
199         int __timerCalledCount;
200         int __maxTimeout;
201                 
202 }; // _WaitingLoopImpl
203
204 } } } // Tizen::Base::Runtime
205
206 #endif // _FBASE_RT_INTERNAL_WAITING_LOOP_IMPL_H_