/* * Copyright (c) 2021-2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef UTILS_INCLUDE_SYNC_FENCE_H #define UTILS_INCLUDE_SYNC_FENCE_H #include #include #include #include #include #include #include #include namespace OHOS { using ns_sec_t = int64_t; // same to linux/sync_file define enum FenceStatus { ERROR = -1, ACTIVE = 0, SIGNALED = 1 }; struct SyncPointInfo { uint64_t timestampNs; FenceStatus status; }; class SyncFence : public RefBase { public: explicit SyncFence(int32_t fenceFd); /* When the SyncFence is destroyed, the fd will be closed in UniqueFd */ virtual ~SyncFence(); SyncFence(const SyncFence& rhs) = delete; SyncFence& operator=(const SyncFence& rhs) = delete; SyncFence(SyncFence&& rhs) = delete; SyncFence& operator=(SyncFence&& rhs) = delete; static const sptr INVALID_FENCE; static const ns_sec_t INVALID_TIMESTAMP; static const ns_sec_t FENCE_PENDING_TIMESTAMP; int32_t Wait(uint32_t timeout); static sptr MergeFence(const std::string &name, const sptr& fence1, const sptr& fence2); ns_sec_t SyncFileReadTimestamp(); int32_t Dup() const; bool IsValid() const; /* this is dangerous, when you use it, do not operator the fd */ int32_t Get() const; void WriteToMessageParcel(MessageParcel &parcel); static sptr ReadFromMessageParcel(MessageParcel &parcel); FenceStatus GetStatus(); static sptr InvalidFence(); private: std::vector GetFenceInfo(); UniqueFd fenceFd_; static int32_t SyncMerge(const char *name, int32_t fd1, int32_t fd2, int32_t &newFenceFd); }; } #endif // UTILS_INCLUDE_SYNC_FENCE_H