-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
WorkSync.java
196 lines (180 loc) · 5.81 KB
/
WorkSync.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.concurrent;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;
/**
* Turns multi-threaded unary work into single-threaded stack work.
* <p>
* The technique used here is inspired in part both by the Flat Combining
* concept from Hendler, Incze, Shavit & Tzafrir, and in part by the
* wait-free linked queue design by Vyukov.
* </p>
* <p>
* In a sense, this turns many small, presumably concurrent, pieces of work
* into fewer, larger batches of work, that is then applied to the material
* under synchronisation.
* </p>
* <p>
* Obviously this only makes sense for work that a) can be combined, and b)
* where the performance improvements from batching effects is large enough
* to overcome the overhead of collecting and batching up the work units.
* </p>
* @see Work
*/
public class WorkSync<Material, W extends Work<Material,W>>
{
private final Material material;
private final AtomicReference<WorkUnit<Material,W>> stack;
private final WorkUnit<Material,W> stackEnd;
private final ReentrantLock lock;
/**
* Create a new WorkSync that will synchronize the application of work to
* the given material.
* @param material The material we want to apply work to, in a thread-safe
* way.
*/
public WorkSync( Material material )
{
this.material = material;
this.stackEnd = new WorkUnit<>( null );
this.stack = new AtomicReference<>( stackEnd );
this.lock = new ReentrantLock();
}
/**
* Apply the given work to the material in a thread-safe way, possibly by
* combining it with other work.
* @param work The work to be done.
*/
public void apply( W work )
{
// Schedule our work on the stack.
WorkUnit<Material,W> unit = new WorkUnit<>( work );
unit.next = stack.getAndSet( unit ); // benign race, see reverse()
// Try grabbing the lock to do all the work, until our work unit
// has been completed.
boolean wasInterrupted = false;
int tryCount = 0;
do
{
tryCount++;
try
{
if ( tryLock( tryCount, unit ) )
{
try
{
doSynchronizedWork();
}
finally
{
unlock();
}
}
}
catch ( InterruptedException e )
{
// We can't stop now, because our work has already been
// scheduled. So instead we're just going to reset the
// interruption status when we're done.
wasInterrupted = true;
}
}
while ( !unit.done );
if ( wasInterrupted )
{
Thread.currentThread().interrupt();
}
}
private boolean tryLock( int tryCount, WorkUnit<Material,W> unit ) throws InterruptedException
{
return lock.tryLock( tryCount < 10? 0 : 10, TimeUnit.MILLISECONDS );
}
private void unlock()
{
lock.unlock();
}
private void doSynchronizedWork()
{
WorkUnit<Material,W> batch = reverse( stack.getAndSet( stackEnd ) );
W combinedWork = combine( batch );
if ( combinedWork != null )
{
combinedWork.apply( material );
}
markAsDone( batch );
}
private WorkUnit<Material,W> reverse( WorkUnit<Material,W> batch )
{
WorkUnit<Material,W> result = stackEnd;
while ( batch != stackEnd )
{
WorkUnit<Material,W> tmp = batch.next;
while ( tmp == null )
{
// We may see 'null' via race, as work units are put on the
// stack before their 'next' pointers are updated. We just spin
// until we observe their volatile write to 'next'.
Thread.yield();
tmp = batch.next;
}
batch.next = result;
result = batch;
batch = tmp;
}
return result;
}
private W combine( WorkUnit<Material,W> batch )
{
W result = null;
while ( batch != stackEnd )
{
if ( result == null )
{
result = batch.work;
}
else
{
result = result.combine( batch.work );
}
batch = batch.next;
}
return result;
}
private void markAsDone( WorkUnit<Material,W> batch )
{
while ( batch != stackEnd )
{
batch.done = true;
batch = batch.next;
}
}
private static class WorkUnit<Material, W extends Work<Material,W>>
{
final W work;
volatile WorkUnit<Material,W> next;
volatile boolean done;
private WorkUnit( W work )
{
this.work = work;
}
}
}