• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef FFRT_VERSION_CTX_H
17 #define FFRT_VERSION_CTX_H
18 #include <unordered_set>
19 #include <vector>
20 #include <string>
21 
22 #include "core/task_ctx.h"
23 #include "internal_inc/types.h"
24 namespace ffrt {
25 /* The relationship of VersionCtx is implemented using a doubly linked list:
26  * 0、data represents the root node of this data signature
27  * 1、Non-nested scenes: v1<—>v2<—>v3<—>data
28  * 2、Nested scenes: v1<—>v2.1<—>v2.2<—>v2<—>v3<—>data
29  */
30 
31 struct VersionCtx : private NonCopyable {
VersionCtxVersionCtx32     VersionCtx(const void* signature, VersionCtx* next, VersionCtx* last)
33         : signature(signature), next(next), last(last) {};
34     // Unique identifier for the data, taking the memory address of the actual data
35     const void* signature;
36 
37     // Nested scenes, is next version, in non-nested scenes, is the next sub version's parent version
38     VersionCtx* next {nullptr};
39     // Non-nested scenes, is last version, in nested scenes, is the parent's last sub version
40     VersionCtx* last {nullptr};
41 
42     // Current version's consumers, notify all when ready
43     std::unordered_set<TaskCtx*> consumers;
44     // Current version's producer
45     TaskCtx* myProducer {nullptr};
46     // Next version's producer, notify when consumed
47     TaskCtx* nextProducer {nullptr};
48 
49     DataStatus status {DataStatus::IDLE};
50     std::vector<TaskCtx*> dataWaitTaskByThis;
51 
52     void AddConsumer(TaskCtx* consumer, NestType nestType);
53     void AddProducer(TaskCtx* producer);
AddDataWaitTaskByThisVersionCtx54     inline void AddDataWaitTaskByThis(TaskCtx* dataWaitTask)
55     {
56         if (last != nullptr && last->status == DataStatus::IDLE) {
57             auto waitVersion = last;
58             waitVersion->dataWaitTaskByThis.push_back(dataWaitTask);
59             dataWaitTask->IncWaitDataRef();
60         }
61     }
62     void onProduced();
63     void onConsumed(TaskCtx* consumer);
64 protected:
65     void CreateChildVersion(TaskCtx* task, DataStatus dataStatus);
66     void MergeChildVersion();
NotifyDataWaitTaskVersionCtx67     inline void NotifyDataWaitTask()
68     {
69         for (auto& dataWaitTask : std::as_const(dataWaitTaskByThis)) {
70             dataWaitTask->DecWaitDataRef();
71         }
72         dataWaitTaskByThis.clear();
73     }
74 
NotifyConsumersVersionCtx75     inline void NotifyConsumers()
76     {
77         for (auto consumer : std::as_const(consumers)) {
78             consumer->DecDepRef();
79         }
80     }
81 
NotifyNextProducerVersionCtx82     inline void NotifyNextProducer()
83     {
84         if (nextProducer != nullptr) {
85             nextProducer->DecDepRef();
86             nextProducer = nullptr;
87         }
88     }
89 
MergeConsumerInDepVersionCtx90     inline void MergeConsumerInDep(VersionCtx* v)
91     {
92         for (const auto& consumer : std::as_const(v->consumers)) {
93             consumer->ins.insert(this);
94             consumer->ins.erase(consumer->ins.find(v));
95         }
96     }
97 
MergeProducerOutDepVersionCtx98     inline void MergeProducerOutDep(VersionCtx* v)
99     {
100         v->myProducer->outs.insert(this);
101         v->myProducer->outs.erase(v->myProducer->outs.find(v));
102     }
103 };
104 } /* namespace ffrt */
105 #endif