[dali_2.3.22] Merge branch 'devel/master'
[platform/core/uifw/dali-core.git] / dali / devel-api / threading / mutex.h
1 #ifndef DALI_MUTEX_H
2 #define DALI_MUTEX_H
3
4 /*
5  * Copyright (c) 2021 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 /**
25  * The top level DALi namespace
26  */
27 namespace Dali
28 {
29 /**
30  * Class to synchronize access to critical resources from multiple threads
31  */
32 class DALI_CORE_API Mutex
33 {
34 public:
35   /**
36    * @brief Constructor, acquires the mutex from the underlying OS
37    */
38   Mutex();
39
40   /**
41    * @brief Destructor, non virtual as this is not meant as a base class
42    */
43   ~Mutex();
44
45   /**
46    * @brief Move constructor
47    */
48   Mutex(Mutex&& rhs) noexcept;
49
50   /**
51    * @brief Move assignment
52    */
53   Mutex& operator=(Mutex&& rhs) noexcept;
54
55   /**
56    * @brief Check if the mutex is locked
57    * @return true if the mutex is locked
58    */
59   bool IsLocked();
60
61 public:
62   /**
63    * Helper class to do a scoped lock on a mutex implementing the RAII idiom.
64    * Note! this class *does not* prevent a deadlock in the case when same thread is
65    * locking the same mutex twice.
66    */
67   class DALI_CORE_API ScopedLock
68   {
69   public:
70     /**
71      * Constructor
72      * @param mutex to lock
73      */
74     ScopedLock(Mutex& mutex);
75
76     /**
77      * Destructor, releases the lock
78      */
79     ~ScopedLock();
80
81   private:
82     Mutex& mMutex;
83   };
84
85 private:
86   /// Not implemented as Mutex is not copyable
87   Mutex(const Mutex&);
88   const Mutex& operator=(const Mutex&);
89
90   struct MutexImpl;
91   MutexImpl* mImpl;
92 };
93
94 } // namespace Dali
95
96 #endif // DALI_MUTEX_H