• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 
16 /**
17  * @addtogroup DriverHdi
18  * @{
19  *
20  * @brief Provides APIs for a system ability to obtain hardware device interface (HDI) services,
21  * load or unload a device, and listen for service status, and capabilities for the hdi-gen tool to
22  * automatically generate code in interface description language (IDL).
23  *
24  * The HDF and IDL code generated allow the system ability to accesses HDI driver services.
25  *
26  * @since 1.0
27  */
28 
29 /**
30  * @file hdi_smq_syncer.h
31  *
32  * @brief Provides communication mechanisms, including the wait() and wake() APIs, for the shared memory queue (SMQ).
33  *
34  * @since 1.0
35  */
36 
37 #ifndef HDI_SHARED_MEM_QUEUE_SYNCER_H
38 #define HDI_SHARED_MEM_QUEUE_SYNCER_H
39 
40 #include <atomic>
41 #include <parcel.h>
42 #include <cstdint>
43 
44 namespace OHOS {
45 namespace HDI {
46 namespace Base {
47 /**
48  * @brief Defines the <b>SharedMemQueueSyncer</b> class.
49  */
50 class SharedMemQueueSyncer {
51 public:
52     explicit SharedMemQueueSyncer(std::atomic<uint32_t> *syncerPtr);
53     ~SharedMemQueueSyncer() = default;
54     /**
55      * @brief Enumerates the synchronization types.
56      */
57     enum SyncWord : uint32_t {
58         /** Synchronous write */
59         SYNC_WORD_WRITE = 0x01,
60         /** Synchronous read */
61         SYNC_WORD_READ = 0x02,
62     };
63 
64     /**
65      * @brief Waits until a certain condition becomes true.
66      * This API will invoke the private member function <b>FutexWait</b>.
67      *
68      * @param bitset Indicates the synchronization type.
69      * @param timeoutNanoSec Indicates the time to wait, in nanoseconds.
70      * @return Returns <b>0</b> if the caller is woken up.
71      */
72     int Wait(uint32_t bitset, int64_t timeoutNanoSec);
73 
74     /**
75      * @brief Wakes up a waiter.
76      *
77      * @param bitset Indicates the synchronization type.
78      * @return Returns <b>0</b> if the waiter is woken up.
79      */
80     int Wake(uint32_t bitset);
81 
82 private:
83     /**
84      * @brief Waits until a certain condition becomes true.
85      *
86      * @param bitset Indicates the synchronization type.
87      * @param timeoutNanoSec Indicates the time to wait, in nanoseconds.
88      * @return Returns <b>0</b> if the caller is woken up.
89      */
90     int FutexWait(uint32_t bitset, int64_t timeoutNanoSec);
91 
92     /**
93      * @brief Converts the wait time to real time.
94      *
95      * @param timeout Indicates the wait time.
96      * @param realtime Indicates the real time.
97      */
98     void TimeoutToRealtime(int64_t timeout, struct timespec &realtime);
99     std::atomic<uint32_t> *syncAddr_;
100 };
101 } // namespace Base
102 } // namespace HDI
103 } // namespace OHOS
104 
105 #endif /* HDI_SHARED_MEM_QUEUE_SYNCER_H */
106