1 /*
2  * Copyright (c) 2021-2023 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 HISTREAMER_FOUNDATION_OSAL_MUTEX_H
17 #define HISTREAMER_FOUNDATION_OSAL_MUTEX_H
18 
19 #ifdef MEDIA_FOUNDATION_FFRT
20     #include "ffrt_inner.h"
21 #else
22     #include <pthread.h>
23 #endif
24 
25 namespace OHOS {
26 namespace Media {
27 
28 #ifdef MEDIA_FOUNDATION_FFRT
29 using Mutex = ffrt::mutex;
30 #else
31 class Mutex {
32 public:
33     Mutex();
34 
35     virtual ~Mutex();
36 
37     Mutex(const Mutex&) = delete;
38 
39     Mutex operator=(const Mutex&) = delete;
40 
41     virtual void lock();
42 
43     bool try_lock();
44 
45     void unlock();
46 
47 private:
48     pthread_mutex_t nativeHandle_;
49     bool created_;
50     friend class ConditionVariable;
51 };
52 
53 class FairMutex : public Mutex {
54 public:
55     FairMutex();
56 
57     virtual ~FairMutex();
58 
59     FairMutex(const FairMutex&) = delete;
60 
61     FairMutex operator=(const FairMutex&) = delete;
62 
63     void lock() override;
64 
65 private:
66     pthread_mutex_t failLockHandle_;
67     bool created_{false};
68 };
69 #endif
70 } // namespace Media
71 } // namespace OHOS
72 #endif // HISTREAMER_FOUNDATION_OSAL_MUTEX_H
73