private MyStat GetStat(string name) { MyStat result; using (m_lock.AcquireSharedUsing()) { if (m_stats.TryGetValue(name, out result)) { return(result); } } using (m_lock.AcquireExclusiveUsing()) { // Racing condition, someone can faster insert this value if (m_stats.TryGetValue(name, out result)) { return(result); } else { result = new MyStat(); m_stats[name] = result; return(result); } } }
internal MyStatToken(MyGameTimer timer, MyStat stat) { m_timer = timer; m_startTime = timer.Elapsed; m_stat = stat; }
private void AppendStat(StringBuilder text, string statKey, MyStat stat) { MyStat.Value sum, min, max, last; int count, decimals; MyStatTypeEnum type; MyTimeSpan inactivity; stat.ReadAndClear(m_timer.Elapsed, out sum, out count, out min, out max, out last, out type, out decimals, out inactivity); if (inactivity > RequiredInactivity(type)) { Remove(statKey); return; } string drawText = stat.DrawText ?? statKey; bool isLong = (type & MyStatTypeEnum.LongFlag) == MyStatTypeEnum.LongFlag; float avg = (float)((isLong ? (double)sum.AsLong : (double)sum.AsFloat) / count); m_format.NumberDecimalDigits = decimals; m_format.NumberGroupSeparator = decimals == 0 ? "," : String.Empty; bool isFormatString = (type & MyStatTypeEnum.FormatFlag) == MyStatTypeEnum.FormatFlag; switch (type & ~MyStatTypeEnum.AllFlags) { case MyStatTypeEnum.Avg: AppendStatLine(text, drawText, avg, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); break; case MyStatTypeEnum.Counter: AppendStatLine(text, drawText, count, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); break; case MyStatTypeEnum.CurrentValue: if (isLong) { AppendStatLine(text, drawText, last.AsLong, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } else { AppendStatLine(text, drawText, last.AsFloat, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } break; case MyStatTypeEnum.Max: if (isLong) { AppendStatLine(text, drawText, max.AsLong, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } else { AppendStatLine(text, drawText, max.AsFloat, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } break; case MyStatTypeEnum.Min: if (isLong) { AppendStatLine(text, drawText, min.AsLong, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } else { AppendStatLine(text, drawText, min.AsFloat, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } break; case MyStatTypeEnum.MinMax: if (isLong) { AppendStatLine(text, drawText, min.AsLong, max.AsLong, 0, m_format, isFormatString ? null : "{0}: {1} / {2}"); } else { AppendStatLine(text, drawText, min.AsFloat, max.AsFloat, 0, m_format, isFormatString ? null : "{0}: {1} / {2}"); } break; case MyStatTypeEnum.MinMaxAvg: if (isLong) { AppendStatLine(text, drawText, min.AsLong, max.AsLong, avg, m_format, isFormatString ? null : "{0}: {1} / {2} / {3}"); } else { AppendStatLine(text, drawText, min.AsFloat, max.AsFloat, avg, m_format, isFormatString ? null : "{0}: {1} / {2} / {3}"); } break; case MyStatTypeEnum.Sum: if (isLong) { AppendStatLine(text, drawText, sum.AsLong, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } else { AppendStatLine(text, drawText, sum.AsFloat, 0, 0, m_format, isFormatString ? null : "{0}: {1}"); } break; case MyStatTypeEnum.CounterSum: if (isLong) { AppendStatLine(text, drawText, count, sum.AsLong, 0, m_format, isFormatString ? null : "{0}: {1} / {2}"); } else { AppendStatLine(text, drawText, count, sum.AsFloat, 0, m_format, isFormatString ? null : "{0}: {1} / {2}"); } break; } }