1 /* 2 * Copyright (c) 2024 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 #ifndef FUTEX_TOOL_H 16 #define FUTEX_TOOL_H 17 18 #include <atomic> 19 #include <unistd.h> 20 #include <functional> 21 22 namespace OHOS { 23 namespace AudioStandard { 24 namespace { 25 const uint32_t IS_READY = 0; 26 const uint32_t IS_NOT_READY = 1; 27 const uint32_t IS_PRE_EXIT = 2; 28 } 29 enum FutexCode : int32_t { 30 FUTEX_SUCCESS = 0, 31 FUTEX_TIMEOUT, 32 FUTEX_INVALID_PARAMS, 33 FUTEX_OPERATION_FAILED, 34 FUTEX_PRE_EXIT, 35 }; 36 class FutexTool { 37 public: 38 /** 39 * FutexWait will first try change futexPtr from IS_READY to IS_NOT_READY, then acomicly wait on IS_NOT_READY. 40 * After Waked up, will check futexPtr == IS_NOT_READY 41 */ 42 static FutexCode FutexWait(std::atomic<uint32_t> *futexPtr, int64_t timeout, const std::function<bool(void)> &pred); 43 static FutexCode FutexWake(std::atomic<uint32_t> *futexPtr, uint32_t wakeVal = IS_READY); 44 }; 45 } // namespace AudioStandard 46 } // namespace OHOS 47 #endif // FUTEX_TOOL_H 48