Remove boost threading from resource loading
[platform/core/uifw/dali-adaptor.git] / adaptors / base / conditional-wait.h
1 #ifndef __DALI_INTERNAL_CONDITIONAL_WAIT_H__
2 #define __DALI_INTERNAL_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 namespace Dali
22 {
23
24 namespace Internal
25 {
26
27 namespace Adaptor
28 {
29
30 /**
31  * Helper class to allow conditional waiting and notifications between multiple threads.
32  */
33 class ConditionalWait
34 {
35 public:
36
37   /**
38    * @brief Allows client code to synchronize updates to its own state with the
39    * internal state of a ConditionalWait object.
40    */
41   class ScopedLock
42   {
43   public:
44     /**
45      * Constructor
46      * @brief Will acquire the internal mutex of the ConditionalWait object passed in.
47      * @param[in] wait The ConditionalWait object to lock.
48      */
49     ScopedLock( ConditionalWait& wait );
50
51     /**
52      * Destructor
53      * @brief Will release the internal mutex of the ConditionalWait object passed in.
54      */
55     ~ScopedLock();
56
57     /**
58      * Getter for the ConditionalWait locked for this instance's lifetime.
59      * @return the ConditionalWait object currently locked.
60      */
61     ConditionalWait& GetLockedWait() const { return mWait; }
62
63   private:
64
65     // Not implemented as ScopedLock cannot be copied:
66     ScopedLock( const ScopedLock& );
67     const ScopedLock& operator=( const ScopedLock& );
68
69     ConditionalWait& mWait;
70   };
71
72   /**
73    * @brief Constructor, creates the internal synchronization objects
74    */
75   ConditionalWait();
76
77   /**
78    * @brief Destructor, non-virtual as this is not a base class
79    */
80   ~ConditionalWait();
81
82   /**
83    * @brief Notifies another thread to continue if it is blocked on a wait.
84    *
85    * Can be called from any thread.
86    * Does not block the current thread but may cause a rescheduling of threads.
87    */
88   void Notify();
89
90   /**
91    * @brief Wait for another thread to notify us when the condition is true and we can continue
92    *
93    * Will always block current thread until Notify is called
94    */
95   void Wait();
96
97   /**
98    * @brief Wait for another thread to notify us when the condition is true and we can continue
99    *
100    * Will always block current thread until Notify is called.
101    * Assumes that the ScopedLock object passed in has already locked the internal state of
102    * this object. Releases the lock while waiting and re-acquires it when returning
103    * from the wait.
104    * param[in] scope A preexisting lock on the internal state of this object.
105    * @pre scope must have been passed this ConditionalWait during its construction.
106    */
107   void Wait( const ScopedLock& scope );
108
109   /**
110    * @brief Return the count of threads waiting for this conditional
111    * @return count of waits
112    */
113   unsigned int GetWaitCount() const;
114
115 private:
116
117   // Not implemented as ConditionalWait is not copyable
118   ConditionalWait( const ConditionalWait& );
119   const ConditionalWait& operator=( const ConditionalWait& );
120
121   struct ConditionalWaitImpl;
122   ConditionalWaitImpl* mImpl;
123
124 };
125
126 } // namespace Adaptor
127
128 } // namespace Internal
129
130 } // namespace Dali
131
132 #endif // __DALI_INTERNAL_CONDITIONAL_WAIT_H__