Skip to content

Commit e4b6a8e

Browse files
committed
make it work on multithread for fuse
1 parent 0991984 commit e4b6a8e

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

src/main/java/DiscUtils/Core/Internal/ObjectCache.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ObjectCache() {
5757
_recent = new ArrayList<>();
5858
}
5959

60-
public V get(K key) {
60+
public synchronized V get(K key) {
6161
for (int i = 0; i < _recent.size(); ++i) {
6262
Tuple<K, V> recentEntry = _recent.get(i);
6363
if (recentEntry.getKey().equals(key)) {
@@ -77,14 +77,14 @@ public V get(K key) {
7777
return null;
7878
}
7979

80-
public V put(K key, V value) {
80+
public synchronized V put(K key, V value) {
8181
WeakReference<V> v = _entries.put(key, new WeakReference<>(value));
8282
makeMostRecent(key, value);
8383
pruneEntries();
8484
return v != null ? v.get() : null;
8585
}
8686

87-
public V remove(Object key) {
87+
public synchronized V remove(Object key) {
8888
for (int i = 0; i < _recent.size(); ++i) {
8989
if (_recent.get(i).getKey().equals(key)) {
9090
_recent.remove(i);
@@ -94,7 +94,7 @@ public V remove(Object key) {
9494
return _entries.remove(key).get();
9595
}
9696

97-
private void pruneEntries() {
97+
private synchronized void pruneEntries() {
9898
_nextPruneCount++;
9999
if (_nextPruneCount > PruneGap) {
100100
List<K> toPrune = new ArrayList<>();
@@ -110,7 +110,7 @@ private void pruneEntries() {
110110
}
111111
}
112112

113-
private void makeMostRecent(int i) {
113+
private synchronized void makeMostRecent(int i) {
114114
if (i == 0) {
115115
return;
116116
}
@@ -120,7 +120,7 @@ private void makeMostRecent(int i) {
120120
_recent.add(0, entry);
121121
}
122122

123-
private void makeMostRecent(K key, V val) {
123+
private synchronized void makeMostRecent(K key, V val) {
124124
while (_recent.size() >= MostRecentListSize) {
125125
_recent.remove(_recent.size() - 1);
126126
}

src/main/java/DiscUtils/Streams/Block/BlockCacheStream.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import java.io.IOException;
2626
import java.util.Arrays;
2727
import java.util.List;
28+
import java.util.logging.Level;
29+
30+
import vavi.util.Debug;
2831

2932
import DiscUtils.Streams.SparseStream;
3033
import DiscUtils.Streams.StreamExtent;
@@ -179,7 +182,7 @@ public List<StreamExtent> getExtentsInRange(long start, long count) {
179182
* @param count The number of bytes to read.
180183
* @return The number of bytes read.
181184
*/
182-
public int read(byte[] buffer, int offset, int count) {
185+
public synchronized int read(byte[] buffer, int offset, int count) {
183186
checkDisposed();
184187

185188
if (_position >= getLength()) {
@@ -229,6 +232,7 @@ public int read(byte[] buffer, int offset, int count) {
229232
int bytesToRead = Math.min(count - totalBytesRead, block[0].getAvailable() - offsetInNextBlock);
230233

231234
System.arraycopy(block[0].getData(), offsetInNextBlock, buffer, offset + totalBytesRead, bytesToRead);
235+
232236
offsetInNextBlock = 0;
233237
totalBytesRead += bytesToRead;
234238
_position += bytesToRead;
@@ -313,7 +317,7 @@ public void flush() {
313317
* @param origin The base location.
314318
* @return The new absolute stream position.
315319
*/
316-
public long seek(long offset, SeekOrigin origin) {
320+
public synchronized long seek(long offset, SeekOrigin origin) {
317321
checkDisposed();
318322
long effectiveOffset = offset;
319323
if (origin == SeekOrigin.Current) {
@@ -348,7 +352,7 @@ public void setLength(long value) {
348352
* @param offset The first byte to write from buffer.
349353
* @param count The number of bytes to write.
350354
*/
351-
public void write(byte[] buffer, int offset, int count) {
355+
public synchronized void write(byte[] buffer, int offset, int count) {
352356
checkDisposed();
353357

354358
_stats.setTotalWritesIn(_stats.getTotalWritesIn() + 1);

0 commit comments

Comments
 (0)