1360842753d078e118f3a76ea6ef18054bc9b2eb
[oota-llvm.git] / lib / Support / Process.cpp
1 //===-- Process.cpp - Implement OS Process Concept --------------*- 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 header file implements the operating system Process concept.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Config/config.h"
15 #if LLVM_ON_WIN32
16   // This define makes stdlib.h declare the rand_s function.
17 #define _CRT_RAND_S
18 #include <stdlib.h>
19 #endif
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/Process.h"
22
23 using namespace llvm;
24 using namespace sys;
25
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only TRULY operating system
28 //===          independent code.
29 //===----------------------------------------------------------------------===//
30
31 // Empty virtual destructor to anchor the vtable for the process class.
32 process::~process() {}
33
34 self_process *process::get_self() {
35   // Use a function local static for thread safe initialization and allocate it
36   // as a raw pointer to ensure it is never destroyed.
37   static self_process *SP = new self_process();
38
39   return SP;
40 }
41
42 #if defined(_MSC_VER)
43 // Visual Studio complains that the self_process destructor never exits. This
44 // doesn't make much sense, as that's the whole point of calling abort... Just
45 // silence this warning.
46 #pragma warning(push)
47 #pragma warning(disable:4722)
48 #endif
49
50 // The destructor for the self_process subclass must never actually be
51 // executed. There should be at most one instance of this class, and that
52 // instance should live until the process terminates to avoid the potential for
53 // racy accesses during shutdown.
54 self_process::~self_process() {
55   llvm_unreachable("This destructor must never be executed!");
56 }
57
58 /// \brief A helper function to compute the elapsed wall-time since the program
59 /// started.
60 ///
61 /// Note that this routine actually computes the elapsed wall time since the
62 /// first time it was called. However, we arrange to have it called during the
63 /// startup of the process to get approximately correct results.
64 static TimeValue getElapsedWallTime() {
65   static TimeValue &StartTime = *new TimeValue(TimeValue::now());
66   return TimeValue::now() - StartTime;
67 }
68
69 /// \brief A special global variable to ensure we call \c getElapsedWallTime
70 /// during global initialization of the program.
71 ///
72 /// Note that this variable is never referenced elsewhere. Doing so could
73 /// create race conditions during program startup or shutdown.
74 static volatile TimeValue DummyTimeValue = getElapsedWallTime();
75
76 // Implement this routine by using the static helpers above. They're already
77 // portable.
78 TimeValue self_process::get_wall_time() const {
79   return getElapsedWallTime();
80 }
81
82
83 #if defined(_MSC_VER)
84 #pragma warning(pop)
85 #endif
86
87
88 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
89
90 #define ALLCOLORS(FGBG,BOLD) {\
91     COLOR(FGBG, "0", BOLD),\
92     COLOR(FGBG, "1", BOLD),\
93     COLOR(FGBG, "2", BOLD),\
94     COLOR(FGBG, "3", BOLD),\
95     COLOR(FGBG, "4", BOLD),\
96     COLOR(FGBG, "5", BOLD),\
97     COLOR(FGBG, "6", BOLD),\
98     COLOR(FGBG, "7", BOLD)\
99   }
100
101 static const char colorcodes[2][2][8][10] = {
102  { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
103  { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
104 };
105
106 // Include the platform-specific parts of this class.
107 #ifdef LLVM_ON_UNIX
108 #include "Unix/Process.inc"
109 #endif
110 #ifdef LLVM_ON_WIN32
111 #include "Windows/Process.inc"
112 #endif