memorysegments scope

This commit is contained in:
Andrea Cavalli 2023-10-20 15:21:27 +02:00
parent 09519997dc
commit fe33243258
3 changed files with 17 additions and 15 deletions

View File

@ -4,7 +4,7 @@
<groupId>it.cavallium</groupId> <groupId>it.cavallium</groupId>
<artifactId>filequeue</artifactId> <artifactId>filequeue</artifactId>
<name>file queue project</name> <name>file queue project</name>
<version>3.1.7</version> <version>3.1.8</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<description>Light weight, high performance, simple, reliable and persistent queue</description> <description>Light weight, high performance, simple, reliable and persistent queue</description>
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>

View File

@ -60,13 +60,15 @@ public class SimpleQueueMemorySegment<T> implements SimpleQueue<T>, Closeable {
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
} }
var memorySegment = arena.allocateArray(ValueLayout.JAVA_BYTE, serialized); try (var arena = Arena.ofConfined()) {
try { var memorySegment = arena.allocateArray(ValueLayout.JAVA_BYTE, serialized);
if (!getQueueSegmentForWrite().offer(memorySegment)) { try {
expandQueueSegmentForWrite().add(memorySegment); if (!getQueueSegmentForWrite().offer(memorySegment)) {
expandQueueSegmentForWrite().add(memorySegment);
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
} }
} catch (IOException ex) {
throw new UncheckedIOException(ex);
} }
} }
@ -118,18 +120,18 @@ public class SimpleQueueMemorySegment<T> implements SimpleQueue<T>, Closeable {
@Override @Override
public T remove() { public T remove() {
MemorySegment removed; MemorySegment removed;
try { try (var arena = Arena.ofConfined()) {
removed = getQueueSegmentForRead().poll(); removed = getQueueSegmentForRead().poll(arena);
if (removed == null) { if (removed == null) {
if (popQueueSegmentForRead()) { if (popQueueSegmentForRead()) {
removed = getQueueSegmentForRead().poll(); removed = getQueueSegmentForRead().poll(arena);
} }
} }
if (removed == null) throw new NoSuchElementException();
return readObject(removed.toArray(ValueLayout.JAVA_BYTE));
} catch (IOException ex) { } catch (IOException ex) {
throw new UncheckedIOException(ex); throw new UncheckedIOException(ex);
} }
if (removed == null) throw new NoSuchElementException();
return readObject(removed.toArray(ValueLayout.JAVA_BYTE));
} }
private T readObject(byte[] byteBuffer) { private T readObject(byte[] byteBuffer) {

View File

@ -62,7 +62,7 @@ public class SimpleQueueMemorySegmentFixedSize implements Closeable {
return true; return true;
} }
public MemorySegment poll() { public MemorySegment poll(Arena arena) {
if (readPosition >= fixedSize) { if (readPosition >= fixedSize) {
return null; return null;
} }
@ -77,8 +77,8 @@ public class SimpleQueueMemorySegmentFixedSize implements Closeable {
return segment; return segment;
} }
public MemorySegment remove() { public MemorySegment remove(Arena arena) {
var polled = poll(); var polled = poll(arena);
if (polled == null) throw new NoSuchElementException(); if (polled == null) throw new NoSuchElementException();
return polled; return polled;
} }