Revert 413372a3218d966f328d09b11f2b4966ea91c357

RwLocks don't work like that.
Fixes #330
This commit is contained in:
IntegratedQuantum 2024-04-23 22:45:56 +02:00
parent 6da2971528
commit c64ece2163
2 changed files with 16 additions and 23 deletions

View File

@ -234,13 +234,9 @@ const PrimitiveMesh = struct {
i += neighborFaces.items.len; i += neighborFaces.items.len;
} }
parent.lightingData[0].lock.lockShared();
parent.lightingData[1].lock.lockShared();
for(completeList) |*face| { for(completeList) |*face| {
face.light = getLight(parent, .{face.position.x, face.position.y, face.position.z}, face.blockAndQuad.quadIndex); face.light = getLight(parent, .{face.position.x, face.position.y, face.position.z}, face.blockAndQuad.quadIndex);
} }
parent.lightingData[1].lock.unlockShared();
parent.lightingData[0].lock.unlockShared();
self.mutex.lock(); self.mutex.lock();
const oldList = self.completeList; const oldList = self.completeList;
@ -260,7 +256,7 @@ const PrimitiveMesh = struct {
const x = (wx >> mesh.chunk.voxelSizeShift) & chunk.chunkMask; const x = (wx >> mesh.chunk.voxelSizeShift) & chunk.chunkMask;
const y = (wy >> mesh.chunk.voxelSizeShift) & chunk.chunkMask; const y = (wy >> mesh.chunk.voxelSizeShift) & chunk.chunkMask;
const z = (wz >> mesh.chunk.voxelSizeShift) & chunk.chunkMask; const z = (wz >> mesh.chunk.voxelSizeShift) & chunk.chunkMask;
return mesh.lightingData[1].getValueHoldingTheLock(x, y, z) ++ mesh.lightingData[0].getValueHoldingTheLock(x, y, z); return mesh.lightingData[1].getValue(x, y, z) ++ mesh.lightingData[0].getValue(x, y, z);
} }
fn getLightAt(parent: *ChunkMesh, x: i32, y: i32, z: i32) [6]u8 { fn getLightAt(parent: *ChunkMesh, x: i32, y: i32, z: i32) [6]u8 {
@ -272,10 +268,6 @@ const PrimitiveMesh = struct {
} }
const neighborMesh = mesh_storage.getMeshAndIncreaseRefCount(.{.wx = wx, .wy = wy, .wz = wz, .voxelSize = parent.pos.voxelSize}) orelse return .{0, 0, 0, 0, 0, 0}; const neighborMesh = mesh_storage.getMeshAndIncreaseRefCount(.{.wx = wx, .wy = wy, .wz = wz, .voxelSize = parent.pos.voxelSize}) orelse return .{0, 0, 0, 0, 0, 0};
defer neighborMesh.decreaseRefCount(); defer neighborMesh.decreaseRefCount();
neighborMesh.lightingData[0].lock.lockShared();
defer neighborMesh.lightingData[0].lock.unlockShared();
neighborMesh.lightingData[1].lock.lockShared();
defer neighborMesh.lightingData[1].lock.unlockShared();
return getValues(neighborMesh, wx, wy, wz); return getValues(neighborMesh, wx, wy, wz);
} }

View File

