1bafb9395Sopenharmony_ci/*
2bafb9395Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd.
3bafb9395Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bafb9395Sopenharmony_ci * you may not use this file except in compliance with the License.
5bafb9395Sopenharmony_ci * You may obtain a copy of the License at
6bafb9395Sopenharmony_ci *
7bafb9395Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bafb9395Sopenharmony_ci *
9bafb9395Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bafb9395Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bafb9395Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bafb9395Sopenharmony_ci * See the License for the specific language governing permissions and
13bafb9395Sopenharmony_ci * limitations under the License.
14bafb9395Sopenharmony_ci */
15bafb9395Sopenharmony_ci
16bafb9395Sopenharmony_ci#ifndef GRAPHIC_LITE_GRAPHIC_MUTEX_H
17bafb9395Sopenharmony_ci#define GRAPHIC_LITE_GRAPHIC_MUTEX_H
18bafb9395Sopenharmony_ci#include "stdbool.h"
19bafb9395Sopenharmony_ci#include "stdint.h"
20bafb9395Sopenharmony_ci#ifdef _WIN32
21bafb9395Sopenharmony_ci#include <windows.h>
22bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
23bafb9395Sopenharmony_ci#include <pthread.h>
24bafb9395Sopenharmony_ci#else
25bafb9395Sopenharmony_ci#include "los_mux.h"
26bafb9395Sopenharmony_ci#endif // WIN32
27bafb9395Sopenharmony_ci#include "gfx_utils/heap_base.h"
28bafb9395Sopenharmony_ci
29bafb9395Sopenharmony_cinamespace OHOS {
30bafb9395Sopenharmony_ci/** @brief graphic mutex adapter for different platform. */
31bafb9395Sopenharmony_ciclass GraphicMutex : public HeapBase {
32bafb9395Sopenharmony_cipublic:
33bafb9395Sopenharmony_ci    /** Default constructor */
34bafb9395Sopenharmony_ci    GraphicMutex()
35bafb9395Sopenharmony_ci    {
36bafb9395Sopenharmony_ci#ifdef _WIN32
37bafb9395Sopenharmony_ci        mutex_ = CreateMutex(NULL, FALSE, NULL);
38bafb9395Sopenharmony_ci        initFlag_ = (mutex_ != NULL);
39bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
40bafb9395Sopenharmony_ci        initFlag_ = (pthread_mutex_init(&mutex_, NULL) == 0);
41bafb9395Sopenharmony_ci#else
42bafb9395Sopenharmony_ci        initFlag_ = (LOS_MuxCreate(&mutex_) == LOS_OK);
43bafb9395Sopenharmony_ci#endif // WIN32
44bafb9395Sopenharmony_ci    }
45bafb9395Sopenharmony_ci
46bafb9395Sopenharmony_ci    /** Default destructor */
47bafb9395Sopenharmony_ci    ~GraphicMutex()
48bafb9395Sopenharmony_ci    {
49bafb9395Sopenharmony_ci        if (!initFlag_) {
50bafb9395Sopenharmony_ci            return;
51bafb9395Sopenharmony_ci        }
52bafb9395Sopenharmony_ci#ifdef _WIN32
53bafb9395Sopenharmony_ci        CloseHandle(mutex_);
54bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
55bafb9395Sopenharmony_ci        pthread_mutex_destroy(&mutex_);
56bafb9395Sopenharmony_ci#else
57bafb9395Sopenharmony_ci        LOS_MuxDelete(mutex_);
58bafb9395Sopenharmony_ci#endif // WIN32
59bafb9395Sopenharmony_ci    }
60bafb9395Sopenharmony_ci
61bafb9395Sopenharmony_ci    inline bool Lock()
62bafb9395Sopenharmony_ci    {
63bafb9395Sopenharmony_ci        if (!initFlag_) {
64bafb9395Sopenharmony_ci            return false;
65bafb9395Sopenharmony_ci        }
66bafb9395Sopenharmony_ci#ifdef _WIN32
67bafb9395Sopenharmony_ci        return (WaitForSingleObject(mutex_, INFINITE) == WAIT_OBJECT_0);
68bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
69bafb9395Sopenharmony_ci        return (pthread_mutex_lock(&mutex_) == 0);
70bafb9395Sopenharmony_ci#else
71bafb9395Sopenharmony_ci        return (LOS_MuxPend(mutex_, LOS_WAIT_FOREVER) == LOS_OK);
72bafb9395Sopenharmony_ci#endif // WIN32
73bafb9395Sopenharmony_ci    }
74bafb9395Sopenharmony_ci
75bafb9395Sopenharmony_ci    inline bool Unlock()
76bafb9395Sopenharmony_ci    {
77bafb9395Sopenharmony_ci        if (!initFlag_) {
78bafb9395Sopenharmony_ci            return false;
79bafb9395Sopenharmony_ci        }
80bafb9395Sopenharmony_ci#ifdef _WIN32
81bafb9395Sopenharmony_ci        return ReleaseMutex(mutex_);
82bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
83bafb9395Sopenharmony_ci        return (pthread_mutex_unlock(&mutex_) == 0);
84bafb9395Sopenharmony_ci#else
85bafb9395Sopenharmony_ci        return (LOS_MuxPost(mutex_) == LOS_OK);
86bafb9395Sopenharmony_ci#endif // WIN32
87bafb9395Sopenharmony_ci    }
88bafb9395Sopenharmony_ci
89bafb9395Sopenharmony_ciprivate:
90bafb9395Sopenharmony_ci    bool initFlag_;
91bafb9395Sopenharmony_ci#ifdef _WIN32
92bafb9395Sopenharmony_ci    HANDLE mutex_;
93bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
94bafb9395Sopenharmony_ci    pthread_mutex_t mutex_;
95bafb9395Sopenharmony_ci#else
96bafb9395Sopenharmony_ci    uint32_t mutex_;
97bafb9395Sopenharmony_ci#endif // WIN32
98bafb9395Sopenharmony_ci};
99bafb9395Sopenharmony_ci} // namespace OHOS
100bafb9395Sopenharmony_ci#endif // GRAPHIC_LITE_GRAPHIC_MUTEX_H
101