Skip to content

Commit fe7f15e

Browse files
committed
deferring fix
1 parent 5239466 commit fe7f15e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/World.Deferred.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public void BeginDeferred()
1818
if (IsMerging)
1919
return;
2020

21-
_worldState.Locks += 1;
22-
EcsAssert.Assert(_worldState.Locks > 0, "");
21+
var locks = _worldState.Begin();
22+
EcsAssert.Assert(locks > 0, "");
2323
}
2424

2525

@@ -28,14 +28,14 @@ public void EndDeferred()
2828
if (IsMerging)
2929
return;
3030

31-
_worldState.Locks -= 1;
32-
EcsAssert.Assert(_worldState.Locks >= 0, "");
31+
var locks = _worldState.End();
32+
EcsAssert.Assert(locks >= 0, "");
3333

34-
if (_worldState.Locks == 0 && _operations.Count > 0)
34+
if (locks == 0 && _operations.Count > 0)
3535
{
36-
_worldState.Locks = -1;
36+
_worldState.Lock();
3737
Merge();
38-
_worldState.Locks = 0;
38+
_worldState.Unlock();
3939
}
4040
}
4141

@@ -163,6 +163,11 @@ private void Merge()
163163
struct WorldState
164164
{
165165
public int Locks;
166+
167+
public int Lock() => Locks = -1;
168+
public int Unlock() => Locks = 0;
169+
public int Begin() => Interlocked.Increment(ref Locks);
170+
public int End() => Interlocked.Decrement(ref Locks);
166171
}
167172

168173
struct DeferredOp

0 commit comments

Comments
 (0)