Browse Source

Merge pull request #247 from Joe4evr/lock

Replace locking on 'this'.
tags/1.0-rc
RogueException GitHub 8 years ago
parent
commit
448ae47a4d
3 changed files with 12 additions and 9 deletions
  1. +4
    -3
      src/Discord.Net.Commands/Map/CommandMap.cs
  2. +3
    -2
      src/Discord.Net.Commands/Map/CommandMapNode.cs
  3. +5
    -4
      src/Discord.Net/WebSocket/Entities/Users/SocketGlobalUser.cs

+ 4
- 3
src/Discord.Net.Commands/Map/CommandMap.cs View File

@@ -7,6 +7,7 @@ namespace Discord.Commands
internal class CommandMap internal class CommandMap
{ {
static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' }; static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };
private readonly object _lockObj = new object();


private readonly ConcurrentDictionary<string, CommandMapNode> _nodes; private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;


@@ -27,7 +28,7 @@ namespace Discord.Commands
else else
name = text.Substring(0, nextSpace); name = text.Substring(0, nextSpace);


lock (this)
lock (_lockObj)
{ {
var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x)); var nextNode = _nodes.GetOrAdd(name, x => new CommandMapNode(x));
nextNode.AddCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command); nextNode.AddCommand(nextSpace == -1 ? "" : text, nextSpace + 1, command);
@@ -46,7 +47,7 @@ namespace Discord.Commands
else else
name = text.Substring(0, nextSpace); name = text.Substring(0, nextSpace);


lock (this)
lock (_lockObj)
{ {
CommandMapNode nextNode; CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode)) if (_nodes.TryGetValue(name, out nextNode))
@@ -69,7 +70,7 @@ namespace Discord.Commands
else else
name = text.Substring(0, nextSpace); name = text.Substring(0, nextSpace);


lock (this)
lock (_lockObj)
{ {
CommandMapNode nextNode; CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode)) if (_nodes.TryGetValue(name, out nextNode))


+ 3
- 2
src/Discord.Net.Commands/Map/CommandMapNode.cs View File

@@ -8,6 +8,7 @@ namespace Discord.Commands
{ {
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes; private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
private readonly string _name; private readonly string _name;
private readonly object _lockObj = new object();
private ImmutableArray<Command> _commands; private ImmutableArray<Command> _commands;


public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0; public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0;
@@ -24,7 +25,7 @@ namespace Discord.Commands
int nextSpace = text.IndexOf(' ', index); int nextSpace = text.IndexOf(' ', index);
string name; string name;


lock (this)
lock (_lockObj)
{ {
if (text == "") if (text == "")
_commands = _commands.Add(command); _commands = _commands.Add(command);
@@ -45,7 +46,7 @@ namespace Discord.Commands
int nextSpace = text.IndexOf(' ', index); int nextSpace = text.IndexOf(' ', index);
string name; string name;


lock (this)
lock (_lockObj)
{ {
if (text == "") if (text == "")
_commands = _commands.Remove(command); _commands = _commands.Remove(command);


+ 5
- 4
src/Discord.Net/WebSocket/Entities/Users/SocketGlobalUser.cs View File

@@ -8,6 +8,7 @@ namespace Discord.WebSocket
internal class SocketGlobalUser : User, ISocketUser internal class SocketGlobalUser : User, ISocketUser
{ {
internal override bool IsAttached => true; internal override bool IsAttached => true;
private readonly object _lockObj = new object();


private ushort _references; private ushort _references;


@@ -25,13 +26,13 @@ namespace Discord.WebSocket
{ {
checked checked
{ {
lock (this)
lock (_lockObj)
_references++; _references++;
} }
} }
public void RemoveRef(DiscordSocketClient discord) public void RemoveRef(DiscordSocketClient discord)
{ {
lock (this)
lock (_lockObj)
{ {
if (--_references == 0) if (--_references == 0)
discord.RemoveUser(Id); discord.RemoveUser(Id);
@@ -40,14 +41,14 @@ namespace Discord.WebSocket


public override void Update(Model model, UpdateSource source) public override void Update(Model model, UpdateSource source)
{ {
lock (this)
lock (_lockObj)
base.Update(model, source); base.Update(model, source);
} }
public void Update(PresenceModel model, UpdateSource source) public void Update(PresenceModel model, UpdateSource source)
{ {
//Race conditions are okay here. Multiple shards racing already cant guarantee presence in order. //Race conditions are okay here. Multiple shards racing already cant guarantee presence in order.


//lock (this)
//lock (_lockObj)
//{ //{
var game = model.Game != null ? new Game(model.Game) : null; var game = model.Game != null ? new Game(model.Game) : null;
Presence = new Presence(game, model.Status); Presence = new Presence(game, model.Status);


Loading…
Cancel
Save