Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions eo-runtime/src/main/java/EOorg/EOeolang/Heaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,16 @@

import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.eolang.ExFailure;
import org.eolang.Phi;

/**
* Dynamic memory.
*
* @since 0.19
* @todo #4884:30min Use ReentrantLock instead of 'synchronized' in Heaps.
* We should use ReentrantLock instead of 'synchronized' to avoid potential
* deadlocks when multiple AtOnce attributes are used together.
* Moreover, 'synchronized' keyword is forbidden by qulice.
*/
@SuppressWarnings("PMD.AvoidSynchronizedStatement")
final class Heaps {

/**
Expand All @@ -37,11 +34,17 @@ final class Heaps {
@SuppressWarnings("PMD.LooseCoupling")
private final ConcurrentHashMap<Integer, byte[]> blocks;

/**
* Lock.
*/
private final Lock lock;

/**
* Ctor.
*/
private Heaps() {
this.blocks = new ConcurrentHashMap<>(0);
this.lock = new ReentrantLock();
}

/**
Expand All @@ -52,7 +55,8 @@ private Heaps() {
*/
int malloc(final Phi phi, final int size) {
final int identifier = phi.hashCode();
synchronized (this.blocks) {
this.lock.lock();
try {
if (this.blocks.containsKey(identifier)) {
throw new ExFailure(
String.format(
Expand All @@ -62,6 +66,8 @@ int malloc(final Phi phi, final int size) {
);
}
this.blocks.put(identifier, new byte[size]);
} finally {
this.lock.unlock();
}
return identifier;
}
Expand All @@ -72,7 +78,8 @@ int malloc(final Phi phi, final int size) {
* @return Size
*/
int size(final int identifier) {
synchronized (this.blocks) {
this.lock.lock();
try {
if (!this.blocks.containsKey(identifier)) {
throw new ExFailure(
String.format(
Expand All @@ -82,6 +89,8 @@ int size(final int identifier) {
);
}
return this.blocks.get(identifier).length;
} finally {
this.lock.unlock();
}
}

Expand All @@ -99,7 +108,8 @@ void resize(final int identifier, final int size) {
)
);
}
synchronized (this.blocks) {
this.lock.lock();
try {
if (!this.blocks.containsKey(identifier)) {
throw new ExFailure(
String.format(
Expand All @@ -112,6 +122,8 @@ void resize(final int identifier, final int size) {
final byte[] resized = new byte[size];
System.arraycopy(bytes, 0, resized, 0, Math.min(bytes.length, size));
this.blocks.put(identifier, resized);
} finally {
this.lock.unlock();
}
}

Expand All @@ -123,7 +135,8 @@ void resize(final int identifier, final int size) {
* @return Bytes from the block in memory
*/
byte[] read(final int identifier, final int offset, final int length) {
synchronized (this.blocks) {
this.lock.lock();
try {
if (!this.blocks.containsKey(identifier)) {
throw new ExFailure(
String.format(
Expand All @@ -144,6 +157,8 @@ byte[] read(final int identifier, final int offset, final int length) {
);
}
return Arrays.copyOfRange(bytes, offset, offset + length);
} finally {
this.lock.unlock();
}
}

Expand All @@ -154,7 +169,8 @@ byte[] read(final int identifier, final int offset, final int length) {
* @param data Data to write
*/
void write(final int identifier, final int offset, final byte[] data) {
synchronized (this.blocks) {
this.lock.lock();
try {
if (!this.blocks.containsKey(identifier)) {
throw new ExFailure(
String.format(
Expand Down Expand Up @@ -191,6 +207,8 @@ void write(final int identifier, final int offset, final byte[] data) {
);
}
this.blocks.put(identifier, result);
} finally {
this.lock.unlock();
}
}

Expand All @@ -199,7 +217,8 @@ void write(final int identifier, final int offset, final byte[] data) {
* @param identifier Identifier of pointer
*/
void free(final int identifier) {
synchronized (this.blocks) {
this.lock.lock();
try {
if (!this.blocks.containsKey(identifier)) {
throw new ExFailure(
String.format(
Expand All @@ -209,6 +228,8 @@ void free(final int identifier) {
);
}
this.blocks.remove(identifier);
} finally {
this.lock.unlock();
}
}
}
Loading