106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci#include "posix_semaphore.h"
1606f6ba60Sopenharmony_ci#include <memory>
1706f6ba60Sopenharmony_ci#include <ctime>
1806f6ba60Sopenharmony_ci
1906f6ba60Sopenharmony_cinamespace {
2006f6ba60Sopenharmony_ciconstexpr int NS_PER_SEC = 1000 * 1000 * 1000;
2106f6ba60Sopenharmony_ci}
2206f6ba60Sopenharmony_ci
2306f6ba60Sopenharmony_ciPosixSemaphore::PosixSemaphore(unsigned int value)
2406f6ba60Sopenharmony_ci{
2506f6ba60Sopenharmony_ci    sem_init(&sem_, 0, value);
2606f6ba60Sopenharmony_ci}
2706f6ba60Sopenharmony_ci
2806f6ba60Sopenharmony_ciPosixSemaphore::~PosixSemaphore()
2906f6ba60Sopenharmony_ci{
3006f6ba60Sopenharmony_ci    sem_destroy(&sem_);
3106f6ba60Sopenharmony_ci}
3206f6ba60Sopenharmony_ci
3306f6ba60Sopenharmony_cibool PosixSemaphore::Wait()
3406f6ba60Sopenharmony_ci{
3506f6ba60Sopenharmony_ci    return sem_wait(&sem_) == 0;
3606f6ba60Sopenharmony_ci}
3706f6ba60Sopenharmony_ci
3806f6ba60Sopenharmony_cibool PosixSemaphore::TryWait()
3906f6ba60Sopenharmony_ci{
4006f6ba60Sopenharmony_ci    return sem_trywait(&sem_) == 0;
4106f6ba60Sopenharmony_ci}
4206f6ba60Sopenharmony_ci
4306f6ba60Sopenharmony_cibool PosixSemaphore::TimedWait(int seconds, int nanoSeconds)
4406f6ba60Sopenharmony_ci{
4506f6ba60Sopenharmony_ci    struct timespec ts = { 0, 0 };
4606f6ba60Sopenharmony_ci    clock_gettime(CLOCK_REALTIME, &ts);
4706f6ba60Sopenharmony_ci    ts.tv_sec += seconds;
4806f6ba60Sopenharmony_ci    ts.tv_nsec += nanoSeconds;
4906f6ba60Sopenharmony_ci    ts.tv_sec += ts.tv_nsec / NS_PER_SEC;
5006f6ba60Sopenharmony_ci    ts.tv_nsec %= NS_PER_SEC;
5106f6ba60Sopenharmony_ci    return sem_timedwait(&sem_, &ts) == 0;
5206f6ba60Sopenharmony_ci}
5306f6ba60Sopenharmony_ci
5406f6ba60Sopenharmony_cibool PosixSemaphore::Post()
5506f6ba60Sopenharmony_ci{
5606f6ba60Sopenharmony_ci    return sem_post(&sem_) == 0;
5706f6ba60Sopenharmony_ci}
5806f6ba60Sopenharmony_ci
5906f6ba60Sopenharmony_ciunsigned PosixSemaphore::Value() const
6006f6ba60Sopenharmony_ci{
6106f6ba60Sopenharmony_ci    int value = 0;
6206f6ba60Sopenharmony_ci    sem_getvalue(&sem_, &value);
6306f6ba60Sopenharmony_ci    return value;
6406f6ba60Sopenharmony_ci}
6506f6ba60Sopenharmony_ci
6606f6ba60Sopenharmony_ciSemaphorePtr PosixSemaphoreFactory::Create(unsigned int value)
6706f6ba60Sopenharmony_ci{
6806f6ba60Sopenharmony_ci    return std::make_shared<PosixSemaphore>(value);
6906f6ba60Sopenharmony_ci}
70