1 // Copyright 2011 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #ifndef NINJA_STATE_H_
16 #define NINJA_STATE_H_
31 /// A pool for delayed edges.
32 /// Pools are scoped to a State. Edges within a State will share Pools. A Pool
33 /// will keep a count of the total 'weight' of the currently scheduled edges. If
34 /// a Plan attempts to schedule an Edge which would cause the total weight to
35 /// exceed the depth of the Pool, the Pool will enque the Edge instead of
36 /// allowing the Plan to schedule it. The Pool will relinquish queued Edges when
37 /// the total scheduled weight diminishes enough (i.e. when a scheduled edge
40 Pool(const string& name, int depth)
41 : name_(name), current_use_(0), depth_(depth), delayed_(&WeightedEdgeCmp) {}
43 // A depth of 0 is infinite
44 bool is_valid() const { return depth_ >= 0; }
45 int depth() const { return depth_; }
46 const string& name() const { return name_; }
47 int current_use() const { return current_use_; }
49 /// true if the Pool might delay this edge
50 bool ShouldDelayEdge() const { return depth_ != 0; }
52 /// informs this Pool that the given edge is committed to be run.
53 /// Pool will count this edge as using resources from this pool.
54 void EdgeScheduled(const Edge& edge);
56 /// informs this Pool that the given edge is no longer runnable, and should
57 /// relinquish its resources back to the pool
58 void EdgeFinished(const Edge& edge);
60 /// adds the given edge to this Pool to be delayed.
61 void DelayEdge(Edge* edge);
63 /// Pool will add zero or more edges to the ready_queue
64 void RetrieveReadyEdges(set<Edge*>* ready_queue);
66 /// Dump the Pool and its edges (useful for debugging).
72 /// |current_use_| is the total of the weights of the edges which are
73 /// currently scheduled in the Plan (i.e. the edges in Plan::ready_).
77 static bool WeightedEdgeCmp(const Edge* a, const Edge* b);
79 typedef set<Edge*,bool(*)(const Edge*, const Edge*)> DelayedEdges;
80 DelayedEdges delayed_;
83 /// Global state (file status) for a single run.
85 static Pool kDefaultPool;
86 static Pool kConsolePool;
87 static const Rule kPhonyRule;
91 void AddPool(Pool* pool);
92 Pool* LookupPool(const string& pool_name);
94 Edge* AddEdge(const Rule* rule);
96 Node* GetNode(StringPiece path, unsigned int slash_bits);
97 Node* LookupNode(StringPiece path) const;
98 Node* SpellcheckNode(const string& path);
100 void AddIn(Edge* edge, StringPiece path, unsigned int slash_bits);
101 bool AddOut(Edge* edge, StringPiece path, unsigned int slash_bits);
102 bool AddDefault(StringPiece path, string* error);
104 /// Reset state. Keeps all nodes and edges, but restores them to the
105 /// state where we haven't yet examined the disk for dirty state.
108 /// Dump the nodes and Pools (useful for debugging).
111 /// @return the root node(s) of the graph. (Root nodes have no output edges).
112 /// @param error where to write the error message if somethings went wrong.
113 vector<Node*> RootNodes(string* error) const;
114 vector<Node*> DefaultNodes(string* error) const;
116 /// Mapping of path -> Node.
117 typedef ExternalStringHashMap<Node*>::Type Paths;
120 /// All the pools used in the graph.
121 map<string, Pool*> pools_;
123 /// All the edges of the graph.
124 vector<Edge*> edges_;
126 BindingEnv bindings_;
127 vector<Node*> defaults_;
130 #endif // NINJA_STATE_H_