1 /*
2  * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef GRAPHIC_LITE_GRAPHIC_LOCKER_H
17 #define GRAPHIC_LITE_GRAPHIC_LOCKER_H
18 
19 #include "stdint.h"
20 #if defined __linux__ || defined __LITEOS__ || defined __APPLE__
21 #include <pthread.h>
22 #endif
23 
24 namespace OHOS {
25 #if defined __linux__ || defined __LITEOS__ || defined __APPLE__
26 class GraphicLocker {
27 public:
GraphicLocker(pthread_mutex_t & mutex)28     explicit GraphicLocker(pthread_mutex_t& mutex) : mutex_(mutex)
29     {
30         pthread_mutex_lock(&mutex_);
31     }
32 
~GraphicLocker()33     ~GraphicLocker()
34     {
35         pthread_mutex_unlock(&mutex_);
36     }
37 
38     GraphicLocker() = delete;
39     GraphicLocker(const GraphicLocker&) = delete;
40     GraphicLocker& operator=(const GraphicLocker&) = delete;
41 
42 private:
43     pthread_mutex_t& mutex_;
44 };
45 
46 class GraphicMutex {
47 public:
GraphicMutex()48     GraphicMutex()
49     {
50         pthread_mutexattr_t attr;
51         pthread_mutexattr_init(&attr);
52         pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
53         pthread_mutex_init(&mutex_, &attr);
54     }
~GraphicMutex()55     ~GraphicMutex()
56     {
57         pthread_mutex_destroy(&mutex_);
58     }
Lock()59     void Lock()
60     {
61         pthread_mutex_lock(&mutex_);
62     }
Unlock()63     void Unlock()
64     {
65         pthread_mutex_unlock(&mutex_);
66     }
67     GraphicMutex(const GraphicMutex&) = delete;
68     GraphicMutex(const GraphicMutex&&) = delete;
69     GraphicMutex& operator=(const GraphicMutex&) = delete;
70     GraphicMutex& operator=(const GraphicMutex&&) = delete;
71 
72 private:
73     pthread_mutex_t mutex_;
74 };
75 #else
76 class GraphicMutex {
77 public:
78     GraphicMutex() = default;
79     ~GraphicMutex() = default;
80     void Lock() {}
81     void Unlock() {}
82     GraphicMutex(const GraphicMutex&) = delete;
83     GraphicMutex(const GraphicMutex&&) = delete;
84     GraphicMutex& operator=(const GraphicMutex&) = delete;
85     GraphicMutex& operator=(const GraphicMutex&&) = delete;
86 };
87 #endif
88 
89 // do not support multi-thread
90 class GraphicLockGuard {
91 public:
GraphicLockGuard(GraphicMutex & mutex)92     explicit GraphicLockGuard(GraphicMutex& mutex) : mutex_(mutex)
93     {
94         Lock();
95     }
~GraphicLockGuard()96     ~GraphicLockGuard()
97     {
98         Unlock();
99     }
Lock()100     void Lock()
101     {
102         mutex_.Lock();
103         lockCnt_++;
104     }
Unlock()105     void Unlock()
106     {
107         if (lockCnt_ > 0) {
108             mutex_.Unlock();
109             lockCnt_--;
110         }
111     }
112 
113     GraphicLockGuard() = delete;
114     GraphicLockGuard(const GraphicLockGuard&) = delete;
115     GraphicLockGuard(const GraphicLockGuard&&) = delete;
116     GraphicLockGuard& operator=(const GraphicLockGuard&) = delete;
117     GraphicLockGuard& operator=(const GraphicLockGuard&&) = delete;
118 
119 private:
120     GraphicMutex& mutex_;
121     int8_t lockCnt_ = 0;
122 };
123 } // namespace OHOS
124 #endif
125