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