DALi Version 2.0.1
[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) 2020 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 Check if the mutex is locked
47    * @return true if the mutex is locked
48    */
49   bool IsLocked();
50
51 public:
52   /**
53    * Helper class to do a scoped lock on a mutex implementing the RAII idiom.
54    * Note! this class *does not* prevent a deadlock in the case when same thread is
55    * locking the same mutex twice.
56    */
57   class DALI_CORE_API ScopedLock
58   {
59   public:
60     /**
61      * Constructor
62      * @param mutex to lock
63      */
64     ScopedLock(Mutex& mutex);
65
66     /**
67      * Destructor, releases the lock
68      */
69     ~ScopedLock();
70
71   private:
72     Mutex& mMutex;
73   };
74
75 private:
76   /// Not implemented as Mutex is not copyable
77   Mutex(const Mutex&);
78   const Mutex& operator=(const Mutex&);
79
80   struct MutexImpl;
81   MutexImpl* mImpl;
82 };
83
84 } // namespace Dali
85
86 #endif // DALI_MUTEX_H