1 /*
2  * Copyright (c) 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 #include "cpp/deadline.h"
17 #include "c/deadline.h"
18 #include "internal_inc/osal.h"
19 #include "sched/interval.h"
20 #include "dm/dependence_manager.h"
21 #include "sched/frame_interval.h"
22 #include "dfx/log/ffrt_log_api.h"
23 
24 namespace ffrt {
25 class QosIntervalPrivate {
26 public:
27     template <typename... Args>
QosIntervalPrivate(uint64_t deadlineUs,const QoS & qos)28     QosIntervalPrivate(uint64_t deadlineUs, const QoS& qos)
29     {
30         if (qos == qos_user_interactive) {
31             it = std::make_unique<FrameInterval>(deadlineUs, qos);
32         } else {
33             it = std::make_unique<DefaultInterval>(deadlineUs, qos);
34         }
35     }
36 
operator ->()37     Interval* operator->()
38     {
39         if (it == nullptr) {
40             FFRT_LOGE("Invalid QoS Interval!");
41             return nullptr;
42         }
43         return it.get();
44     }
45 
46 private:
47     std::unique_ptr<Interval> it;
48 };
49 }; // namespace ffrt
50 
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54 API_ATTRIBUTE((visibility("default")))
55 ffrt_interval_t ffrt_interval_create(uint64_t deadline_us, ffrt_qos_t qos)
56 {
57     if (qos < static_cast<int>(ffrt_qos_deadline_request) || qos > static_cast<int>(ffrt_qos_user_interactive)) {
58         FFRT_LOGE("Invalid QoS Interval!");
59         return nullptr;
60     }
61 
62     return new ffrt::QosIntervalPrivate(deadline_us, qos);
63 }
64 
65 API_ATTRIBUTE((visibility("default")))
66 int ffrt_interval_update(ffrt_interval_t it, uint64_t new_deadline_us)
67 {
68     if (!it) {
69         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
70         return ffrt_error;
71     }
72 
73     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
74 
75     (*_it)->Update(new_deadline_us);
76     return ffrt_success;
77 }
78 
79 API_ATTRIBUTE((visibility("default")))
80 int ffrt_interval_begin(ffrt_interval_t it)
81 {
82     if (!it) {
83         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
84         return ffrt_error;
85     }
86 
87     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
88 
89     return (*_it)->Begin();
90 }
91 
92 API_ATTRIBUTE((visibility("default")))
93 int ffrt_interval_end(ffrt_interval_t it)
94 {
95     if (!it) {
96         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
97         return ffrt_error;
98     }
99 
100     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
101 
102     (*_it)->End();
103     return ffrt_success;
104 }
105 
106 API_ATTRIBUTE((visibility("default")))
107 void ffrt_interval_destroy(ffrt_interval_t it)
108 {
109     if (!it) {
110         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
111         return;
112     }
113 
114     delete static_cast<ffrt::QosIntervalPrivate *>(it);
115 }
116 
117 API_ATTRIBUTE((visibility("default")))
118 int ffrt_interval_join(ffrt_interval_t it)
119 {
120     if (!it) {
121         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
122         return ffrt_error;
123     }
124 
125     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
126 
127     (*_it)->Join();
128     return ffrt_success;
129 }
130 
131 API_ATTRIBUTE((visibility("default")))
132 int ffrt_interval_leave(ffrt_interval_t it)
133 {
134     if (!it) {
135         FFRT_LOGE("QoS Interval Not Created Or Has Been Canceled!");
136         return ffrt_error;
137     }
138 
139     auto _it = static_cast<ffrt::QosIntervalPrivate *>(it);
140 
141     (*_it)->Leave();
142     return ffrt_success;
143 }
144 #ifdef __cplusplus
145 }
146 #endif
147