Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / components / nacl / renderer / plugin / pnacl_translate_thread.h
blob3c6b622a0ce25376cd1e4c267e5aa288d9ca5338
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.
5 #ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_
6 #define COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_
8 #include <deque>
9 #include <vector>
11 #include "components/nacl/renderer/plugin/plugin_error.h"
12 #include "components/nacl/renderer/plugin/service_runtime.h"
13 #include "native_client/src/include/nacl_macros.h"
14 #include "native_client/src/include/nacl_scoped_ptr.h"
15 #include "native_client/src/shared/platform/nacl_sync_checked.h"
16 #include "native_client/src/shared/platform/nacl_threads.h"
17 #include "ppapi/cpp/completion_callback.h"
19 struct PP_PNaClOptions;
21 namespace nacl {
22 class DescWrapper;
26 namespace plugin {
28 class NaClSubprocess;
29 class Plugin;
30 class PnaclCoordinator;
31 class PnaclResources;
32 class TempFile;
34 class PnaclTranslateThread {
35 public:
36 PnaclTranslateThread();
37 ~PnaclTranslateThread();
39 // Start the translation process. It will continue to run and consume data
40 // as it is passed in with PutBytes.
41 void RunTranslate(const pp::CompletionCallback& finish_callback,
42 const std::vector<TempFile*>* obj_files,
43 int num_threads,
44 TempFile* nexe_file,
45 nacl::DescWrapper* invalid_desc_wrapper,
46 ErrorInfo* error_info,
47 PnaclResources* resources,
48 PP_PNaClOptions* pnacl_options,
49 const std::string& architecture_attributes,
50 PnaclCoordinator* coordinator,
51 Plugin* plugin);
53 // Kill the llc and/or ld subprocesses. This happens by closing the command
54 // channel on the plugin side, which causes the trusted code in the nexe to
55 // exit, which will cause any pending SRPCs to error. Because this is called
56 // on the main thread, the translation thread must not use the subprocess
57 // objects without the lock, other than InvokeSrpcMethod, which does not
58 // race with service runtime shutdown.
59 void AbortSubprocesses();
61 // Send bitcode bytes to the translator. Called from the main thread.
62 void PutBytes(const void* data, int count);
64 // Notify the translator that the end of the bitcode stream has been reached.
65 // Called from the main thread.
66 void EndStream();
68 int64_t GetCompileTime() const { return compile_time_; }
70 // Returns true if RunTranslate() has been called, false otherwise.
71 bool started() const { return plugin_ != NULL; }
73 private:
74 // Helper thread entry point for translation. Takes a pointer to
75 // PnaclTranslateThread and calls DoTranslate().
76 static void WINAPI DoTranslateThread(void* arg);
77 // Runs the streaming translation. Called from the helper thread.
78 void DoTranslate() ;
79 // Signal that Pnacl translation failed, from the translation thread only.
80 void TranslateFailed(PP_NaClError err_code,
81 const std::string& error_string);
82 // Run the LD subprocess, returning true on success.
83 // On failure, it returns false and runs the callback.
84 bool RunLdSubprocess();
87 // Callback to run when tasks are completed or an error has occurred.
88 pp::CompletionCallback report_translate_finished_;
90 nacl::scoped_ptr<NaClThread> translate_thread_;
92 // Used to guard compiler_subprocess and ld_subprocess
93 struct NaClMutex subprocess_mu_;
94 nacl::scoped_ptr<NaClSubprocess> compiler_subprocess_;
95 nacl::scoped_ptr<NaClSubprocess> ld_subprocess_;
96 // Used to ensure the subprocesses don't get shutdown more than once.
97 bool compiler_subprocess_active_;
98 bool ld_subprocess_active_;
100 bool subprocesses_aborted_;
102 // Condition variable to synchronize communication with the SRPC thread.
103 // SRPC thread waits on this condvar if data_buffers_ is empty (meaning
104 // there is no bitcode to send to the translator), and the main thread
105 // appends to data_buffers_ and signals it when it receives bitcode.
106 struct NaClCondVar buffer_cond_;
107 // Mutex for buffer_cond_.
108 struct NaClMutex cond_mu_;
109 // Data buffers from FileDownloader are enqueued here to pass from the
110 // main thread to the SRPC thread. Protected by cond_mu_
111 std::deque<std::vector<char> > data_buffers_;
112 // Whether all data has been downloaded and copied to translation thread.
113 // Associated with buffer_cond_
114 bool done_;
116 int64_t compile_time_;
118 // Data about the translation files, owned by the coordinator
119 const std::vector<TempFile*>* obj_files_;
120 int num_threads_;
121 TempFile* nexe_file_;
122 nacl::DescWrapper* invalid_desc_wrapper_;
123 ErrorInfo* coordinator_error_info_;
124 PnaclResources* resources_;
125 PP_PNaClOptions* pnacl_options_;
126 std::string architecture_attributes_;
127 PnaclCoordinator* coordinator_;
128 Plugin* plugin_;
129 private:
130 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread);
134 #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_