|
| 1 | +// Copyright (c) Facebook, Inc. and its affiliates. |
| 2 | +// |
| 3 | +// This source code is licensed under the MIT license found in the |
| 4 | +// LICENSE file in the root directory of this source tree. |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include <grpcpp/grpcpp.h> |
| 8 | + |
| 9 | +#include "boost/filesystem.hpp" |
| 10 | + |
| 11 | +namespace compiler_gym { |
| 12 | + |
| 13 | +/** |
| 14 | + * Execution context of a compiler gym service. |
| 15 | + * |
| 16 | + * This class encapsulates mutable state that is shared between all compilation |
| 17 | + * sessions. An instance of this class is passed to every new |
| 18 | + * CompilationSession. |
| 19 | + * |
| 20 | + * You may subclass CompilerGymServiceContext to add additional mutable state. |
| 21 | + * The subclass . |
| 22 | + * |
| 23 | + * \code{.cpp} |
| 24 | + * |
| 25 | + * #include "compiler_gym/service/CompilationSession.h" |
| 26 | + * #include "compiler_gym/service/CompilerGymServiceContext.h" |
| 27 | + * #include "compiler_gym/service/runtime/Runtime.h" |
| 28 | + * |
| 29 | + * using namespace compiler_gym; |
| 30 | + * |
| 31 | + * class MyServiceContext final : public ServiceContext { ... } |
| 32 | + * |
| 33 | + * class MyCompilationSession final : public CompilationSession { ... } |
| 34 | + * |
| 35 | + * int main(int argc, char** argv) { |
| 36 | + * return runtime::createAndRunCompilerGymService<MyCompilationSession, MyServiceContext>(); |
| 37 | + * } |
| 38 | + * \endcode |
| 39 | + */ |
| 40 | +class CompilerGymServiceContext { |
| 41 | + public: |
| 42 | + CompilerGymServiceContext(const boost::filesystem::path& workingDirectory); |
| 43 | + |
| 44 | + /** |
| 45 | + * Initialize context. |
| 46 | + * |
| 47 | + * Called before any compilation sessions are created. Use this method to |
| 48 | + * initialize any mutable state. If this routine returns an error, the service |
| 49 | + * will terminate. |
| 50 | + * |
| 51 | + * @return A status. |
| 52 | + */ |
| 53 | + [[nodiscard]] virtual grpc::Status init(); |
| 54 | + |
| 55 | + /** |
| 56 | + * Uninitialize context. |
| 57 | + * |
| 58 | + * Called after all compilation sessions have ended, before a service |
| 59 | + * terminates. Use this method to perform tidying up. This method is always |
| 60 | + * called, even if init() fails. If this routine returns an error, the service |
| 61 | + * will terminate with a nonzero error code. |
| 62 | + * |
| 63 | + * @return A status. |
| 64 | + */ |
| 65 | + [[nodiscard]] virtual grpc::Status shutdown(); |
| 66 | + |
| 67 | + /** |
| 68 | + * Get the working directory. |
| 69 | + * |
| 70 | + * The working directory is a local filesystem directory that compilation |
| 71 | + * sessions can use to store temporary files such as build artifacts. The |
| 72 | + * directory is guaranteed to exist. |
| 73 | + * |
| 74 | + * \note When possible, an in-memory filesystem will be used for the working |
| 75 | + * directory. This means that the working directory is not suitable for |
| 76 | + * very large files or executables, as some systems prevent execution of |
| 77 | + * in-memory files. |
| 78 | + * |
| 79 | + * \note A single working directory is shared by all of the compilation |
| 80 | + * sessions of a service. Do not assume that you have exclusive access. |
| 81 | + * |
| 82 | + * @return A path. |
| 83 | + */ |
| 84 | + inline const boost::filesystem::path& workingDirectory() const { return workingDirectory_; }; |
| 85 | + |
| 86 | + private: |
| 87 | + const boost::filesystem::path workingDirectory_; |
| 88 | +}; |
| 89 | + |
| 90 | +} // namespace compiler_gym |
0 commit comments