Try r84890 again (adding ValueMap<>), now that I've tested the compile on
[oota-llvm.git] / include / llvm / Support / Timer.h
index 4164ddc891a7ac34a75b495d4db2e4905a9e52be..54f1da96cad67ed4768eccb3c6445cc66e101a68 100644 (file)
 #define LLVM_SUPPORT_TIMER_H
 
 #include "llvm/Support/DataTypes.h"
+#include "llvm/System/Mutex.h"
 #include <string>
 #include <vector>
-#include <iosfwd>
 #include <cassert>
 
 namespace llvm {
 
 class TimerGroup;
+class raw_ostream;
 
 /// Timer - This class is used to track the amount of time spent between
-/// invocations of it's startTimer()/stopTimer() methods.  Given appropriate OS
+/// invocations of its startTimer()/stopTimer() methods.  Given appropriate OS
 /// support it can also keep track of the RSS of the program at various points.
 /// By default, the Timer will print the amount of time it has captured to
 /// standard error when the laster timer is destroyed, otherwise it is printed
@@ -43,6 +44,7 @@ class Timer {
   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:
   explicit Timer(const std::string &N);
   Timer(const std::string &N, TimerGroup &tg);
@@ -56,6 +58,14 @@ public:
   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;
@@ -65,6 +75,15 @@ public:
     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;
   }
 
@@ -93,7 +112,7 @@ public:
 
   /// print - Print the current timer to standard error, and reset the "Started"
   /// flag.
-  void print(const Timer &Total, std::ostream &OS);
+  void print(const Timer &Total, raw_ostream &OS);
 
 private:
   friend class TimerGroup;
@@ -113,14 +132,19 @@ private:
 /// 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:
-  explicit 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();
   }
 };
 
@@ -132,6 +156,8 @@ public:
 ///
 struct NamedRegionTimer : public TimeRegion {
   explicit NamedRegionTimer(const std::string &Name);
+  explicit NamedRegionTimer(const std::string &Name,
+                            const std::string &GroupName);
 };
 
 
@@ -153,11 +179,9 @@ public:
 
 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