blob: 087c459e187e40e65fe464ff8d818a93e757a8c4 [file] [log] [blame]
Reid Spencer566ac282004-09-11 04:59:30 +00001//===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
Misha Brukman10468d82005-04-21 22:55:34 +00002//
Reid Spencer566ac282004-09-11 04:59:30 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman10468d82005-04-21 22:55:34 +00007//
Reid Spencer566ac282004-09-11 04:59:30 +00008//===----------------------------------------------------------------------===//
9//
Hans Wennborgcfe341f2014-06-20 01:36:00 +000010// This file implements the operating system Process concept.
Reid Spencer566ac282004-09-11 04:59:30 +000011//
12//===----------------------------------------------------------------------===//
13
Reid Spencer97d4a172004-12-27 06:15:29 +000014#include "llvm/Config/config.h"
Chandler Carruth97683aa2012-12-31 11:17:50 +000015#include "llvm/Support/ErrorHandling.h"
Chandler Carruthbe810232013-01-02 10:22:59 +000016#include "llvm/Support/Process.h"
Reid Spencer566ac282004-09-11 04:59:30 +000017
Chandler Carruth5473dfb2012-12-31 11:45:20 +000018using namespace llvm;
Reid Spencer566ac282004-09-11 04:59:30 +000019using namespace sys;
20
21//===----------------------------------------------------------------------===//
22//=== WARNING: Implementation here must contain only TRULY operating system
Misha Brukman10468d82005-04-21 22:55:34 +000023//=== independent code.
Reid Spencer566ac282004-09-11 04:59:30 +000024//===----------------------------------------------------------------------===//
25
Chandler Carruth97683aa2012-12-31 11:17:50 +000026// 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// The destructor for the self_process subclass must never actually be
38// executed. There should be at most one instance of this class, and that
39// instance should live until the process terminates to avoid the potential for
40// racy accesses during shutdown.
41self_process::~self_process() {
42 llvm_unreachable("This destructor must never be executed!");
43}
44
Chandler Carruthef7f9682013-01-04 23:19:55 +000045/// \brief A helper function to compute the elapsed wall-time since the program
46/// started.
47///
48/// Note that this routine actually computes the elapsed wall time since the
49/// first time it was called. However, we arrange to have it called during the
50/// startup of the process to get approximately correct results.
51static TimeValue getElapsedWallTime() {
52 static TimeValue &StartTime = *new TimeValue(TimeValue::now());
53 return TimeValue::now() - StartTime;
54}
55
56/// \brief A special global variable to ensure we call \c getElapsedWallTime
57/// during global initialization of the program.
58///
59/// Note that this variable is never referenced elsewhere. Doing so could
60/// create race conditions during program startup or shutdown.
61static volatile TimeValue DummyTimeValue = getElapsedWallTime();
62
63// Implement this routine by using the static helpers above. They're already
64// portable.
65TimeValue self_process::get_wall_time() const {
66 return getElapsedWallTime();
67}
68
69
Nico Rieck92d649a2013-09-11 00:36:48 +000070#define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
71
72#define ALLCOLORS(FGBG,BOLD) {\
73 COLOR(FGBG, "0", BOLD),\
74 COLOR(FGBG, "1", BOLD),\
75 COLOR(FGBG, "2", BOLD),\
76 COLOR(FGBG, "3", BOLD),\
77 COLOR(FGBG, "4", BOLD),\
78 COLOR(FGBG, "5", BOLD),\
79 COLOR(FGBG, "6", BOLD),\
80 COLOR(FGBG, "7", BOLD)\
81 }
82
83static const char colorcodes[2][2][8][10] = {
84 { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
85 { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
86};
87
Reid Spencer566ac282004-09-11 04:59:30 +000088// Include the platform-specific parts of this class.
Reid Spencer97d4a172004-12-27 06:15:29 +000089#ifdef LLVM_ON_UNIX
Reid Spencerc892a0d2005-01-09 23:29:00 +000090#include "Unix/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000091#endif
92#ifdef LLVM_ON_WIN32
Michael J. Spencer447762d2010-11-29 18:16:10 +000093#include "Windows/Process.inc"
Reid Spencer97d4a172004-12-27 06:15:29 +000094#endif