• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 UTILS_INCLUDE_SYNC_FENCE_TIMELINE_H
17 #define UTILS_INCLUDE_SYNC_FENCE_TIMELINE_H
18 
19 #include <atomic>
20 #include <memory>
21 #include <mutex>
22 #include <queue>
23 #include <refbase.h>
24 
25 #include "sync_fence.h"
26 
27 namespace OHOS {
28 class SyncFenceTime {
29 public:
30     explicit SyncFenceTime(const sptr<SyncFence>& fence);
31     explicit SyncFenceTime(sptr<SyncFence>&& fence);
32 
33     // Do not allow default construction.
34     SyncFenceTime() = delete;
35     ~SyncFenceTime() = default;
36 
37     // Do not allow copy, assign, move
38     SyncFenceTime(const SyncFence& rhs) = delete;
39     SyncFenceTime& operator=(const SyncFence& rhs) = delete;
40     SyncFenceTime(SyncFence&& rhs) = delete;
41     SyncFenceTime& operator=(SyncFence&& rhs) = delete;
42 
43     bool IsValid() const;
44 
45     ns_sec_t GetSignalTimestamp();
46     ns_sec_t GetCachedSignalTimestamp() const;
47 private:
48     mutable std::mutex mutex_;
49     sptr<SyncFence> fence_ {SyncFence::INVALID_FENCE};
50     std::atomic<ns_sec_t> signaledTimestamps_ {SyncFence::INVALID_TIMESTAMP};
51 };
52 
53 class SyncFenceTimeline {
54 public:
55     static const size_t MAX_COUNT = 128;
56 
57     SyncFenceTimeline() = default;
58     ~SyncFenceTimeline() = default;
59 
60     void Push(const std::shared_ptr<SyncFenceTime>& fence);
61     void UpdateFenceTimeline();
62 
63 private:
64     mutable std::mutex mutex_;
65     std::queue<std::weak_ptr<SyncFenceTime>> queue_;
66 };
67 } // namespace OHOS
68 
69 #endif