Revert "Change Dali::Thread, Dali::Mutex and Dali::ConditionalWait to use std::thread...
[platform/core/uifw/dali-core.git] / dali / devel-api / threading / conditional-wait.h
1 #ifndef __DALI_CONDITIONAL_WAIT_H__
2 #define __DALI_CONDITIONAL_WAIT_H__
3
4 /*
5  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // INTERNAL INCLUDES
22 #include <dali/public-api/common/dali-common.h>
23
24 namespace Dali
25 {
26
27 /**
28  * Helper class to allow conditional waiting and notifications between multiple threads.
29  */
30 class DALI_IMPORT_API ConditionalWait
31 {
32 public:
33
34   /**
35    * @brief Allows client code to synchronize updates to its own state with the
36    * internal state of a ConditionalWait object.
37    */
38   class ScopedLock
39   {
40   public:
41     /**
42      * Constructor
43      * @brief Will acquire the internal mutex of the ConditionalWait object passed in.
44      * @param[in] wait The ConditionalWait object to lock.
45      */
46     ScopedLock( ConditionalWait& wait );
47
48     /**
49      * Destructor
50      * @brief Will release the internal mutex of the ConditionalWait object passed in.
51      */
52     ~ScopedLock();
53
54     /**
55      * Getter for the ConditionalWait locked for this instance's lifetime.
56      * @return the ConditionalWait object currently locked.
57      */
58     ConditionalWait& GetLockedWait() const { return mWait; }
59
60   private:
61
62     // Not implemented as ScopedLock cannot be copied:
63     ScopedLock( const ScopedLock& );
64     const ScopedLock& operator=( const ScopedLock& );
65
66     ConditionalWait& mWait;
67   };
68
69   /**
70    * @brief Constructor, creates the internal synchronization objects
71    */
72   ConditionalWait();
73
74   /**
75    * @brief Destructor, non-virtual as this is not a base class
76    */
77   ~ConditionalWait();
78
79   /**
80    * @brief Notifies another thread to continue if it is blocked on a wait.
81    *
82    * Can be called from any thread.
83    * Does not block the current thread but may cause a rescheduling of threads.
84    */
85   void Notify();
86
87   /**
88    * @brief Notifies another thread to continue if it is blocked on a wait.
89    *
90    * Assumes that the ScopedLock object passed in has already locked the internal state of
91    * this object.
92    * Can be called from any thread.
93    * Does not block the current thread but may cause a rescheduling of threads.
94    *
95    * @param[in] scope A pre-existing lock on the internal state of this object.
96    */
97   void Notify( const ScopedLock& scope );
98
99   /**
100    * @brief Wait for another thread to notify us when the condition is true and we can continue
101    *
102    * Will always block current thread until Notify is called
103    */
104   void Wait();
105
106   /**
107    * @brief Wait for another thread to notify us when the condition is true and we can continue
108    *
109    * Will always block current thread until Notify is called.
110    * Assumes that the ScopedLock object passed in has already locked the internal state of
111    * this object. Releases the lock while waiting and re-acquires it when returning
112    * from the wait.
113    * @param[in] scope A pre-existing lock on the internal state of this object.
114    * @pre scope must have been passed this ConditionalWait during its construction.
115    */
116   void Wait( const ScopedLock& scope );
117
118   /**
119    * @brief Return the count of threads waiting for this conditional
120    * @return count of waits
121    */
122   unsigned int GetWaitCount() const;
123
124 private:
125
126   // Not implemented as ConditionalWait is not copyable
127   ConditionalWait( const ConditionalWait& );
128   const ConditionalWait& operator=( const ConditionalWait& );
129
130   struct ConditionalWaitImpl;
131   ConditionalWaitImpl* mImpl;
132
133 };
134
135 } // namespace Dali
136
137 #endif // __DALI_CONDITIONAL_WAIT_H__