@ -28,7 +28,7 @@ fn extractColor(in: u32) [3]u8 {
pub const ChannelChunk = struct { pub const ChannelChunk = struct {
data: main.utils.PaletteCompressedRegion([3]u8, chunk.chunkVolume), data: main.utils.PaletteCompressedRegion([3]u8, chunk.chunkVolume),
lock: std.Thread.RwLock, mutex: std.Thread.Mutex,
ch: *chunk.Chunk, ch: *chunk.Chunk,
isSun: bool, isSun: bool,
@ -36,7 +36,7 @@ pub const ChannelChunk = struct {
memoryPoolMutex.lock(); memoryPoolMutex.lock();
const self = memoryPool.create() catch unreachable; const self = memoryPool.create() catch unreachable;
memoryPoolMutex.unlock(); memoryPoolMutex.unlock();
self.lock = .{}; self.mutex = .{};
self.ch = ch; self.ch = ch;
self.isSun = isSun; self.isSun = isSun;
self.data.init(); self.data.init();
@ -70,8 +70,9 @@ pub const ChannelChunk = struct {
entries: main.ListUnmanaged(PositionEntry), entries: main.ListUnmanaged(PositionEntry),
}; };
pub fn getValueHoldingTheLock(self: *ChannelChunk, x: i32, y: i32, z: i32) [3]u8 { pub fn getValue(self: *ChannelChunk, x: i32, y: i32, z: i32) [3]u8 {
main.utils.assertLockedShared(&self.lock); self.mutex.lock();
defer self.mutex.unlock();
const index = chunk.getIndex(x, y, z); const index = chunk.getIndex(x, y, z);
return self.data.getValue(index); return self.data.getValue(index);
} }
@ -111,7 +112,7 @@ pub const ChannelChunk = struct {
} }
} }
self.lock.lock(); self.mutex.lock();
while(lightQueue.dequeue()) |entry| { while(lightQueue.dequeue()) |entry| {
const index = chunk.getIndex(entry.x, entry.y, entry.z); const index = chunk.getIndex(entry.x, entry.y, entry.z);
const oldValue: [3]u8 = self.data.getValue(index); const oldValue: [3]u8 = self.data.getValue(index);
@ -145,7 +146,7 @@ pub const ChannelChunk = struct {
} }
} }
self.data.optimizeLayout(); self.data.optimizeLayout();
self.lock.unlock(); self.mutex.unlock();
if(mesh_storage.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| { if(mesh_storage.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| {
mesh.scheduleLightRefreshAndDecreaseRefCount(); mesh.scheduleLightRefreshAndDecreaseRefCount();
} }
@ -168,7 +169,7 @@ pub const ChannelChunk = struct {
} }
var isFirstIteration: bool = isFirstBlock; var isFirstIteration: bool = isFirstBlock;
self.lock.lock(); self.mutex.lock();
while(lightQueue.dequeue()) |entry| { while(lightQueue.dequeue()) |entry| {
const index = chunk.getIndex(entry.x, entry.y, entry.z); const index = chunk.getIndex(entry.x, entry.y, entry.z);
const oldValue: [3]u8 = self.data.getValue(index); const oldValue: [3]u8 = self.data.getValue(index);
@ -223,7 +224,7 @@ pub const ChannelChunk = struct {
lightQueue.enqueue(result); lightQueue.enqueue(result);
} }
} }
self.lock.unlock(); self.mutex.unlock();
if(mesh_storage.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| { if(mesh_storage.getMeshAndIncreaseRefCount(self.ch.pos)) |mesh| {
mesh.scheduleLightRefreshAndDecreaseRefCount(); mesh.scheduleLightRefreshAndDecreaseRefCount();
} }
@ -302,8 +303,8 @@ pub const ChannelChunk = struct {
const neighborMesh = mesh_storage.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue; const neighborMesh = mesh_storage.getNeighborAndIncreaseRefCount(self.ch.pos, self.ch.pos.voxelSize, @intCast(neighbor)) orelse continue;
defer neighborMesh.decreaseRefCount(); defer neighborMesh.decreaseRefCount();
const neighborLightChunk = neighborMesh.lightingData[@intFromBool(self.isSun)]; const neighborLightChunk = neighborMesh.lightingData[@intFromBool(self.isSun)];
neighborLightChunk.lock.lockShared(); neighborLightChunk.mutex.lock();
defer neighborLightChunk.lock.unlockShared(); defer neighborLightChunk.mutex.unlock();
const index = chunk.getIndex(x, y, z); const index = chunk.getIndex(x, y, z);
const neighborIndex = chunk.getIndex(otherX, otherY, otherZ); const neighborIndex = chunk.getIndex(otherX, otherY, otherZ);
var value: [3]u8 = neighborLightChunk.data.getValue(neighborIndex); var value: [3]u8 = neighborLightChunk.data.getValue(neighborIndex);
@ -326,12 +327,12 @@ pub const ChannelChunk = struct {
pub fn propagateLightsDestructive(self: *ChannelChunk, lights: []const [3]u8) void { pub fn propagateLightsDestructive(self: *ChannelChunk, lights: []const [3]u8) void {
var lightQueue = main.utils.CircularBufferQueue(Entry).init(main.stackAllocator, 1 << 12); var lightQueue = main.utils.CircularBufferQueue(Entry).init(main.stackAllocator, 1 << 12);
defer lightQueue.deinit(); defer lightQueue.deinit();
self.lock.lockShared(); self.mutex.lock();
for(lights) |pos| { for(lights) |pos| {
const index = chunk.getIndex(pos[0], pos[1], pos[2]); const index = chunk.getIndex(pos[0], pos[1], pos[2]);
lightQueue.enqueue(.{.x = @intCast(pos[0]), .y = @intCast(pos[1]), .z = @intCast(pos[2]), .value = self.data.getValue(index), .sourceDir = 6, .activeValue = 0b111}); lightQueue.enqueue(.{.x = @intCast(pos[0]), .y = @intCast(pos[1]), .z = @intCast(pos[2]), .value = self.data.getValue(index), .sourceDir = 6, .activeValue = 0b111});
} }
self.lock.unlockShared(); self.mutex.unlock();
var constructiveEntries: main.ListUnmanaged(ChunkEntries) = .{}; var constructiveEntries: main.ListUnmanaged(ChunkEntries) = .{};
defer constructiveEntries.deinit(main.stackAllocator); defer constructiveEntries.deinit(main.stackAllocator);
constructiveEntries.append(main.stackAllocator, .{ constructiveEntries.append(main.stackAllocator, .{
@ -344,7 +345,7 @@ pub const ChannelChunk = struct {
var entryList = entries.entries; var entryList = entries.entries;
defer entryList.deinit(main.stackAllocator); defer entryList.deinit(main.stackAllocator);
const channelChunk = if(mesh) |_mesh| _mesh.lightingData[@intFromBool(self.isSun)] else self; const channelChunk = if(mesh) |_mesh| _mesh.lightingData[@intFromBool(self.isSun)] else self;
channelChunk.lock.lockShared(); channelChunk.mutex.lock();
for(entryList.items) |entry| { for(entryList.items) |entry| {
const index = chunk.getIndex(entry.x, entry.y, entry.z); const index = chunk.getIndex(entry.x, entry.y, entry.z);
const value = channelChunk.data.getValue(index); const value = channelChunk.data.getValue(index);
@ -352,7 +353,7 @@ pub const ChannelChunk = struct {
channelChunk.data.setValue(index, .{0, 0, 0}); channelChunk.data.setValue(index, .{0, 0, 0});
lightQueue.enqueue(.{.x = entry.x, .y = entry.y, .z = entry.z, .value = value, .sourceDir = 6, .activeValue = 0b111}); lightQueue.enqueue(.{.x = entry.x, .y = entry.y, .z = entry.z, .value = value, .sourceDir = 6, .activeValue = 0b111});
} }
channelChunk.lock.unlockShared(); channelChunk.mutex.unlock();
channelChunk.propagateDirect(&lightQueue); channelChunk.propagateDirect(&lightQueue);
} }
} }