13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci#include "semaphore_ex.h" 163f4cbf05Sopenharmony_ci 173f4cbf05Sopenharmony_ci#include <pthread.h> // pthread_mutex_lock 183f4cbf05Sopenharmony_ci#include <sys/types.h> // pid_t 193f4cbf05Sopenharmony_ci#include <unistd.h> // getpid 203f4cbf05Sopenharmony_ci#include <fcntl.h> // O_CREAT 213f4cbf05Sopenharmony_ci#include <semaphore.h> // sem_t 223f4cbf05Sopenharmony_ci 233f4cbf05Sopenharmony_ci#include <sstream> // ostringstream 243f4cbf05Sopenharmony_ci#include <iomanip> // setw/setfill 253f4cbf05Sopenharmony_ci 263f4cbf05Sopenharmony_ciusing namespace std; 273f4cbf05Sopenharmony_ci 283f4cbf05Sopenharmony_cinamespace OHOS { 293f4cbf05Sopenharmony_civoid Semaphore::Wait() 303f4cbf05Sopenharmony_ci{ 313f4cbf05Sopenharmony_ci std::unique_lock<std::mutex> lck(mutex_); 323f4cbf05Sopenharmony_ci if (--count_ < 0) { 333f4cbf05Sopenharmony_ci cv_.wait(lck); 343f4cbf05Sopenharmony_ci } 353f4cbf05Sopenharmony_ci} 363f4cbf05Sopenharmony_ci 373f4cbf05Sopenharmony_civoid Semaphore::Post() 383f4cbf05Sopenharmony_ci{ 393f4cbf05Sopenharmony_ci std::unique_lock<mutex> lck(mutex_); 403f4cbf05Sopenharmony_ci if (++count_ <= 0) { 413f4cbf05Sopenharmony_ci cv_.notify_one(); 423f4cbf05Sopenharmony_ci } 433f4cbf05Sopenharmony_ci} 443f4cbf05Sopenharmony_ci 453f4cbf05Sopenharmony_ci} // namespace OHOS 46