#define LLVM_SUPPORT_TIMER_H
#include "llvm/Support/DataTypes.h"
+#include "llvm/System/Mutex.h"
#include <string>
#include <vector>
#include <iosfwd>
std::string Name; // The name of this time variable
bool Started; // Has this time variable ever been started?
TimerGroup *TG; // The TimerGroup this Timer is in.
+ mutable sys::SmartMutex<true> Lock; // Mutex for the contents of this Timer.
public:
- Timer(const std::string &N);
+ explicit Timer(const std::string &N);
Timer(const std::string &N, TimerGroup &tg);
Timer(const Timer &T);
~Timer();
std::string getName() const { return Name; }
const Timer &operator=(const Timer &T) {
+ if (&T < this) {
+ T.Lock.acquire();
+ Lock.acquire();
+ } else {
+ Lock.acquire();
+ T.Lock.acquire();
+ }
+
Elapsed = T.Elapsed;
UserTime = T.UserTime;
SystemTime = T.SystemTime;
Name = T.Name;
Started = T.Started;
assert(TG == T.TG && "Can only assign timers in the same TimerGroup!");
+
+ if (&T < this) {
+ T.Lock.release();
+ Lock.release();
+ } else {
+ Lock.release();
+ T.Lock.release();
+ }
+
return *this;
}
/// the relevant timer. This makes it easy to time a region of code.
///
class TimeRegion {
- Timer &T;
+ Timer *T;
TimeRegion(const TimeRegion &); // DO NOT IMPLEMENT
public:
- TimeRegion(Timer &t) : T(t) {
- T.startTimer();
+ explicit TimeRegion(Timer &t) : T(&t) {
+ T->startTimer();
+ }
+ explicit TimeRegion(Timer *t) : T(t) {
+ if (T)
+ T->startTimer();
}
~TimeRegion() {
- T.stopTimer();
+ if (T)
+ T->stopTimer();
}
};
/// is primarily used for debugging and for hunting performance problems.
///
struct NamedRegionTimer : public TimeRegion {
- NamedRegionTimer(const std::string &Name);
+ explicit NamedRegionTimer(const std::string &Name);
+ explicit NamedRegionTimer(const std::string &Name,
+ const std::string &GroupName);
};
unsigned NumTimers;
std::vector<Timer> TimersToPrint;
public:
- TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
+ explicit TimerGroup(const std::string &name) : Name(name), NumTimers(0) {}
~TimerGroup() {
assert(NumTimers == 0 &&
"TimerGroup destroyed before all contained timers!");
private:
friend class Timer;
- void addTimer() { ++NumTimers; }
+ void addTimer();
void removeTimer();
- void addTimerToPrint(const Timer &T) {
- TimersToPrint.push_back(Timer(true, T));
- }
+ void addTimerToPrint(const Timer &T);
};
} // End llvm namespace