• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_
6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_
7 
8 #include <deque>
9 #include <vector>
10 
11 #include "native_client/src/include/nacl_macros.h"
12 #include "native_client/src/include/nacl_scoped_ptr.h"
13 #include "native_client/src/include/nacl_string.h"
14 #include "native_client/src/shared/platform/nacl_threads.h"
15 #include "native_client/src/shared/platform/nacl_sync_checked.h"
16 
17 #include "ppapi/cpp/completion_callback.h"
18 
19 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
20 #include "ppapi/native_client/src/trusted/plugin/service_runtime.h"
21 
22 struct PP_PNaClOptions;
23 
24 namespace nacl {
25 class DescWrapper;
26 }
27 
28 
29 namespace plugin {
30 
31 class NaClSubprocess;
32 class Plugin;
33 class PnaclCoordinator;
34 class PnaclResources;
35 class TempFile;
36 
37 class PnaclTranslateThread {
38  public:
39   PnaclTranslateThread();
40   ~PnaclTranslateThread();
41 
42   // Start the translation process. It will continue to run and consume data
43   // as it is passed in with PutBytes.
44   void RunTranslate(const pp::CompletionCallback& finish_callback,
45                     const std::vector<TempFile*>* obj_files,
46                     TempFile* nexe_file,
47                     nacl::DescWrapper* invalid_desc_wrapper,
48                     ErrorInfo* error_info,
49                     PnaclResources* resources,
50                     PP_PNaClOptions* pnacl_options,
51                     const nacl::string &architecture_attributes,
52                     PnaclCoordinator* coordinator,
53                     Plugin* plugin);
54 
55   // Kill the llc and/or ld subprocesses. This happens by closing the command
56   // channel on the plugin side, which causes the trusted code in the nexe to
57   // exit, which will cause any pending SRPCs to error. Because this is called
58   // on the main thread, the translation thread must not use the subprocess
59   // objects without the lock, other than InvokeSrpcMethod, which does not
60   // race with service runtime shutdown.
61   void AbortSubprocesses();
62 
63   // Send bitcode bytes to the translator. Called from the main thread.
64   void PutBytes(std::vector<char>* data, int count);
65 
GetCompileTime()66   int64_t GetCompileTime() const { return compile_time_; }
67 
68  private:
69   // Helper thread entry point for translation. Takes a pointer to
70   // PnaclTranslateThread and calls DoTranslate().
71   static void WINAPI DoTranslateThread(void* arg);
72   // Runs the streaming translation. Called from the helper thread.
73   void DoTranslate() ;
74   // Signal that Pnacl translation failed, from the translation thread only.
75   void TranslateFailed(PP_NaClError err_code,
76                        const nacl::string& error_string);
77   // Run the LD subprocess, returning true on success.
78   // On failure, it returns false and runs the callback.
79   bool RunLdSubprocess();
80 
81 
82   // Callback to run when tasks are completed or an error has occurred.
83   pp::CompletionCallback report_translate_finished_;
84 
85   nacl::scoped_ptr<NaClThread> translate_thread_;
86 
87   // Used to guard llc_subprocess and ld_subprocess
88   struct NaClMutex subprocess_mu_;
89   nacl::scoped_ptr<NaClSubprocess> llc_subprocess_;
90   nacl::scoped_ptr<NaClSubprocess> ld_subprocess_;
91   // Used to ensure the subprocesses don't get shutdown more than once.
92   bool llc_subprocess_active_;
93   bool ld_subprocess_active_;
94 
95   bool subprocesses_aborted_;
96 
97   // Condition variable to synchronize communication with the SRPC thread.
98   // SRPC thread waits on this condvar if data_buffers_ is empty (meaning
99   // there is no bitcode to send to the translator), and the main thread
100   // appends to data_buffers_ and signals it when it receives bitcode.
101   struct NaClCondVar buffer_cond_;
102   // Mutex for buffer_cond_.
103   struct NaClMutex cond_mu_;
104   // Data buffers from FileDownloader are enqueued here to pass from the
105   // main thread to the SRPC thread. Protected by cond_mu_
106   std::deque<std::vector<char> > data_buffers_;
107   // Whether all data has been downloaded and copied to translation thread.
108   // Associated with buffer_cond_
109   bool done_;
110 
111   int64_t compile_time_;
112 
113   // Data about the translation files, owned by the coordinator
114   const std::vector<TempFile*>* obj_files_;
115   TempFile* nexe_file_;
116   nacl::DescWrapper* invalid_desc_wrapper_;
117   ErrorInfo* coordinator_error_info_;
118   PnaclResources* resources_;
119   PP_PNaClOptions* pnacl_options_;
120   nacl::string architecture_attributes_;
121   PnaclCoordinator* coordinator_;
122   Plugin* plugin_;
123  private:
124   NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread);
125 };
126 
127 }
128 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_
129