View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.pack;
12  
13  import java.util.concurrent.locks.ReentrantLock;
14  
15  import org.eclipse.jgit.storage.pack.PackConfig;
16  
17  class ThreadSafeDeltaCache extends DeltaCache {
18  	private final ReentrantLock lock;
19  
20  	ThreadSafeDeltaCache(PackConfig pc) {
21  		super(pc);
22  		lock = new ReentrantLock();
23  	}
24  
25  	@Override
26  	boolean canCache(int length, ObjectToPack./../../../../../org/eclipse/jgit/internal/storage/pack/ObjectToPack.html#ObjectToPack">ObjectToPack src, ObjectToPack res) {
27  		lock.lock();
28  		try {
29  			return super.canCache(length, src, res);
30  		} finally {
31  			lock.unlock();
32  		}
33  	}
34  
35  	@Override
36  	void credit(int reservedSize) {
37  		lock.lock();
38  		try {
39  			super.credit(reservedSize);
40  		} finally {
41  			lock.unlock();
42  		}
43  	}
44  
45  	@Override
46  	Ref cache(byte[] data, int actLen, int reservedSize) {
47  		data = resize(data, actLen);
48  		lock.lock();
49  		try {
50  			return super.cache(data, actLen, reservedSize);
51  		} finally {
52  			lock.unlock();
53  		}
54  	}
55  }