34 #ifndef GRAPHLAB_RW_LOCK_MANAGER_HPP
35 #define GRAPHLAB_RW_LOCK_MANAGER_HPP
39 #include <graphlab/parallel/pthread_tools.hpp>
42 #include <graphlab/macros_def.hpp>
46 template<
typename Graph>
47 class rw_lock_manager {
52 typedef typename graph_type::edge_list_type edge_list_type;
53 typedef typename edge_list_type::const_iterator iterator;
58 std::vector<rwlock> locks;
62 graph(graph), locks(graph.num_vertices()) { }
66 locks[vid].writelock();
71 locks[vid].readlock();
76 return locks[vid].try_readlock();
85 const edge_list_type in_edges = graph.in_edges(vid);
86 const edge_list_type out_edges = graph.out_edges(vid);
88 bool processed_center =
false;
89 iterator in = in_edges.begin(), out = out_edges.begin();
90 while(in != in_edges.end() && out != out_edges.end()) {
93 if(__builtin_expect(!processed_center &&
94 vid < in_vid && vid < out_vid,
false)) {
95 processed_center =
true; writelock_vertex(vid);
96 }
else if (in_vid == out_vid) {
97 readlock_vertex(in_vid); ++in; ++out;
98 }
else if(in_vid < out_vid) {
99 readlock_vertex(in_vid); ++in;
101 readlock_vertex(out_vid); ++out;
105 while(in != in_edges.end()) {
107 if(__builtin_expect(!processed_center && vid < in_vid,
false)) {
108 processed_center =
true; writelock_vertex(vid);
109 }
else { readlock_vertex(in_vid); ++in; }
112 while(out != out_edges.end()) {
114 if(__builtin_expect(!processed_center && vid < out_vid,
false)) {
115 processed_center =
true; writelock_vertex(vid);
116 }
else { readlock_vertex(out_vid); ++out; }
119 if(__builtin_expect(!processed_center,
false)) { writelock_vertex(vid); }
123 const edge_list_type in_edges = graph.in_edges(vid);
124 const edge_list_type out_edges = graph.out_edges(vid);
126 bool processed_center =
false;
127 iterator in = in_edges.begin(), out = out_edges.begin();
128 while(in != in_edges.end() && out != out_edges.end()) {
131 if(__builtin_expect(!processed_center &&
132 vid < in_vid && vid < out_vid,
false)) {
133 processed_center =
true; writelock_vertex(vid);
134 }
else if (in_vid == out_vid) {
135 writelock_vertex(in_vid); ++in; ++out;
136 }
else if(in_vid < out_vid) {
137 writelock_vertex(in_vid); ++in;
139 writelock_vertex(out_vid); ++out;
143 while(in != in_edges.end()) {
145 if(__builtin_expect(!processed_center && vid < in_vid,
false)) {
146 processed_center =
true; writelock_vertex(vid);
147 }
else { writelock_vertex(in_vid); ++in; }
150 while(out != out_edges.end()) {
152 if(__builtin_expect(!processed_center && vid < out_vid,
false)) {
153 processed_center =
true; writelock_vertex(vid);
154 }
else { writelock_vertex(out_vid); ++out; }
157 if(__builtin_expect(!processed_center,
false)) { writelock_vertex(vid); }
162 const edge_list_type in_edges = graph.in_edges(vid);
163 const edge_list_type out_edges = graph.out_edges(vid);
165 bool processed_center =
false;
166 iterator in = in_edges.begin(), out = out_edges.begin();
167 while(in != in_edges.end() && out != out_edges.end()) {
170 if(__builtin_expect(!processed_center &&
171 vid < in_vid && vid < out_vid,
false)) {
172 processed_center =
true; release_vertex(vid);
173 }
else if (in_vid == out_vid) {
174 release_vertex(in_vid); ++in; ++out;
175 }
else if(in_vid < out_vid) {
176 release_vertex(in_vid); ++in;
178 release_vertex(out_vid); ++out;
182 while(in != in_edges.end()) {
184 if(__builtin_expect(!processed_center && vid < in_vid,
false)) {
185 processed_center =
true; release_vertex(vid);
186 }
else { release_vertex(in_vid); ++in; }
189 while(out != out_edges.end()) {
191 if(__builtin_expect(!processed_center && vid < out_vid,
false)) {
192 processed_center =
true; release_vertex(vid);
193 }
else { release_vertex(out_vid); ++out; }
196 if(__builtin_expect(!processed_center,
false)) { release_vertex(vid); }
203 if(center < neighbor) { writelock_vertex(center); readlock_vertex(neighbor); }
204 else { readlock_vertex(neighbor); writelock_vertex(center); }
209 release_vertex(center); release_vertex(neighbor);
215 release_vertex(old_neighbor);
216 if(!try_readlock_vertex(new_neighbor)) {
217 release_vertex(center);
218 lock_single_edge(center, new_neighbor);
227 #include <graphlab/macros_undef.hpp>