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/*======
5This file is part of PerconaFT.
6
7
8Copyright (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#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39#pragma once
40
41#include "locktree/txnid_set.h"
42#include "util/omt.h"
43
44namespace toku {
45
46// A wfg is a 'wait-for' graph. A directed edge in represents one
47// txn waiting for another to finish before it can acquire a lock.
48
49class wfg {
50public:
51 // Create a lock request graph
52 void create(void);
53
54 // Destroy the internals of the lock request graph
55 void destroy(void);
56
57 // Add an edge (a_id, b_id) to the graph
58 void add_edge(TXNID a_txnid, TXNID b_txnid);
59
60 // Return true if a node with the given transaction id exists in the graph.
61 // Return false otherwise.
62 bool node_exists(TXNID txnid);
63
64 // Return true if there exists a cycle from a given transaction id in the graph.
65 // Return false otherwise.
66 bool cycle_exists_from_txnid(TXNID txnid);
67
68 // Apply a given function f to all of the nodes in the graph. The apply function
69 // returns when the function f is called for all of the nodes in the graph, or the
70 // function f returns non-zero.
71 void apply_nodes(int (*fn)(TXNID txnid, void *extra), void *extra);
72
73 // Apply a given function f to all of the edges whose origin is a given node id.
74 // The apply function returns when the function f is called for all edges in the
75 // graph rooted at node id, or the function f returns non-zero.
76 void apply_edges(TXNID txnid,
77 int (*fn)(TXNID txnid, TXNID edge_txnid, void *extra), void *extra);
78
79private:
80 struct node {
81 // txnid for this node and the associated set of edges
82 TXNID txnid;
83 txnid_set edges;
84 bool visited;
85
86 static node *alloc(TXNID txnid);
87
88 static void free(node *n);
89 };
90 ENSURE_POD(node);
91
92 toku::omt<node *> m_nodes;
93
94 node *find_node(TXNID txnid);
95
96 node *find_create_node(TXNID txnid);
97
98 bool cycle_exists_from_node(node *target, node *head);
99
100 static int find_by_txnid(node *const &node_a, const TXNID &txnid_b);
101};
102ENSURE_POD(wfg);
103
104} /* namespace toku */
105