Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace locking on 'this'. #247

Merged
merged 1 commit into from
Aug 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/Discord.Net.Commands/Map/CommandMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Discord.Commands
internal class CommandMap
{
static readonly char[] _whitespaceChars = new char[] { ' ', '\r', '\n' };
private readonly object _lockObj = new object();

private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;

Expand All @@ -27,7 +28,7 @@ public void AddCommand(Command command)
else
name = text.Substring(0, nextSpace);

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

lock (this)
lock (_lockObj)
{
CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode))
Expand All @@ -69,7 +70,7 @@ public IEnumerable<Command> GetCommands(string text)
else
name = text.Substring(0, nextSpace);

lock (this)
lock (_lockObj)
{
CommandMapNode nextNode;
if (_nodes.TryGetValue(name, out nextNode))
Expand Down
5 changes: 3 additions & 2 deletions src/Discord.Net.Commands/Map/CommandMapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ internal class CommandMapNode
{
private readonly ConcurrentDictionary<string, CommandMapNode> _nodes;
private readonly string _name;
private readonly object _lockObj = new object();
private ImmutableArray<Command> _commands;

public bool IsEmpty => _commands.Length == 0 && _nodes.Count == 0;
Expand All @@ -24,7 +25,7 @@ public void AddCommand(string text, int index, Command command)
int nextSpace = text.IndexOf(' ', index);
string name;

lock (this)
lock (_lockObj)
{
if (text == "")
_commands = _commands.Add(command);
Expand All @@ -45,7 +46,7 @@ public void RemoveCommand(string text, int index, Command command)
int nextSpace = text.IndexOf(' ', index);
string name;

lock (this)
lock (_lockObj)
{
if (text == "")
_commands = _commands.Remove(command);
Expand Down
9 changes: 5 additions & 4 deletions src/Discord.Net/WebSocket/Entities/Users/SocketGlobalUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Discord.WebSocket
internal class SocketGlobalUser : User, ISocketUser
{
internal override bool IsAttached => true;
private readonly object _lockObj = new object();

private ushort _references;

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

public override void Update(Model model, UpdateSource source)
{
lock (this)
lock (_lockObj)
base.Update(model, source);
}
public void Update(PresenceModel model, UpdateSource source)
{
//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;
Presence = new Presence(game, model.Status);
Expand Down