1 //===-- Process.cpp - Implement OS Process Concept --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the operating system Process concept.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/StringExtras.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/ErrorHandling.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Process.h"
19 #include "llvm/Support/Program.h"
24 //===----------------------------------------------------------------------===//
25 //=== WARNING: Implementation here must contain only TRULY operating system
26 //=== independent code.
27 //===----------------------------------------------------------------------===//
29 // Empty virtual destructor to anchor the vtable for the process class.
30 process::~process() {}
32 self_process *process::get_self() {
33 // Use a function local static for thread safe initialization and allocate it
34 // as a raw pointer to ensure it is never destroyed.
35 static self_process *SP = new self_process();
40 // The destructor for the self_process subclass must never actually be
41 // executed. There should be at most one instance of this class, and that
42 // instance should live until the process terminates to avoid the potential for
43 // racy accesses during shutdown.
44 self_process::~self_process() {
45 llvm_unreachable("This destructor must never be executed!");
48 /// \brief A helper function to compute the elapsed wall-time since the program
51 /// Note that this routine actually computes the elapsed wall time since the
52 /// first time it was called. However, we arrange to have it called during the
53 /// startup of the process to get approximately correct results.
54 static TimeValue getElapsedWallTime() {
55 static TimeValue &StartTime = *new TimeValue(TimeValue::now());
56 return TimeValue::now() - StartTime;
59 /// \brief A special global variable to ensure we call \c getElapsedWallTime
60 /// during global initialization of the program.
62 /// Note that this variable is never referenced elsewhere. Doing so could
63 /// create race conditions during program startup or shutdown.
64 static volatile TimeValue DummyTimeValue = getElapsedWallTime();
66 // Implement this routine by using the static helpers above. They're already
68 TimeValue self_process::get_wall_time() const {
69 return getElapsedWallTime();
72 Optional<std::string> Process::FindInEnvPath(const std::string& EnvName,
73 const std::string& FileName)
75 Optional<std::string> FoundPath;
76 Optional<std::string> OptPath = Process::GetEnv(EnvName);
77 if (!OptPath.hasValue())
80 const char EnvPathSeparatorStr[] = {EnvPathSeparator, '\0'};
81 SmallVector<StringRef, 8> Dirs;
82 SplitString(OptPath.getValue(), Dirs, EnvPathSeparatorStr);
84 for (const auto &Dir : Dirs) {
88 SmallString<128> FilePath(Dir);
89 path::append(FilePath, FileName);
90 if (fs::exists(Twine(FilePath))) {
91 FoundPath = FilePath.str();
100 #define COLOR(FGBG, CODE, BOLD) "\033[0;" BOLD FGBG CODE "m"
102 #define ALLCOLORS(FGBG,BOLD) {\
103 COLOR(FGBG, "0", BOLD),\
104 COLOR(FGBG, "1", BOLD),\
105 COLOR(FGBG, "2", BOLD),\
106 COLOR(FGBG, "3", BOLD),\
107 COLOR(FGBG, "4", BOLD),\
108 COLOR(FGBG, "5", BOLD),\
109 COLOR(FGBG, "6", BOLD),\
110 COLOR(FGBG, "7", BOLD)\
113 static const char colorcodes[2][2][8][10] = {
114 { ALLCOLORS("3",""), ALLCOLORS("3","1;") },
115 { ALLCOLORS("4",""), ALLCOLORS("4","1;") }
118 // Include the platform-specific parts of this class.
120 #include "Unix/Process.inc"
123 #include "Windows/Process.inc"