1 /* 2 * Copyright (c) 2025 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 #ifndef FILEMANAGEMENT_FILE_API_INTERFACES_KITS_IOURING_HYPER_AIO_H 17 #define FILEMANAGEMENT_FILE_API_INTERFACES_KITS_IOURING_HYPER_AIO_H 18 19 #include <functional> 20 #include <memory> 21 #include <mutex> 22 #include <thread> 23 24 25 namespace OHOS { 26 namespace HyperAio { 27 #define IOURING_APP_PERMISSION (1U << 0) 28 29 #ifndef EOK 30 #define EOK (0) 31 #endif 32 33 struct ReadInfo { 34 int32_t fd; 35 uint32_t len; 36 uint64_t offset; 37 void *buf; 38 uint64_t userData; 39 }; 40 41 struct ReadReqs { 42 uint32_t reqNum; 43 struct ReadInfo *reqs; 44 }; 45 46 struct OpenInfo { 47 int32_t dfd; 48 int32_t flags; 49 uint32_t mode; 50 void *path; 51 uint64_t userData; 52 }; 53 54 struct OpenReqs { 55 uint32_t reqNum; 56 struct OpenInfo *reqs; 57 }; 58 59 struct CancelInfo { 60 uint64_t userData; 61 uint64_t targetUserData; 62 }; 63 64 struct CancelReqs { 65 uint32_t reqNum; 66 struct CancelInfo *reqs; 67 }; 68 69 struct IoResponse { 70 uint64_t userData; 71 int32_t res; 72 uint32_t flags; IoResponseIoResponse73 IoResponse(uint64_t userData, int32_t res, uint32_t flags) 74 : userData(userData), 75 res(res), 76 flags(flags) { 77 } 78 }; 79 80 #define DECLARE_PIMPL(ClassName) \ 81 class Impl; \ 82 std::shared_ptr<Impl> pImpl_ 83 84 class HyperAio { 85 public: 86 using ProcessIoResultCallBack = std::function<void(std::unique_ptr<IoResponse>)>; 87 uint32_t SupportIouring(); 88 int32_t CtxInit(ProcessIoResultCallBack *callBack); 89 int32_t StartReadReqs(ReadReqs *req); 90 int32_t StartOpenReqs(OpenReqs *req); 91 int32_t StartCancelReqs(CancelReqs *req); 92 int32_t DestroyCtx(); 93 private: 94 DECLARE_PIMPL(HyperAio); 95 ProcessIoResultCallBack ioResultCallBack_ = nullptr; 96 std::thread harvestThread_; 97 std::atomic<bool> stopThread_ = true; 98 std::atomic<bool> initialized_ = false; 99 void HarvestRes(); 100 void HandleRequestError(std::vector<uint64_t> &errorVec, int32_t errorcode); 101 void HandleSqeError(uint32_t count, std::vector<uint64_t> &infoVec); 102 int32_t CheckParameter(uint32_t reqNum); 103 }; 104 } 105 } 106 #endif