summaryrefslogtreecommitdiffstats
path: root/source/core/slang-process.h
blob: 7cd7596aec857c7c0f14fb5fbd74f5aa7d796905 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// slang-process.h
#ifndef SLANG_PROCESS_H
#define SLANG_PROCESS_H

#include "slang-string.h"
#include "slang-list.h"
#include "slang-stream.h"
#include "slang-io.h"

#include "slang-string-escape-util.h"

#include "slang-command-line.h"

namespace Slang {

class Process : public RefObject
{
public:
    enum class StreamType
    {
        ErrorOut,
        StdOut,
        StdIn,
        CountOf,
    };

    typedef uint32_t Flags;
    struct Flag
    {
        enum Enum : Flags
        {
            AttachDebugger = 0x01,
        };
    };

        /// Get the stream for the type
    Stream* getStream(StreamType type) const { return m_streams[Index(type)]; }

        /// Get the value returned from the process when it exited/returned. 
    int32_t getReturnValue() const { return m_returnValue;  }

        /// True if the process has terminated
    virtual bool isTerminated() = 0;

        /// Blocks until the process has terminated or the timeout completes
        /// Can optionally supply a timeout time. -1 means 'infinite' and is the default.
        /// Note that the timeOut is only used approximately. 
        /// Returns true if has terminated.
    virtual bool waitForTermination(Int timeOutInMs = -1) = 0;

        /// Terminate the process gracefully.
        /// After calling it may take time before the process actually terminates
        /// Ie calling isTerminated directly after `terminate` may return false.
        /// The return code depending on implementation/termination style, may not be set.
    virtual void terminate(int32_t returnCode) = 0;

        /// Kill the process - attempt to terminate immediately. 
    virtual void kill(int32_t returnCode) = 0;

        /// The quoting style used for the command line on this target. Currently just uses Space,
        /// but in future may take into account platform sec
    static StringEscapeHandler* getEscapeHandler();

        /// Get the suffix used on this platform
    static UnownedStringSlice getExecutableSuffix();

        /// Create a process using the executable/args defined from the commandLine
    static SlangResult create(const CommandLine& commandLine, Process::Flags flags, RefPtr<Process>& outProcess);

        /// Sleep the current thread for time specified in milliseconds. 0 indicates to OS ok to yield this thread.
    static void sleepCurrentThread(Int timeInMs);

        /// Get a standard stream 
    static SlangResult getStdStream(StreamType type, RefPtr<Stream>& out);

        /// Get the clock frequency
    static uint64_t getClockFrequency();

        /// Get the clock tick.
    static uint64_t getClockTick();

protected:
    int32_t m_returnValue = 0;                              ///< Value returned if process terminated
    RefPtr<Stream> m_streams[Index(StreamType::CountOf)];   ///< Streams to communicate with the process
};

}

#endif // SLANG_PROCESS_H