Skip to content

Commit fc052e8

Browse files
committed
Added concurrent use of some dictinionarys to increase multithread speed and avoiding locks
1 parent 7fe8b73 commit fc052e8

10 files changed

Lines changed: 68 additions & 94 deletions

File tree

Assets/Plugins/x64/gzBase64.dll

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.

Assets/Saab/GizmoSDK/GizmoBase/Reference.cs

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using System.Runtime.InteropServices;
2323
using System;
2424
using System.Collections.Generic;
25+
using System.Collections.Concurrent;
2526

2627
namespace GizmoSDK
2728
{
@@ -180,15 +181,7 @@ static public bool AddFactory(Reference iface)
180181

181182
string typeName = iface.GetNativeTypeName();
182183

183-
lock (s_factory)
184-
{
185-
if (s_factory.ContainsKey(typeName))
186-
return false;
187-
188-
s_factory.Add(typeName, iface);
189-
190-
return true;
191-
}
184+
return s_factory.TryAdd(typeName, iface);
192185
}
193186

194187
static public bool RemoveFactory<T>() where T : Reference
@@ -197,44 +190,37 @@ static public bool RemoveFactory<T>() where T : Reference
197190
}
198191
static public bool RemoveFactory(string typeName)
199192
{
200-
lock (s_factory)
201-
{
202-
if (!s_factory.ContainsKey(typeName))
203-
return false;
193+
IReferenceFactory factory;
204194

205-
s_factory.Remove(typeName);
195+
return s_factory.TryRemove(typeName,out factory);
206196

207-
return true;
208-
}
209197
}
210198

211199
static public Reference CreateObject(IntPtr nativeReference)
212200
{
213201
if (nativeReference==IntPtr.Zero)
214202
return null;
215203

216-
lock (s_factory)
217-
{
218-
IntPtr type = Reference_getType(nativeReference);
204+
IntPtr type = Reference_getType(nativeReference);
219205

220-
IReferenceFactory factory;
206+
IReferenceFactory factory;
221207

222-
while (type != IntPtr.Zero)
223-
{
224-
string typeName = Marshal.PtrToStringUni(Reference_getTypeName(type));
208+
while (type != IntPtr.Zero)
209+
{
210+
string typeName = Marshal.PtrToStringUni(Reference_getTypeName(type));
225211

226-
if (s_factory.TryGetValue(typeName, out factory))
227-
return factory.Create(nativeReference);
212+
if (s_factory.TryGetValue(typeName, out factory))
213+
return factory.Create(nativeReference);
228214

229-
type = Reference_getParentType(type);
230-
}
231-
return new Reference(nativeReference);
215+
type = Reference_getParentType(type);
232216
}
217+
218+
return new Reference(nativeReference);
233219
}
234220

235221
private HandleRef m_reference;
236222

237-
private static Dictionary<string, IReferenceFactory> s_factory = new Dictionary<string, IReferenceFactory>();
223+
private static ConcurrentDictionary<string, IReferenceFactory> s_factory = new ConcurrentDictionary<string, IReferenceFactory>();
238224

239225
#endregion
240226
}

Assets/Saab/GizmoSDK/GizmoDistribution/gzDistribution/DistClient.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
// Who Date Description
1717
//
1818
// AMO 180301 Created file
19+
// AMO 281211 Added UseAutoProperty to enable automatic store/restore of properties
1920
//
2021
//******************************************************************************
2122

