Skip to content

Commit 64cdf80

Browse files
committed
Removing mutex lock from notification registry instead using concurrentDictionary
1 parent 8f0879c commit 64cdf80

File tree

1 file changed

+12
-14
lines changed

1 file changed

+12
-14
lines changed

OptimizelySDK/Notifications/NotificationCenterRegistry.cs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616

1717
using OptimizelySDK.Logger;
18+
using System.Collections.Concurrent;
1819
using System.Collections.Generic;
1920

2021
namespace OptimizelySDK.Notifications
@@ -23,8 +24,7 @@ internal static class NotificationCenterRegistry
2324
{
2425
private static readonly object _mutex = new object();
2526

26-
private static Dictionary<string, NotificationCenter> _notificationCenters =
27-
new Dictionary<string, NotificationCenter>();
27+
private static ConcurrentDictionary<string, NotificationCenter> _notificationCenters = new ConcurrentDictionary<string, NotificationCenter>();
2828

2929
/// <summary>
3030
/// Thread-safe access to the NotificationCenter
@@ -40,18 +40,16 @@ public static NotificationCenter GetNotificationCenter(string sdkKey, ILogger lo
4040
return default;
4141
}
4242

43-
NotificationCenter notificationCenter;
44-
lock (_mutex)
43+
NotificationCenter notificationCenter = null;
44+
45+
if (_notificationCenters.ContainsKey(sdkKey))
4546
{
46-
if (_notificationCenters.ContainsKey(sdkKey))
47-
{
48-
notificationCenter = _notificationCenters[sdkKey];
49-
}
50-
else
51-
{
52-
notificationCenter = new NotificationCenter(logger);
53-
_notificationCenters[sdkKey] = notificationCenter;
54-
}
47+
notificationCenter = _notificationCenters[sdkKey];
48+
}
49+
else
50+
{
51+
notificationCenter = new NotificationCenter(logger);
52+
_notificationCenters[sdkKey] = notificationCenter;
5553
}
5654

5755
return notificationCenter;
@@ -74,7 +72,7 @@ public static void RemoveNotificationCenter(string sdkKey)
7472
out NotificationCenter notificationCenter))
7573
{
7674
notificationCenter.ClearAllNotifications();
77-
_notificationCenters.Remove(sdkKey);
75+
_notificationCenters.TryRemove(sdkKey, out NotificationCenter obj);
7876
}
7977
}
8078
}

0 commit comments

Comments
 (0)