• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_PLATFORM_WINDOWS_SUBPROCESS_H_
17 #define TENSORFLOW_CORE_PLATFORM_WINDOWS_SUBPROCESS_H_
18 
19 #include <string>
20 #include <vector>
21 
22 #include "tensorflow/core/platform/macros.h"
23 #include "tensorflow/core/platform/mutex.h"
24 #include "tensorflow/core/platform/types.h"
25 
26 namespace tensorflow {
27 
28 class SubProcess {
29  public:
30   // SubProcess()
31   //    nfds: The number of file descriptors to use.
32   explicit SubProcess(int nfds = 3);
33 
34   // Virtual for backwards compatibility; do not create new subclasses.
35   // It is illegal to delete the SubProcess within its exit callback.
36   virtual ~SubProcess();
37 
38   // SetChannelAction()
39   //    Set how to handle a channel.  The default action is ACTION_CLOSE.
40   //    The action is set for all subsequent processes, until SetChannel()
41   //    is called again.
42   //
43   //    SetChannel may not be called while the process is running.
44   //
45   //    chan: Which channel this applies to.
46   //    action: What to do with the channel.
47   // Virtual for backwards compatibility; do not create new subclasses.
48   virtual void SetChannelAction(Channel chan, ChannelAction action);
49 
50   // SetProgram()
51   //    Set up a program and argument list for execution, with the full
52   //    "raw" argument list passed as a vector of strings.  argv[0]
53   //    should be the program name, just as in execv().
54   //
55   //    file: The file containing the program.  This must be an absolute path
56   //          name - $PATH is not searched.
57   //    argv: The argument list.
58   virtual void SetProgram(const string& file, const std::vector<string>& argv);
59 
60   // Start()
61   //    Run the command that was previously set up with SetProgram().
62   //    The following are fatal programming errors:
63   //       * Attempting to start when a process is already running.
64   //       * Attempting to start without first setting the command.
65   //    Note, however, that Start() does not try to validate that the binary
66   //    does anything reasonable (e.g. exists or can execute); as such, you can
67   //    specify a non-existent binary and Start() will still return true.  You
68   //    will get a failure from the process, but only after Start() returns.
69   //
70   //    Return true normally, or false if the program couldn't be started
71   //    because of some error.
72   // Virtual for backwards compatibility; do not create new subclasses.
73   virtual bool Start();
74 
75   // Kill()
76   //    Send the given signal to the process.
77   //    Return true normally, or false if we couldn't send the signal - likely
78   //    because the process doesn't exist.
79   virtual bool Kill(int signal);
80 
81   // Wait()
82   //    Block until the process exits.
83   //    Return true normally, or false if the process wasn't running.
84   virtual bool Wait();
85 
86   // Communicate()
87   //    Read from stdout and stderr and writes to stdin until all pipes have
88   //    closed, then waits for the process to exit.
89   //    Note: Do NOT call Wait() after calling Communicate as it will always
90   //     fail, since Communicate calls Wait() internally.
91   //    'stdin_input', 'stdout_output', and 'stderr_output' may be NULL.
92   //    If this process is not configured to send stdout or stderr to pipes,
93   //     the output strings will not be modified.
94   //    If this process is not configured to take stdin from a pipe, stdin_input
95   //     will be ignored.
96   //    Returns the command's exit status.
97   virtual int Communicate(const string* stdin_input, string* stdout_output,
98                           string* stderr_output);
99 
100  private:
101   static const int kNFds = 3;
chan_valid(int chan)102   static bool chan_valid(int chan) { return ((chan >= 0) && (chan < kNFds)); }
103 
104   void FreeArgs() TF_EXCLUSIVE_LOCKS_REQUIRED(data_mu_);
105   void ClosePipes() TF_EXCLUSIVE_LOCKS_REQUIRED(data_mu_);
106   bool WaitInternal(int* status);
107 
108   // The separation between proc_mu_ and data_mu_ mutexes allows Kill() to be
109   // called by a thread while another thread is inside Wait() or Communicate().
110   mutable mutex proc_mu_;
111   bool running_ TF_GUARDED_BY(proc_mu_);
112   void* win_pi_ TF_GUARDED_BY(proc_mu_);
113 
114   mutable mutex data_mu_ TF_ACQUIRED_AFTER(proc_mu_);
115   char* exec_path_ TF_GUARDED_BY(data_mu_);
116   char** exec_argv_ TF_GUARDED_BY(data_mu_);
117   ChannelAction action_[kNFds] TF_GUARDED_BY(data_mu_);
118   void* parent_pipe_[kNFds] TF_GUARDED_BY(data_mu_);
119 
120   TF_DISALLOW_COPY_AND_ASSIGN(SubProcess);
121 };
122 
123 }  // namespace tensorflow
124 
125 #endif  // TENSORFLOW_CORE_PLATFORM_WINDOWS_SUBPROCESS_H_
126