char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyencommon: add `subproc.h` wrapper, disabled on android/ios (#26102)d2a818231

char/aliceai-t5-moe
1.8 KiB59 linesraw
1#pragma once
2
3#include <atomic>
4#include <cstdio>
5#include <string>
6#include <vector>
7
8#ifdef LLAMA_SUBPROCESS
9#include <sheredom/subprocess.h>
10#else
11// dummy values to allow compilation when subprocess is disabled
12struct subprocess_s {};
13static constexpr int subprocess_option_no_window = 0;
14static constexpr int subprocess_option_combined_stdout_stderr = 0;
15static constexpr int subprocess_option_inherit_environment = 0;
16static constexpr int subprocess_option_search_user_path = 0;
17#endif
18
19// RAII-style wrapper around https://github.com/sheredom/subprocess.h,
20// exposing method calls instead of free functions operating on subprocess_s.
21struct common_subproc {
22    common_subproc() = default;
23    ~common_subproc();
24
25    common_subproc(const common_subproc &) = delete;
26    common_subproc & operator=(const common_subproc &) = delete;
27
28    // spawn a child process; if env is non-empty it replaces the child's environment
29    // (do not combine with subprocess_option_inherit_environment)
30    bool create(
31            const std::vector<std::string> & args,
32            int options,
33            const std::vector<std::string> & env = {},
34            const char * cwd = nullptr);
35
36    bool alive();
37
38    // true if LLAMA_SUBPROCESS was enabled at build time; when false, create() always fails
39    static bool is_supported();
40
41    FILE * stdin_file();
42    FILE * stdout_file();
43    FILE * stderr_file();
44
45    // close stdin and detach it from the process, so a later join()/destroy() won't double-close it;
46    // use this after writing all input to signal EOF to the child while it's still running
47    void close_stdin();
48
49    void terminate();
50
51    // wait for the process to exit, release the underlying handle and return its exit code
52    int join();
53
54private:
55    subprocess_s proc {};
56    std::atomic<bool> is_created{false};
57
58    bool has_handle() const;
59};