private void Session_TurnBegun(object sender, GameLogic.TurnBegunEventArgs e) { // Since this is the only code that updates this array and // we ASSUME that session is handled correctly elsewhere... // so no race condition on the event trigger... // ...then we should not need a lock here. // (Importantly, reference assignment is atomic in .Net) int i = turnCacheIndex + 1 % turnCache.Length; turnCache[i] = new TurnCache(e, session.CurrentSlot); turnCacheIndex = i; }
// returns (0,-1) if the game has not started yet. // returns (t,-1) if piece is out of range. public (int turn, int slot) GetPieceInfo(int piece, out PieceInfo pieceInfo) { pieceInfo = null; // must grab a local reference to our cache object to be safe: TurnCache turn = CurrentTurn; if (turn == null) // game not started yet. { return(0, -1); } if (unchecked ((uint)piece >= (uint)session.PieceCount)) { return(turn.Turn, -1); } pieceInfo = turn.pieces[piece]; return(turn.Turn, turn.Slot); }