UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
ProcessTimer.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "Misc/ScopeLock.h"
6
7namespace BuildPatchServices
8{
14 template<typename FCyclesProvider, bool bThreadSafe = true>
16 {
17 public:
22 : ThreadLock()
23 , StartCycles(0)
24 , Cycles(0)
25 , bIsRunning(false)
26 , bIsPaused(false)
27 {
28 }
29
34 double GetSeconds()
35 {
36 if (bThreadSafe)
37 {
38 ThreadLock.Lock();
39 }
40 double Seconds = FCyclesProvider::CyclesToSeconds(Cycles);
41 if (bIsRunning && !bIsPaused)
42 {
43 Seconds += FCyclesProvider::CyclesToSeconds(FCyclesProvider::GetCycles() - StartCycles);
44 }
45 if (bThreadSafe)
46 {
47 ThreadLock.Unlock();
48 }
49 return Seconds;
50 }
51
55 void Start()
56 {
57 if (bThreadSafe)
58 {
59 ThreadLock.Lock();
60 }
61 if (!bIsRunning)
62 {
63 bIsRunning = true;
64 if (!bIsPaused)
65 {
66 StartCycles = FCyclesProvider::GetCycles();
67 }
68 }
69 if (bThreadSafe)
70 {
71 ThreadLock.Unlock();
72 }
73 }
74
78 void Stop()
79 {
80 if (bThreadSafe)
81 {
82 ThreadLock.Lock();
83 }
84 if (bIsRunning)
85 {
86 bIsRunning = false;
87 if (!bIsPaused)
88 {
89 Cycles += FCyclesProvider::GetCycles() - StartCycles;
90 }
91 }
92 if (bThreadSafe)
93 {
94 ThreadLock.Unlock();
95 }
96 }
97
102 void SetPause(bool bPause)
103 {
104 if (bThreadSafe)
105 {
106 ThreadLock.Lock();
107 }
108 if (bIsPaused != bPause)
109 {
110 bIsPaused = bPause;
111 if (bIsPaused)
112 {
113 if (bIsRunning)
114 {
115 Cycles += FCyclesProvider::GetCycles() - StartCycles;
116 StartCycles = 0;
117 }
118 }
119 else
120 {
121 if (bIsRunning)
122 {
123 StartCycles = FCyclesProvider::GetCycles();
124 }
125 }
126 }
127 if (bThreadSafe)
128 {
129 ThreadLock.Unlock();
130 }
131 }
132
133 void Reset()
134 {
135 if (bThreadSafe)
136 {
137 ThreadLock.Lock();
138 }
139 StartCycles = 0;
140 Cycles = 0;
141 bIsRunning = false;
142 bIsPaused = false;
143 if (bThreadSafe)
144 {
145 ThreadLock.Unlock();
146 }
147 }
148
149 private:
150 // Thread lock for multi-threaded use.
151 FCriticalSection ThreadLock;
152 // The cycle count when we started timing.
153 uint64 StartCycles;
154 // The total accumulated cycles between Start and Stop calls.
155 uint64 Cycles;
156 // Whether the timer is currently running.
157 bool bIsRunning;
158 // Whether the timer is in pause state.
159 bool bIsPaused;
160 };
162}
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
UE::FPlatformRecursiveMutex FCriticalSection
Definition CriticalSection.h:53
Definition ProcessTimer.h:16
void Stop()
Definition ProcessTimer.h:78
void Start()
Definition ProcessTimer.h:55
double GetSeconds()
Definition ProcessTimer.h:34
TProcessTimer()
Definition ProcessTimer.h:21
void SetPause(bool bPause)
Definition ProcessTimer.h:102
void Reset()
Definition ProcessTimer.h:133
TProcessTimer< class FStatsCollector > FProcessTimer
Definition ProcessTimer.h:161
Definition BuildPatchFileConstructor.h:28
@ false
Definition radaudio_common.h:23