Add missing #include
[oota-llvm.git] / lib / Support / SlowOperationInformer.cpp
1 //===-- SlowOperationInformer.cpp - Keep the user informed ----------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 // 
10 // This file implements the SlowOperationInformer class for the LLVM debugger.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "Support/SlowOperationInformer.h"
15 #include "Config/config.h"     // Get the signal handler return type
16 #include <iostream>
17 #include <sstream>
18 #include <signal.h>
19 #include <unistd.h>
20 #include <cassert>
21 using namespace llvm;
22
23 /// OperationCancelled - This flag is set by the SIGINT signal handler if the
24 /// user presses CTRL-C.
25 static volatile bool OperationCancelled;
26
27 /// ShouldShowStatus - This flag gets set if the operation takes a long time.
28 ///
29 static volatile bool ShouldShowStatus;
30
31 /// NestedSOI - Sanity check.  SlowOperationInformers cannot be nested or run in
32 /// parallel.  This ensures that they never do.
33 static bool NestedSOI = false;
34
35 static RETSIGTYPE SigIntHandler(int Sig) {
36   OperationCancelled = true;
37   signal(SIGINT, SigIntHandler);
38 }
39
40 static RETSIGTYPE SigAlarmHandler(int Sig) {
41   ShouldShowStatus = true;
42 }
43
44 static sighandler_t OldSigIntHandler;
45
46
47 SlowOperationInformer::SlowOperationInformer(const std::string &Name)
48   : OperationName(Name), LastPrintAmount(0) {
49   assert(!NestedSOI && "SlowerOperationInformer objects cannot be nested!");
50   NestedSOI = true;
51
52   OperationCancelled = 0;
53   ShouldShowStatus = 0;
54
55   signal(SIGALRM, SigAlarmHandler);
56   OldSigIntHandler = signal(SIGINT, SigIntHandler);
57   alarm(1);
58 }
59
60 SlowOperationInformer::~SlowOperationInformer() {
61   NestedSOI = false;
62   if (LastPrintAmount)
63     std::cout << "\n";
64
65   alarm(0);
66   signal(SIGALRM, SIG_DFL);
67   signal(SIGINT, OldSigIntHandler);
68 }
69
70 /// progress - Clients should periodically call this method when they are in
71 /// an exception-safe state.  The Amount variable should indicate how far
72 /// along the operation is, given in 1/10ths of a percent (in other words,
73 /// Amount should range from 0 to 1000).
74 void SlowOperationInformer::progress(unsigned Amount) {
75   if (OperationCancelled) {
76     std::cout << "\n";
77     LastPrintAmount = 0;
78     throw "While " + OperationName + ", operation cancelled.";
79   }
80
81   // If we haven't spent enough time in this operation to warrant displaying the
82   // progress bar, don't do so yet.
83   if (!ShouldShowStatus)
84     return;
85
86   // Delete whatever we printed last time.
87   std::string ToPrint = std::string(LastPrintAmount, '\b');
88
89   std::ostringstream OS;
90   OS << "Progress " << OperationName << ": " << Amount/10 << "." << Amount % 10
91      << "%";
92
93   LastPrintAmount = OS.str().size();
94   std::cout << ToPrint+OS.str() << std::flush;
95 }