Process.cpp revision 36b56886974eae4f9c5ebc96befd3e7bfe5de338
1474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
2474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
3474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//                     The LLVM Compiler Infrastructure
4474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
5474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// This file is distributed under the University of Illinois Open Source
6474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// License. See LICENSE.TXT for details.
7474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
8474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===----------------------------------------------------------------------===//
9474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
10474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//  This header file implements the operating system Process concept.
11474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//
12474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===----------------------------------------------------------------------===//
13474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
14474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/Config/config.h"
15474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/Support/ErrorHandling.h"
16474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "llvm/Support/Process.h"
17474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
18474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgusing namespace llvm;
19474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgusing namespace sys;
20474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
21474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===----------------------------------------------------------------------===//
22474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//=== WARNING: Implementation here must contain only TRULY operating system
23474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org//===          independent code.
24167514562bbce1eb0566271d6cb41d90d2b5ffa0hclam@chromium.org//===----------------------------------------------------------------------===//
25167514562bbce1eb0566271d6cb41d90d2b5ffa0hclam@chromium.org
265c1d3b27608a3f3f6028c069b9bf066a4de474b6hclam@chromium.org// Empty virtual destructor to anchor the vtable for the process class.
27474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgprocess::~process() {}
28474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
29474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgself_process *process::get_self() {
30474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  // Use a function local static for thread safe initialization and allocate it
31474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  // as a raw pointer to ensure it is never destroyed.
32474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  static self_process *SP = new self_process();
33474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
34474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  return SP;
35474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org}
36474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
37474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// The destructor for the self_process subclass must never actually be
38474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// executed. There should be at most one instance of this class, and that
39474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// instance should live until the process terminates to avoid the potential for
40474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// racy accesses during shutdown.
41474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgself_process::~self_process() {
42474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  llvm_unreachable("This destructor must never be executed!");
43474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org}
44474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
45474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// \brief A helper function to compute the elapsed wall-time since the program
46474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// started.
47474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org///
48474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// Note that this routine actually computes the elapsed wall time since the
49474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// first time it was called. However, we arrange to have it called during the
50474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// startup of the process to get approximately correct results.
51474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgstatic TimeValue getElapsedWallTime() {
521cedac70da0675180660d8d3478648400afde47djohannkoenig@chromium.org  static TimeValue &StartTime = *new TimeValue(TimeValue::now());
53474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  return TimeValue::now() - StartTime;
54474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org}
55474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
56474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// \brief A special global variable to ensure we call \c getElapsedWallTime
57474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// during global initialization of the program.
58474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org///
59474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// Note that this variable is never referenced elsewhere. Doing so could
60474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org/// create race conditions during program startup or shutdown.
61474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgstatic volatile TimeValue DummyTimeValue = getElapsedWallTime();
62474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
63474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// Implement this routine by using the static helpers above. They're already
64474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// portable.
65474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgTimeValue self_process::get_wall_time() const {
66474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  return getElapsedWallTime();
67474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org}
68474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
69474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
70474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
71474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
72474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#define ALLCOLORS(FGBG,BOLD) {\
73474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "0", BOLD),\
74474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "1", BOLD),\
75474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "2", BOLD),\
76474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "3", BOLD),\
77474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "4", BOLD),\
78474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "5", BOLD),\
79474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "6", BOLD),\
80474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org    COLOR(FGBG, "7", BOLD)\
81474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org  }
82474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
83474eb7536515fb785e925cc9375d22817c416851hclam@chromium.orgstatic const char colorcodes[2][2][8][10] = {
84474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
85474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
86474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org};
87474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org
88474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org// Include the platform-specific parts of this class.
89474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#ifdef LLVM_ON_UNIX
90474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "Unix/Process.inc"
91474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#endif
921929f88e772d51271e69324816701e01c9e7c579johannkoenig@chromium.org#ifdef LLVM_ON_WIN32
93474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#include "Windows/Process.inc"
94474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org#endif
95474eb7536515fb785e925cc9375d22817c416851hclam@chromium.org