Skip to content

Commit 7b5e385

Browse files
authored
Add locks to unique IDs
1 parent 7841bc9 commit 7b5e385

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

Runtime/MobileStudio.cs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,7 @@ namespace MobileStudio
4242
{
4343
public class Annotations
4444
{
45-
// Maintain global UID for each Custom Activity Map.
46-
private static UInt32 globalCamView = 1;
45+
private static readonly object _locker = new object();
4746

4847
// Global state as to whether annotations are available for use.
4948
private enum AnnotationState { Active, Inactive };
@@ -232,8 +231,11 @@ public class Counter
232231
*/
233232
public Counter(string title, string name, CounterType type)
234233
{
235-
counterCount++;
236-
counter = counterCount;
234+
lock(_locker)
235+
{
236+
counterCount++;
237+
counter = counterCount;
238+
}
237239

238240
#if UNITY_ANDROID && !UNITY_EDITOR
239241
if (state == AnnotationState.Active)
@@ -285,8 +287,11 @@ public class Channel
285287
*/
286288
public Channel(string name)
287289
{
288-
channelCount++;
289-
channel = channelCount;
290+
lock(_locker)
291+
{
292+
channelCount++;
293+
channel = channelCount;
294+
}
290295

291296
#if UNITY_ANDROID && !UNITY_EDITOR
292297
if (state == AnnotationState.Active)
@@ -388,6 +393,10 @@ public interface CAMJob
388393
void stop();
389394
}
390395

396+
397+
// Maintain a unique ID for each CAM.
398+
private static UInt32 camCount = 1;
399+
391400
private UInt32 trackCount;
392401
private UInt32 jobCount;
393402
private UInt32 viewUid;
@@ -402,8 +411,11 @@ public CAM(string name)
402411
this.trackCount = 0;
403412
this.jobCount = 0;
404413

405-
// Each CAM needs a unuque ID.
406-
this.viewUid = globalCamView++;
414+
lock(_locker)
415+
{
416+
// Each CAM needs a unique ID.
417+
this.viewUid = camCount++;
418+
}
407419

408420
#if UNITY_ANDROID && !UNITY_EDITOR
409421
if (state == AnnotationState.Active)
@@ -421,7 +433,12 @@ public CAM(string name)
421433
[MethodImpl(MethodImplOptions.AggressiveInlining)]
422434
public CAMTrack createTrack(string _name)
423435
{
424-
return new CAMTrackImp(this, _name, ++trackCount);
436+
CAMTrack newTrack;
437+
lock(_locker)
438+
{
439+
newTrack = new CAMTrackImp(this, _name, ++trackCount);
440+
}
441+
return newTrack;
425442
}
426443

427444
/*
@@ -462,7 +479,12 @@ public CAMJob makeJob(string _name, Color32 color)
462479
{
463480
UInt32 intColor = colorToGatorInt(color);
464481

465-
return new CAMJobImp(parent, trackUid, parent.jobCount++, _name, intColor);
482+
CAMJob newJob;
483+
lock(_locker)
484+
{
485+
newJob = new CAMJobImp(parent, trackUid, parent.jobCount++, _name, intColor);
486+
}
487+
return newJob;
466488
}
467489

468490
/*
@@ -474,7 +496,11 @@ public CAMJob makeJob(string _name, Color32 color)
474496
[MethodImpl(MethodImplOptions.AggressiveInlining)]
475497
public void registerJob(string name, Color32 color, UInt64 startTime, UInt64 stopTime)
476498
{
477-
UInt32 jobUid = parent.jobCount++;
499+
UInt32 jobUid;
500+
lock (_locker)
501+
{
502+
jobUid = parent.jobCount++;
503+
}
478504

479505
#if UNITY_ANDROID && !UNITY_EDITOR
480506
if (state == AnnotationState.Active)

0 commit comments

Comments
 (0)