Skip to content
Draft
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions src/block_entity.zig
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ pub const BlockEntityTypes = struct { // MARK: BlockEntityTypes

fn onInventoryUpdateCallback(source: main.items.Inventory.Source) void {
const pos = source.blockInventory;
const simChunk = main.server.world.?.getSimulationChunkAndIncreaseRefCount(pos[0], pos[1], pos[2]) orelse return;
defer simChunk.decreaseRefCount();
const simChunk = main.server.world.?.getSimulationChunk(pos[0], pos[1], pos[2]) orelse return;
const ch = simChunk.getChunk() orelse return;
ch.mutex.lock();
defer ch.mutex.unlock();
Expand Down
32 changes: 8 additions & 24 deletions src/chunk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,8 @@ pub const ServerChunk = struct { // MARK: ServerChunk
shouldStoreNeighbors: bool = false,

mutex: main.utils.Mutex = .{},
refCount: std.atomic.Value(u16),

pub fn initAndIncreaseRefCount(pos: ChunkPosition) *ServerChunk {
pub fn init(pos: ChunkPosition) *ServerChunk {
const self = serverPool.create();
std.debug.assert((pos.voxelSize - 1 & pos.voxelSize) == 0);
std.debug.assert(@mod(pos.wx, pos.voxelSize) == 0 and @mod(pos.wy, pos.voxelSize) == 0 and @mod(pos.wz, pos.voxelSize) == 0);
Expand All @@ -483,14 +482,12 @@ pub const ServerChunk = struct { // MARK: ServerChunk
.blockPosToEntityDataMap = .{},
.blockPosToEntityDataMapMutex = .{},
},
.refCount = .init(1),
};
self.super.data.init();
return self;
}

pub fn deinit(self: *ServerChunk) void {
std.debug.assert(self.refCount.raw == 0);
pub fn privateDeinit(self: *ServerChunk) void {
const oldContext = main.sync.threadContext;
defer main.sync.threadContext = oldContext;
main.sync.threadContext = .chunkDeiniting;
Expand All @@ -502,25 +499,14 @@ pub const ServerChunk = struct { // MARK: ServerChunk
serverPool.destroy(@alignCast(self));
}

pub fn deferredDeinit(self: *ServerChunk) void {
main.heap.GarbageCollection.deferredFree(.{.ptr = self, .freeFunction = main.meta.castFunctionSelfToAnyopaque(privateDeinit)});
}
pub fn setChanged(self: *ServerChunk) void {
self.mutex.assertLocked();
if (!self.wasChanged) {
self.wasChanged = true;
self.increaseRefCount();
main.server.world.?.queueChunkUpdateAndDecreaseRefCount(self);
}
}

pub fn increaseRefCount(self: *ServerChunk) void {
const prevVal = self.refCount.fetchAdd(1, .monotonic);
std.debug.assert(prevVal != 0);
}

pub fn decreaseRefCount(self: *ServerChunk) void {
const prevVal = self.refCount.fetchSub(1, .monotonic);
std.debug.assert(prevVal != 0);
if (prevVal == 1) {
self.deinit();
main.server.world.?.queueChunkUpdate(self);
}
}

Expand Down Expand Up @@ -690,13 +676,12 @@ pub const ServerChunk = struct { // MARK: ServerChunk
var dz: i32 = -@as(i32, chunkSize);
while (dz <= chunkSize) : (dz += chunkSize) {
if (dx == 0 and dy == 0 and dz == 0) continue;
const ch = main.server.world.?.getOrGenerateChunkAndIncreaseRefCount(.{
const ch = main.server.world.?.getOrGenerateChunk(.{
.wx = self.super.pos.wx +% dx,
.wy = self.super.pos.wy +% dy,
.wz = self.super.pos.wz +% dz,
.voxelSize = 1,
});
defer ch.decreaseRefCount();
ch.mutex.lock();
defer ch.mutex.unlock();
if (!ch.wasStored) {
Expand Down Expand Up @@ -747,8 +732,7 @@ pub const ServerChunk = struct { // MARK: ServerChunk
nextPos.wy &= ~@as(i32, pos.voxelSize*chunkSize);
nextPos.wz &= ~@as(i32, pos.voxelSize*chunkSize);
nextPos.voxelSize *= 2;
const nextHigherLod = world.getOrGenerateChunkAndIncreaseRefCount(nextPos);
defer nextHigherLod.decreaseRefCount();
const nextHigherLod = world.getOrGenerateChunk(nextPos);
nextHigherLod.updateFromLowerResolution(self);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/itemdrop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ pub const ItemDropManager = struct { // MARK: ItemDropManager
var ii: u32 = 0;
while (ii < self.size) {
const i = self.indices[ii];
if (self.world.?.getSimulationChunkAndIncreaseRefCount(@trunc(pos[i][0]), @trunc(pos[i][1]), @trunc(pos[i][2]))) |simChunk| {
defer simChunk.decreaseRefCount();
if (self.world.?.getSimulationChunk(@trunc(pos[i][0]), @trunc(pos[i][1]), @trunc(pos[i][2]))) |simChunk| {
if (simChunk.getChunk() != null) {
// Check collision with blocks:
updateEnt(&pos[i], &vel[i], &onGround[i], deltaTime);
Expand Down
7 changes: 3 additions & 4 deletions src/network/protocols.zig
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ pub const chunkRequest = struct { // MARK: chunkRequest
.voxelSize = @as(u31, 1) << voxelSizeShift,
};
conn.user.?.increaseRefCount();
main.server.world.?.queueChunkAndDecreaseRefCount(request, conn.user.?);
main.server.world.?.queueChunk(request, conn.user.?);
}
}
pub fn sendRequest(conn: *Connection, requests: []chunk.ChunkPosition, basePosition: Vec3i, renderDistance: u16) void {
Expand Down Expand Up @@ -839,7 +839,7 @@ pub const lightMapRequest = struct { // MARK: lightMapRequest
};
if (conn.user) |user| {
user.increaseRefCount();
main.server.world.?.queueLightMapAndDecreaseRefCount(request, user);
main.server.world.?.queueLightMap(request, user);
}
}
}
Expand Down Expand Up @@ -1006,8 +1006,7 @@ pub const blockEntityUpdate = struct { // MARK: blockEntityUpdate
fn serverReceive(_: *Connection, reader: *utils.BinaryReader) !void {
const pos = try reader.readVec(Vec3i);
const blockType = try reader.readInt(u16);
const simChunk = main.server.world.?.getSimulationChunkAndIncreaseRefCount(pos[0], pos[1], pos[2]) orelse return;
defer simChunk.decreaseRefCount();
const simChunk = main.server.world.?.getSimulationChunk(pos[0], pos[1], pos[2]) orelse return;
const ch = simChunk.chunk.load(.monotonic) orelse return;
ch.mutex.lock();
defer ch.mutex.unlock();
Expand Down
26 changes: 5 additions & 21 deletions src/server/SimulationChunk.zig
Original file line number Diff line number Diff line change
Expand Up @@ -8,48 +8,32 @@ const BlockUpdateSystem = main.server.BlockUpdateSystem;
const SimulationChunk = @This();

chunk: std.atomic.Value(?*ServerChunk) = .init(null),
refCount: std.atomic.Value(u32),
pos: ChunkPosition,
blockUpdateSystem: BlockUpdateSystem,

pub fn initAndIncreaseRefCount(pos: ChunkPosition) *SimulationChunk {
pub fn init(pos: ChunkPosition) *SimulationChunk {
const self = main.globalAllocator.create(SimulationChunk);
self.* = .{
.refCount = .init(1),
.pos = pos,
.blockUpdateSystem = .init(),
};
return self;
}

fn deinit(self: *SimulationChunk) void {
std.debug.assert(self.refCount.load(.monotonic) == 0);
fn privateDeinit(self: *SimulationChunk) void {
self.blockUpdateSystem.deinit();
if (self.chunk.raw) |ch| ch.decreaseRefCount();
main.globalAllocator.destroy(self);
}

pub fn increaseRefCount(self: *SimulationChunk) void {
const prevVal = self.refCount.fetchAdd(1, .monotonic);
std.debug.assert(prevVal != 0);
}

pub fn decreaseRefCount(self: *SimulationChunk) void {
const prevVal = self.refCount.fetchSub(1, .monotonic);
std.debug.assert(prevVal != 0);
if (prevVal == 2) {
main.server.world_zig.ChunkManager.tryRemoveSimulationChunk(self);
}
if (prevVal == 1) {
self.deinit();
}
pub fn deferredDeinit(self: *SimulationChunk) void {
main.heap.GarbageCollection.deferredFree(.{.ptr = self, .freeFunction = main.meta.castFunctionSelfToAnyopaquep(privateDeinit)});
}

pub fn getChunk(self: *SimulationChunk) ?*ServerChunk {
return self.chunk.load(.acquire);
}

pub fn setChunkAndDecreaseRefCount(self: *SimulationChunk, ch: *ServerChunk) void {
pub fn setChunk(self: *SimulationChunk, ch: *ServerChunk) void {
std.debug.assert(self.chunk.swap(ch, .release) == null);
}

Expand Down
3 changes: 1 addition & 2 deletions src/server/server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,6 @@ pub const User = struct { // MARK: User
while (z != lastBoxEnd[2]) : (z +%= chunk.chunkSize) {
const inZDistance = z -% newBoxStart[2] >= 0 and z -% newBoxEnd[2] < 0;
if (!inXDistance or !inYDistance or !inZDistance) {
self.loadedChunks[simArrIndex(x)][simArrIndex(y)][simArrIndex(z)].decreaseRefCount();
self.loadedChunks[simArrIndex(x)][simArrIndex(y)][simArrIndex(z)] = undefined;
}
}
Expand All @@ -355,7 +354,7 @@ pub const User = struct { // MARK: User
while (z != newBoxEnd[2]) : (z +%= chunk.chunkSize) {
const inZDistance = z -% lastBoxStart[2] >= 0 and z -% lastBoxEnd[2] < 0;
if (!inXDistance or !inYDistance or !inZDistance) {
self.loadedChunks[simArrIndex(x)][simArrIndex(y)][simArrIndex(z)] = world_zig.ChunkManager.getOrGenerateSimulationChunkAndIncreaseRefCount(.{.wx = x, .wy = y, .wz = z, .voxelSize = 1});
self.loadedChunks[simArrIndex(x)][simArrIndex(y)][simArrIndex(z)] = world_zig.ChunkManager.getOrGenerateSimulationChunk(.{.wx = x, .wy = y, .wz = z, .voxelSize = 1});
}
}
}
Expand Down
Loading
Loading