fix race condition issues - WorldEvent and others

This commit is contained in:
Robert Paciorek 2024-03-06 12:18:44 +00:00
parent c909d07462
commit cf41c0175a
3 changed files with 22 additions and 14 deletions

View File

@ -3,16 +3,19 @@ using sodoffmmo.Data;
namespace sodoffmmo.Core; namespace sodoffmmo.Core;
public class GauntletRoom : Room { public class GauntletRoom : Room {
static object NextRoomLock = new object();
static GauntletRoom? NextRoom = null; static GauntletRoom? NextRoom = null;
public static GauntletRoom Get() { public static GauntletRoom Get() {
if (NextRoom != null && NextRoom.ClientsCount == 1) { lock(NextRoomLock) {
var ret = NextRoom!; if (NextRoom != null && NextRoom.ClientsCount == 1) {
NextRoom = null; var ret = NextRoom!;
return ret; NextRoom = null;
} else { return ret;
NextRoom = new GauntletRoom(); } else {
return NextRoom!; NextRoom = new GauntletRoom();
return NextRoom!;
}
} }
} }

View File

@ -4,6 +4,7 @@ using sodoffmmo.Data;
namespace sodoffmmo.Core; namespace sodoffmmo.Core;
public class Room { public class Room {
public static int MaxId { get; private set; } = 2; public static int MaxId { get; private set; } = 2;
static object RoomsListLock = new object();
protected static Dictionary<string, Room> rooms = new(); protected static Dictionary<string, Room> rooms = new();
List<Client> clients = new(); List<Client> clients = new();
@ -70,9 +71,11 @@ public class Room {
public static Room Get(string name) => rooms[name]; public static Room Get(string name) => rooms[name];
public static Room GetOrAdd(string name) { public static Room GetOrAdd(string name) {
if (!Room.Exists(name)) lock(RoomsListLock) {
return new Room(name); if (!Room.Exists(name))
return rooms[name]; return new Room(name);
return rooms[name];
}
} }
public static Room[] AllRooms() { public static Room[] AllRooms() {

View File

@ -10,15 +10,17 @@ class WorldEvent {
NotActive NotActive
} }
private static WorldEvent? _instance = null; private static WorldEvent? _instance = null;
private object EventLock = new object(); private static object EventLock = new object();
private Random random = new Random(); private Random random = new Random();
private System.Timers.Timer? timer = null; private System.Timers.Timer? timer = null;
public static WorldEvent Get() { public static WorldEvent Get() {
if (_instance == null) { lock(EventLock) {
_instance = new WorldEvent(); if (_instance == null) {
_instance = new WorldEvent();
}
return _instance;
} }
return _instance;
} }
private WorldEvent() { private WorldEvent() {