ThreadIdentifier.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. namespace UnityEditor.Performance.ProfileAnalyzer
  3. {
  4. /// <summary>
  5. /// Individual Thread details
  6. /// </summary>
  7. internal struct ThreadIdentifier
  8. {
  9. /// <summary>Thread name with index combined. A profiler analyzer specific unique thread representation</summary>
  10. public string threadNameWithIndex { get; private set; }
  11. /// <summary>Thread name (may not be unique)</summary>
  12. public string name { get; private set; }
  13. /// <summary>Thread index. A 1 based id for threads with matching names. -1 indicates all threads with the same name, 0 if there is only one thread with this name</summary>
  14. public int index { get; private set; }
  15. /// <summary>Thread index id which means all threads of the same name</summary>
  16. public static int kAll = -1;
  17. /// <summary>Thread index id use when there is only one thread with this name</summary>
  18. public static int kSingle = 0;
  19. /// <summary>Initialise ThreadIdentifier</summary>
  20. /// <param name="name">The thread name</param>
  21. /// <param name="index">The thread index</param>
  22. public ThreadIdentifier(string name, int index)
  23. {
  24. this.name = name;
  25. this.index = index;
  26. if (index == kAll)
  27. threadNameWithIndex = string.Format("All:{0}", name);
  28. else
  29. threadNameWithIndex = string.Format("{0}:{1}", index, name);
  30. }
  31. /// <summary>Initialise ThreadIdentifier from another ThreadIdentifier</summary>
  32. /// <param name="threadIdentifier">The other ThreadIdentifier</param>
  33. public ThreadIdentifier(ThreadIdentifier threadIdentifier)
  34. {
  35. name = threadIdentifier.name;
  36. index = threadIdentifier.index;
  37. threadNameWithIndex = threadIdentifier.threadNameWithIndex;
  38. }
  39. /// <summary>Initialise ThreadIdentifier from a unique name</summary>
  40. /// <param name="threadNameWithIndex">The unique name string (name with index)</param>
  41. public ThreadIdentifier(string threadNameWithIndex)
  42. {
  43. this.threadNameWithIndex = threadNameWithIndex;
  44. string[] tokens = threadNameWithIndex.Split(':');
  45. if (tokens.Length >= 2)
  46. {
  47. name = tokens[1];
  48. string indexString = tokens[0];
  49. if (indexString == "All")
  50. {
  51. index = kAll;
  52. }
  53. else
  54. {
  55. int intValue;
  56. if (Int32.TryParse(tokens[0], out intValue))
  57. index = intValue;
  58. else
  59. index = kSingle;
  60. }
  61. }
  62. else
  63. {
  64. index = kSingle;
  65. name = threadNameWithIndex;
  66. }
  67. }
  68. void UpdateThreadNameWithIndex()
  69. {
  70. if (index == kAll)
  71. threadNameWithIndex = string.Format("All:{0}", name);
  72. else
  73. threadNameWithIndex = string.Format("{0}:{1}", index, name);
  74. }
  75. /// <summary>Set the name of the thread</summary>
  76. /// <param name="newName">The name (without index)</param>
  77. public void SetName(string newName)
  78. {
  79. name = newName;
  80. UpdateThreadNameWithIndex();
  81. }
  82. /// <summary>Set the index of the thread name</summary>
  83. /// <param name="newIndex">The index of the thread with the same name</param>
  84. public void SetIndex(int newIndex)
  85. {
  86. index = newIndex;
  87. UpdateThreadNameWithIndex();
  88. }
  89. /// <summary>Sets the index to indicate we want all threads (used for filtering against other ThreadIdentifiers)</summary>
  90. public void SetAll()
  91. {
  92. SetIndex(kAll);
  93. }
  94. }
  95. }