@@ -163,7 +164,7 @@ public bool UnSubscribeEvents(DistSession session, string typeName = null, Int32
163164

164165
public bool SendEvent(DistEvent e,DistSession session)
165166
{
166-
if (e.GetType().IsDefined(typeof(DistPropertyAutoStore),true))
167+
if (UseAutoProperty && e.GetType().IsDefined(typeof(DistPropertyAutoStore),true))
167168
e.StorePropertiesAndFields();
168169

169170
return DistClient_sendEvent(GetNativeReference(), e.GetNativeReference(), session.GetNativeReference());
@@ -176,13 +177,13 @@ public T SendEventAndAwaitResponse<T>(DistEvent e,DistSession session,UInt32 tim
176177

177178
public DistEvent SendEventAndAwaitResponse(DistEvent e, DistSession session, DistEvent responseEventType, UInt32 timeout=100)
178179
{
179-
if (e.GetType().IsDefined(typeof(DistPropertyAutoStore), true))
180+
if (UseAutoProperty && e.GetType().IsDefined(typeof(DistPropertyAutoStore), true))
180181
e.StorePropertiesAndFields();
181182

182183
DistEvent response = Reference.CreateObject(DistClient_sendEventAndAwaitResponse(GetNativeReference(), e.GetNativeReference(), session.GetNativeReference(), responseEventType.GetNativeReference(), timeout)) as DistEvent;
183184

184185
if(response?.IsValid() ?? false)
185-
if (response.GetType().IsDefined(typeof(DistPropertyAutoRestore), true))
186+
if (UseAutoProperty && response.GetType().IsDefined(typeof(DistPropertyAutoRestore), true))
186187
response.RestorePropertiesAndFields();
187188

188189
return response;
@@ -198,7 +199,7 @@ public DistEvent AwaitResponse(DistEvent responseEventType, UInt32 timeout = 100
198199
DistEvent response = Reference.CreateObject(DistClient_awaitResponse(GetNativeReference(), responseEventType.GetNativeReference(), timeout)) as DistEvent;
199200

200201
if (response?.IsValid() ?? false)
201-
if (response.GetType().IsDefined(typeof(DistPropertyAutoRestore), true))
202+
if (UseAutoProperty && response.GetType().IsDefined(typeof(DistPropertyAutoRestore), true))
202203
response.RestorePropertiesAndFields();
203204

204205
return response;
@@ -309,6 +310,8 @@ static public string GetDistThreadError(bool clearError=true)
309310
return Marshal.PtrToStringUni(DistClient_getDistThreadError(clearError));
310311
}
311312

313+
public bool UseAutoProperty = true;
314+
312315
#region ------------------------ Private Callbacks ---------------------------------------------
313316

314317
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
@@ -350,7 +353,7 @@ private void OnEvent_callback(IntPtr e)
350353

351354
if (@event != null)
352355
{
353-
if (@event.GetType().IsDefined(typeof(DistPropertyAutoRestore), true))
356+
if (UseAutoProperty && @event.GetType().IsDefined(typeof(DistPropertyAutoRestore), true))
354357
@event.RestorePropertiesAndFields();
355358

356359
OnEvent?.Invoke(this, @event);

Assets/Saab/GizmoSDK/GizmoDistribution/gzDistribution/DistObject.cs

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
//
1616
// Who Date Description
1717
//
18-
// AMO 180301 Created file
18+
// AMO 180301 Created file
19+
// AMO 181210 Added Concurrent reading of dictionary
1920
//
2021
//******************************************************************************
2122

2223
using System;
23-
using System.Collections;
24-
using System.Collections.Generic;
2524
using System.Runtime.InteropServices;
2625
using GizmoSDK.GizmoBase;
26+
using System.Collections.Concurrent;
2727

2828

2929
namespace GizmoSDK
@@ -35,7 +35,7 @@ public class DistObjectInstanceManager
3535
{
3636
public DistObjectInstanceManager()
3737
{
38-
instanses = new Dictionary<IntPtr, DistObject>();
38+
_instanses = new ConcurrentDictionary<IntPtr, DistObject>();
3939
}
4040

4141
public DistObject GetObject(IntPtr nativeReference)
@@ -45,51 +45,44 @@ public DistObject GetObject(IntPtr nativeReference)
4545
if (nativeReference == IntPtr.Zero)
4646
return null;
4747

48-
lock (instanses)
49-
{
50-
DistObject obj;
51-
52-
if (!instanses.TryGetValue(nativeReference, out obj))
53-
{
54-
obj = Reference.CreateObject(nativeReference) as DistObject;
48+
DistObject obj;
5549

56-
// At least we will always get a DistObject
50+
if (!_instanses.TryGetValue(nativeReference, out obj))
51+
{
52+
obj = Reference.CreateObject(nativeReference) as DistObject;
5753

58-
instanses.Add(nativeReference, obj);
59-
}
54+
// At least we will always get a DistObject
6055

61-
if( obj==null || !obj.IsValid() )
62-
{
63-
instanses[nativeReference]= obj = Reference.CreateObject(nativeReference) as DistObject;
64-
}
56+
_instanses.TryAdd(nativeReference, obj);
57+
}
6558

66-
return obj;
59+
if( obj==null || !obj.IsValid() )
60+
{
61+
// In case we lost the factory native ref
62+
_instanses[nativeReference]=obj=Reference.CreateObject(nativeReference) as DistObject;
6763
}
64+
65+
return obj;
6866
}
6967

7068
public void Clear()
7169
{
72-
lock (instanses)
70+
foreach (var key in _instanses)
7371
{
74-
foreach (var key in instanses)
75-
{
76-
key.Value.Dispose();
77-
}
78-
79-
instanses.Clear();
72+
key.Value.Dispose();
8073
}
74+
75+
_instanses.Clear();
8176
}
8277

8378
public bool DropObject(IntPtr nativeReference)
8479
{
85-
lock (instanses)
86-
{
87-
return instanses.Remove(nativeReference);
88-
}
80+
DistObject obj;
81+
return _instanses.TryRemove(nativeReference,out obj);
8982
}
9083

9184

92-
Dictionary<IntPtr, DistObject> instanses;
85+
ConcurrentDictionary<IntPtr, DistObject> _instanses;
9386
}
9487

9588
public class DistObject : Reference

Assets/Saab/GizmoSDK/GizmoDistribution/gzDistribution/DistSession.cs

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
// Who Date Description
1717
//
1818
// AMO 180301 Created file
19+
// AMO 181210 Added Concurrent reading of dictionary
1920
//
2021
//******************************************************************************
2122

2223
using System;
23-
using System.Collections;
24-
using System.Collections.Generic;
24+
using System.Collections.Concurrent;
2525
using System.Runtime.InteropServices;
2626
using GizmoSDK.GizmoBase;
2727

@@ -34,7 +34,7 @@ public class DistSessionInstanceManager
3434
{
3535
public DistSessionInstanceManager()
3636
{
37-
instanses = new Dictionary<IntPtr, DistSession>();
37+
_instanses = new ConcurrentDictionary<IntPtr, DistSession>();
3838
}
3939
public DistSession GetSession(IntPtr nativeReference)
4040
{
@@ -43,49 +43,41 @@ public DistSession GetSession(IntPtr nativeReference)
4343
if (nativeReference == IntPtr.Zero)
4444
return null;
4545

46-
lock (instanses)
47-
{
48-
DistSession sess;
49-
50-
if (!instanses.TryGetValue(nativeReference, out sess))
51-
{
52-
sess = new DistSession(nativeReference);
46+
DistSession sess;
5347

54-
instanses.Add(nativeReference, sess);
55-
}
48+
if (!_instanses.TryGetValue(nativeReference, out sess))
49+
{
50+
sess = new DistSession(nativeReference);
5651

57-
if (sess==null || !sess.IsValid())
58-
{
59-
instanses[nativeReference] = sess = new DistSession(nativeReference);
60-
}
52+
_instanses.TryAdd(nativeReference, sess);
53+
}
6154

62-
return sess;
55+
if (sess==null || !sess.IsValid())
56+
{
57+
_instanses[nativeReference] = sess = new DistSession(nativeReference);
6358
}
59+
60+
return sess;
6461
}
6562

6663
public void Clear()
6764
{
68-
lock (instanses)
65+
foreach(var key in _instanses )
6966
{
70-
foreach(var key in instanses )
71-
{
72-
key.Value.Dispose();
73-
}
74-
75-
instanses.Clear();
67+
key.Value.Dispose();
7668
}
69+
70+
_instanses.Clear();
7771
}
7872

7973
public bool DropSession(IntPtr nativeReference)
8074
{
81-
lock (instanses)
82-
{
83-
return instanses.Remove(nativeReference);
84-
}
75+
DistSession obj;
76+
return _instanses.TryRemove(nativeReference,out obj);
8577
}
8678

8779

88-
Dictionary<IntPtr, DistSession> instanses;
80+
ConcurrentDictionary<IntPtr, DistSession> _instanses;
8981
}
9082

9183
public class DistSession : Reference

0 commit comments

Comments
 (0)