This commit is contained in:
Shautvast 2024-07-03 13:47:31 +02:00
parent 2e1200b911
commit 0b1ac32869
4 changed files with 6 additions and 10 deletions

View file

@ -9,7 +9,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
/**
* Enables multithreaded writing, while keeping CircularByteBuffer simpler (only suitable for single-threaded writing)
*/
public class MPSCBufferWriter implements AutoCloseable {
public class CircularBufferWriter implements AutoCloseable {
private static Linker linker;
private static SymbolLookup rustlib;
private static final LinkedBlockingDeque<byte[]> writeQueue = new LinkedBlockingDeque<>(); // unbounded
@ -20,8 +20,7 @@ public class MPSCBufferWriter implements AutoCloseable {
});
private final AtomicBoolean active = new AtomicBoolean(false);
public MPSCBufferWriter() {
// DatabaseInit.initializeDatabase();
public CircularBufferWriter() {
startWriteQueueListener();
}
@ -39,7 +38,7 @@ public class MPSCBufferWriter implements AutoCloseable {
arena = Arena.ofConfined();
linker = Linker.nativeLinker();
//TODO relative path, or configurable
rustlib = SymbolLookup.libraryLookup("/Users/Shautvast/dev/exceptional/rustlib/target/debug/librustlib.dylib", arena);
rustlib = SymbolLookup.libraryLookup(System.getProperty("agentlib"), arena);
MemorySegment create = rustlib.find("buffer_updated").orElseThrow();
var updateHandle = linker.downcallHandle(create, FunctionDescriptor.ofVoid(
ValueLayout.ADDRESS

View file

@ -5,7 +5,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
@SuppressWarnings("unused") // this code is called from the instrumented code
public class ExceptionLogger {
private final static ObjectMapper objectMapper = new ObjectMapper();
private final static MPSCBufferWriter bufferWriter = new MPSCBufferWriter();
private final static CircularBufferWriter bufferWriter = new CircularBufferWriter();
public static void log(Throwable throwable) {
try {

View file

@ -1,7 +1,5 @@
package com.github.shautvast.exceptional;
import org.junit.jupiter.api.Test;
import java.lang.foreign.Arena;
import static java.nio.charset.StandardCharsets.UTF_8;
@ -14,7 +12,7 @@ class MPSCBufferWriterTest {
var arena = Arena.ofConfined();
var ringbufferMemory = arena.allocate(4096);
// var buffer = new CircularByteBuffer(ringbufferMemory);
MPSCBufferWriter writer = new MPSCBufferWriter();
CircularBufferWriter writer = new CircularBufferWriter();
byte[] bytes = "cow".getBytes(UTF_8);
writer.put(bytes);
writer.put(bytes);

View file

@ -3,7 +3,6 @@ package com.github.shautvast.exceptional;
import org.junit.jupiter.api.Test;
import java.lang.foreign.MemorySegment;
import java.nio.charset.StandardCharsets;
// TODO scheduled for demolition
@ -12,7 +11,7 @@ class RingBufferTest {
@Test
void testWriteAndRead() {
// var ringBuffer = new CircularByteBuffer(MemorySegment.ofArray(new byte[16]));
var writer = new MPSCBufferWriter();
var writer = new CircularBufferWriter();
// writer.startReader(x -> System.out.println("read " + new String(x, StandardCharsets.UTF_8)));
for (int i = 0; i < 10; i++) {