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_SEMAPHORE_H
17bafb9395Sopenharmony_ci#define GRAPHIC_LITE_GRAPHIC_SEMAPHORE_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 <limits.h>
24bafb9395Sopenharmony_ci#include <semaphore.h>
25bafb9395Sopenharmony_ci#else
26bafb9395Sopenharmony_ci#include "los_sem.h"
27bafb9395Sopenharmony_ci#endif // WIN32
28bafb9395Sopenharmony_ci#include "gfx_utils/heap_base.h"
29bafb9395Sopenharmony_ci
30bafb9395Sopenharmony_cinamespace OHOS {
31bafb9395Sopenharmony_cistatic constexpr int32_t MAX_SEM_COUNT = 1000; // 1000: max number of semaphore count
32bafb9395Sopenharmony_ci
33bafb9395Sopenharmony_ci/** @brief Semaphore adapter for different platform. */
34bafb9395Sopenharmony_ciclass GraphicSemaphore : public HeapBase {
35bafb9395Sopenharmony_cipublic:
36bafb9395Sopenharmony_ci    /** Default constructor */
37bafb9395Sopenharmony_ci    GraphicSemaphore() : GraphicSemaphore(0, MAX_SEM_COUNT) {}
38bafb9395Sopenharmony_ci
39bafb9395Sopenharmony_ci    GraphicSemaphore(int32_t init) : GraphicSemaphore(init, MAX_SEM_COUNT) {}
40bafb9395Sopenharmony_ci
41bafb9395Sopenharmony_ci    GraphicSemaphore(int32_t init, int32_t max)
42bafb9395Sopenharmony_ci    {
43bafb9395Sopenharmony_ci        if (max > MAX_SEM_COUNT) {
44bafb9395Sopenharmony_ci            max = MAX_SEM_COUNT;
45bafb9395Sopenharmony_ci        }
46bafb9395Sopenharmony_ci        if (init > max) {
47bafb9395Sopenharmony_ci            init = max;
48bafb9395Sopenharmony_ci        }
49bafb9395Sopenharmony_ci#ifdef _WIN32
50bafb9395Sopenharmony_ci        sem_ = CreateSemaphore(NULL, init, max, NULL);
51bafb9395Sopenharmony_ci        initFlag_ = (sem_ != nullptr);
52bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
53bafb9395Sopenharmony_ci        initFlag_ = (sem_init(&sem_, 0, init) == 0);
54bafb9395Sopenharmony_ci#else
55bafb9395Sopenharmony_ci        if (max == 1) {
56bafb9395Sopenharmony_ci            initFlag_ = (LOS_BinarySemCreate((uint16_t)init, &sem_) == LOS_OK);
57bafb9395Sopenharmony_ci        } else {
58bafb9395Sopenharmony_ci            initFlag_ = (LOS_SemCreate((uint16_t)init, &sem_) == LOS_OK);
59bafb9395Sopenharmony_ci        }
60bafb9395Sopenharmony_ci#endif // WIN32
61bafb9395Sopenharmony_ci    }
62bafb9395Sopenharmony_ci
63bafb9395Sopenharmony_ci    /** Default destructor */
64bafb9395Sopenharmony_ci    ~GraphicSemaphore()
65bafb9395Sopenharmony_ci    {
66bafb9395Sopenharmony_ci        if (!initFlag_) {
67bafb9395Sopenharmony_ci            return;
68bafb9395Sopenharmony_ci        }
69bafb9395Sopenharmony_ci#ifdef _WIN32
70bafb9395Sopenharmony_ci        CloseHandle(sem_);
71bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
72bafb9395Sopenharmony_ci        sem_destroy(&sem_);
73bafb9395Sopenharmony_ci#else
74bafb9395Sopenharmony_ci        LOS_SemDelete(sem_);
75bafb9395Sopenharmony_ci#endif // WIN32
76bafb9395Sopenharmony_ci    }
77bafb9395Sopenharmony_ci
78bafb9395Sopenharmony_ci    /** Increases the count of the specified semaphore object by a specified amount. */
79bafb9395Sopenharmony_ci    inline bool Notify()
80bafb9395Sopenharmony_ci    {
81bafb9395Sopenharmony_ci        if (!initFlag_) {
82bafb9395Sopenharmony_ci            return false;
83bafb9395Sopenharmony_ci        }
84bafb9395Sopenharmony_ci#ifdef _WIN32
85bafb9395Sopenharmony_ci        return ReleaseSemaphore(sem_, 1, NULL);
86bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
87bafb9395Sopenharmony_ci        return (sem_post(&sem_) == 0);
88bafb9395Sopenharmony_ci#else
89bafb9395Sopenharmony_ci        return (LOS_SemPost(sem_) == LOS_OK);
90bafb9395Sopenharmony_ci#endif // WIN32
91bafb9395Sopenharmony_ci    }
92bafb9395Sopenharmony_ci
93bafb9395Sopenharmony_ci    /** Waits until the specified object is in the signaled state. */
94bafb9395Sopenharmony_ci    inline bool Wait()
95bafb9395Sopenharmony_ci    {
96bafb9395Sopenharmony_ci        if (!initFlag_) {
97bafb9395Sopenharmony_ci            return false;
98bafb9395Sopenharmony_ci        }
99bafb9395Sopenharmony_ci#ifdef _WIN32
100bafb9395Sopenharmony_ci        return (WaitForSingleObject(sem_, INFINITE) == WAIT_OBJECT_0);
101bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
102bafb9395Sopenharmony_ci        return (sem_wait(&sem_) == 0);
103bafb9395Sopenharmony_ci#else
104bafb9395Sopenharmony_ci        return (LOS_SemPend(sem_, LOS_WAIT_FOREVER) == LOS_OK);
105bafb9395Sopenharmony_ci#endif // WIN32
106bafb9395Sopenharmony_ci    }
107bafb9395Sopenharmony_ci
108bafb9395Sopenharmony_ciprivate:
109bafb9395Sopenharmony_ci    bool initFlag_;
110bafb9395Sopenharmony_ci#ifdef _WIN32
111bafb9395Sopenharmony_ci    HANDLE sem_;
112bafb9395Sopenharmony_ci#elif defined __linux__ || defined __LITEOS__ || defined __APPLE__
113bafb9395Sopenharmony_ci    sem_t sem_;
114bafb9395Sopenharmony_ci#else
115bafb9395Sopenharmony_ci    uint32_t sem_;
116bafb9395Sopenharmony_ci#endif // WIN32
117bafb9395Sopenharmony_ci};
118bafb9395Sopenharmony_ci} // namespace OHOS
119bafb9395Sopenharmony_ci#endif // GRAPHIC_LITE_GRAPHIC_SEMAPHORE_H
120