1 /* 2 * Copyright (c) 2021 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 #include "semaphore_ex.h" 16 17 #include <pthread.h> // pthread_mutex_lock 18 #include <sys/types.h> // pid_t 19 #include <unistd.h> // getpid 20 #include <fcntl.h> // O_CREAT 21 #include <semaphore.h> // sem_t 22 23 #include <sstream> // ostringstream 24 #include <iomanip> // setw/setfill 25 26 using namespace std; 27 28 namespace OHOS { Wait()29void Semaphore::Wait() 30 { 31 std::unique_lock<std::mutex> lck(mutex_); 32 if (--count_ < 0) { 33 cv_.wait(lck); 34 } 35 } 36 Post()37void Semaphore::Post() 38 { 39 std::unique_lock<mutex> lck(mutex_); 40 if (++count_ <= 0) { 41 cv_.notify_one(); 42 } 43 } 44 45 } // namespace OHOS 46