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