1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 
36 ----------------------------------------
37 
38    Licensed under the Apache License, Version 2.0 (the "License");
39    you may not use this file except in compliance with the License.
40    You may obtain a copy of the License at
41 
42        http://www.apache.org/licenses/LICENSE-2.0
43 
44    Unless required by applicable law or agreed to in writing, software
45    distributed under the License is distributed on an "AS IS" BASIS,
46    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47    See the License for the specific language governing permissions and
48    limitations under the License.
49 ======= */
50 
51 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
52 
53 #pragma once
54 
55 #include "locktree/txnid_set.h"
56 #include "util/omt.h"
57 
58 namespace toku {
59 
60 // A wfg is a 'wait-for' graph. A directed edge in represents one
61 // txn waiting for another to finish before it can acquire a lock.
62 
63 class wfg {
64 public:
65     // Create a lock request graph
66     void create(void);
67 
68     // Destroy the internals of the lock request graph
69     void destroy(void);
70 
71     // Add an edge (a_id, b_id) to the graph
72     void add_edge(TXNID a_txnid, TXNID b_txnid);
73 
74     // Return true if a node with the given transaction id exists in the graph.
75     // Return false otherwise.
76     bool node_exists(TXNID txnid);
77 
78     // Return true if there exists a cycle from a given transaction id in the graph.
79     // Return false otherwise.
80     bool cycle_exists_from_txnid(TXNID txnid);
81 
82     // Apply a given function f to all of the nodes in the graph.  The apply function
83     // returns when the function f is called for all of the nodes in the graph, or the
84     // function f returns non-zero.
85     void apply_nodes(int (*fn)(TXNID txnid, void *extra), void *extra);
86 
87     // Apply a given function f to all of the edges whose origin is a given node id.
88     // The apply function returns when the function f is called for all edges in the
89     // graph rooted at node id, or the function f returns non-zero.
90     void apply_edges(TXNID txnid,
91             int (*fn)(TXNID txnid, TXNID edge_txnid, void *extra), void *extra);
92 
93 private:
94     struct node {
95         // txnid for this node and the associated set of edges
96         TXNID txnid;
97         txnid_set edges;
98         bool visited;
99 
100         static node *alloc(TXNID txnid);
101 
102         static void free(node *n);
103     };
104     ENSURE_POD(node);
105 
106     toku::omt<node *> m_nodes;
107 
108     node *find_node(TXNID txnid);
109 
110     node *find_create_node(TXNID txnid);
111 
112     bool cycle_exists_from_node(node *target, node *head);
113 
114     static int find_by_txnid(node *const &node_a, const TXNID &txnid_b);
115 };
116 ENSURE_POD(wfg);
117 
118 } /* namespace toku */
119