Fix data races.

This commit is contained in:
IntegratedQuantum 2023-02-04 16:32:30 +01:00
parent b84332d426
commit fc52ad22ef
3 changed files with 18 additions and 10 deletions

View File

@ -101,7 +101,7 @@ pub const ClientEntityManager = struct {
fn update() void {
std.debug.assert(!mutex.tryLock()); // The mutex should be locked when calling this function.
var time = @truncate(i16, std.time.milliTimestamp());
time -%= timeDifference.difference;
time -%= timeDifference.difference.load(.Monotonic);
for(entities.items) |*ent| {
ent.update(time, lastTime);
}

View File

@ -493,13 +493,19 @@ pub const ClientItemDropManager = struct {
vel[i][2] = @bitCast(f64, std.mem.readIntBig(u64, data[42..50]));
data = data[50..];
}
self.super.mutex.lock();
defer self.super.mutex.unlock();
self.interpolation.updatePosition(@ptrCast(*[maxf64Capacity]f64, &pos), @ptrCast(*[maxf64Capacity]f64, &vel), time); // TODO: Only update the ones we actually changed.
}
pub fn updateInterpolationData(self: *ClientItemDropManager) void {
var time = @truncate(i16, std.time.milliTimestamp()) -% settings.entityLookback;
time -%= self.timeDifference.difference;
self.interpolation.updateIndexed(time, self.lastTime, &self.super.indices, 4);
time -%= self.timeDifference.difference.load(.Monotonic);
{
self.super.mutex.lock();
defer self.super.mutex.unlock();
self.interpolation.updateIndexed(time, self.lastTime, &self.super.indices, 4);
}
self.lastTime = time;
}
@ -731,7 +737,9 @@ pub const ItemDropRenderer = struct {
c.glUniform3fv(itemUniforms.ambientLight, 1, @ptrCast([*c]const f32, &ambientLight));
c.glUniformMatrix4fv(itemUniforms.viewMatrix, 1, c.GL_FALSE, @ptrCast([*c]const f32, &game.camera.viewMatrix));
c.glUniform1f(itemUniforms.sizeScale, @floatCast(f32, ItemDropManager.diameter/4.0));
const itemDrops = game.world.?.itemDrops.super;
var itemDrops = &game.world.?.itemDrops.super;
itemDrops.mutex.lock();
defer itemDrops.mutex.unlock();
for(itemDrops.indices[0..itemDrops.size]) |i| {
if(itemDrops.list.items(.itemStack)[i].item) |item| {
var pos = itemDrops.list.items(.pos)[i];

View File

@ -591,20 +591,20 @@ pub fn GenericInterpolation(comptime elements: comptime_int) type {
}
pub const TimeDifference = struct {
difference: i16 = 0,
difference: std.atomic.Atomic(i16) = std.atomic.Atomic(i16).init(0),
firstValue: bool = true,
pub fn addDataPoint(self: *TimeDifference, time: i16) void {
const currentTime = @truncate(i16, std.time.milliTimestamp());
const timeDifference = currentTime -% time;
if(self.firstValue) {
self.difference = timeDifference;
self.difference.store(timeDifference, .Monotonic);
self.firstValue = false;
}
if(timeDifference -% self.difference > 0) {
self.difference +%= 1;
} else if(timeDifference -% self.difference < 0) {
self.difference -%= 1;
if(timeDifference -% self.difference.load(.Monotonic) > 0) {
_ = @atomicRmw(i16, &self.difference.value, .Add, 1, .Monotonic);
} else if(timeDifference -% self.difference.load(.Monotonic) < 0) {
_ = @atomicRmw(i16, &self.difference.value, .Add, -1, .Monotonic);
}
}
};