b30aa987523cf97504c2539334ce2f689a38c51d
[oota-llvm.git] / include / llvm / Support / SlowOperationInformer.h
1 //===- llvm/Support/SlowOperationInformer.h - Keep user informed *- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines a simple object which can be used to let the user know what
11 // is going on when a slow operation is happening, and gives them the ability to
12 // cancel it.  Potentially slow operations can stack allocate one of these
13 // objects, and periodically call the "progress" method to update the progress
14 // bar.  If the operation takes more than 1 second to complete, the progress bar
15 // is automatically shown and updated.  As such, the slow operation should not
16 // print stuff to the screen, and should not be confused if an extra line
17 // appears on the screen (ie, the cursor should be at the start of the line).
18 //
19 // If the user presses CTRL-C during the operation, the next invocation of the
20 // progress method return true indicating that the operation was cancelled.
21 //
22 // Because SlowOperationInformers fiddle around with signals, they cannot be
23 // nested, and interact poorly with threads.  The SIGALRM handler is set back to
24 // SIGDFL, but the SIGINT signal handler is restored when the
25 // SlowOperationInformer is destroyed.
26 //
27 //===----------------------------------------------------------------------===//
28
29 #ifndef LLVM_SUPPORT_SLOW_OPERATION_INFORMER_H
30 #define LLVM_SUPPORT_SLOW_OPERATION_INFORMER_H
31
32 #include <string>
33 #include <cassert>
34 #include "llvm/Support/DataTypes.h"
35
36 namespace llvm {
37   class SlowOperationInformer {
38     std::string OperationName;
39     unsigned LastPrintAmount;
40
41     SlowOperationInformer(const SlowOperationInformer&);   // DO NOT IMPLEMENT
42     void operator=(const SlowOperationInformer&);          // DO NOT IMPLEMENT
43   public:
44     SlowOperationInformer(const std::string &Name);
45     ~SlowOperationInformer();
46
47     /// progress - Clients should periodically call this method when they can
48     /// handle cancellation.  The Amount variable should indicate how far
49     /// along the operation is, given in 1/10ths of a percent (in other words,
50     /// Amount should range from 0 to 1000).  If the user cancels the operation,
51     /// this returns true, false otherwise.
52     bool progress(unsigned Amount);
53
54     /// progress - Same as the method above, but this performs the division for
55     /// you, and helps you avoid overflow if you are dealing with largish
56     /// numbers.
57     bool progress(unsigned Current, unsigned Maximum) {
58       assert(Maximum != 0 &&
59              "Shouldn't be doing work if there is nothing to do!");
60       return progress(Current*uint64_t(1000UL)/Maximum);
61     }
62   };
63 } // end namespace llvm
64
65 #endif /* SLOW_OPERATION_INFORMER_H */