• 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 #include <utility>
22 
23 #include "internal_inc/types.h"
24 #include "tm/scpu_task.h"
25 namespace ffrt {
26 /* The relationship of VersionCtx is implemented using a doubly linked list:
27  * 0、data represents the root node of this data signature
28  * 1、Non-nested scenes: v1<—>v2<—>v3<—>data
29  * 2、Nested scenes: v1<—>v2.1<—>v2.2<—>v2<—>v3<—>data
30  */
31 
32 struct VersionCtx : private NonCopyable {
VersionCtxVersionCtx33     VersionCtx(const void* signature, VersionCtx* next, VersionCtx* last)
34         : signature(signature), next(next), last(last) {};
35     // Unique identifier for the data, taking the memory address of the actual data
36     const void* signature;
37 
38     // Nested scenes, is next version, in non-nested scenes, is the next sub version's parent version
39     VersionCtx* next {nullptr};
40     // Non-nested scenes, is last version, in nested scenes, is the parent's last sub version
41     VersionCtx* last {nullptr};
42 
43     // Current version's consumers, notify all when ready
44     std::unordered_set<SCPUEUTask*> consumers;
45     // Current version's producer
46     SCPUEUTask* myProducer {nullptr};
47     // Next version's producer, notify when consumed
48     SCPUEUTask* nextProducer {nullptr};
49 
50     DataStatus status {DataStatus::IDLE};
51     std::vector<SCPUEUTask*> dataWaitTaskByThis;
52 
53     void AddConsumer(SCPUEUTask* consumer, NestType nestType);
54     void AddProducer(SCPUEUTask* producer);
AddDataWaitTaskByThisVersionCtx55     inline void AddDataWaitTaskByThis(SCPUEUTask* dataWaitTask)
56     {
57         if (last != nullptr && last->status == DataStatus::IDLE) {
58             auto waitVersion = last;
59             waitVersion->dataWaitTaskByThis.push_back(dataWaitTask);
60             dataWaitTask->IncWaitDataRef();
61         }
62     }
63     void onProduced();
64     void onConsumed(SCPUEUTask* consumer);
65 protected:
66     void CreateChildVersion(SCPUEUTask* task, DataStatus dataStatus);
67     void MergeChildVersion();
NotifyDataWaitTaskVersionCtx68     inline void NotifyDataWaitTask()
69     {
70         for (auto& dataWaitTask : std::as_const(dataWaitTaskByThis)) {
71             dataWaitTask->DecWaitDataRef();
72         }
73         dataWaitTaskByThis.clear();
74     }
75 
NotifyConsumersVersionCtx76     inline void NotifyConsumers()
77     {
78         for (auto consumer : std::as_const(consumers)) {
79             consumer->DecDepRef();
80         }
81     }
82 
NotifyNextProducerVersionCtx83     inline void NotifyNextProducer()
84     {
85         if (nextProducer != nullptr) {
86             nextProducer->DecDepRef();
87             nextProducer = nullptr;
88         }
89     }
90 
MergeConsumerInDepVersionCtx91     inline void MergeConsumerInDep(VersionCtx* v)
92     {
93         for (const auto& consumer : std::as_const(v->consumers)) {
94             consumer->ins.insert(this);
95             consumer->ins.erase(consumer->ins.find(v));
96         }
97     }
98 
MergeProducerOutDepVersionCtx99     inline void MergeProducerOutDep(VersionCtx* v)
100     {
101         v->myProducer->outs.insert(this);
102         v->myProducer->outs.erase(v->myProducer->outs.find(v));
103     }
104 };
105 } /* namespace ffrt */
106 #endif
107