OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ | 5 #ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ |
6 #define COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ | 6 #define COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "components/nacl/renderer/plugin/plugin_error.h" | 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" | 12 #include "native_client/src/include/nacl_macros.h" |
14 #include "native_client/src/include/nacl_scoped_ptr.h" | 13 #include "native_client/src/include/nacl_scoped_ptr.h" |
15 #include "native_client/src/shared/platform/nacl_sync_checked.h" | 14 #include "native_client/src/shared/platform/nacl_sync_checked.h" |
16 #include "native_client/src/shared/platform/nacl_threads.h" | 15 #include "native_client/src/shared/platform/nacl_threads.h" |
17 #include "ppapi/cpp/completion_callback.h" | 16 #include "ppapi/cpp/completion_callback.h" |
18 | 17 |
19 struct PP_PNaClOptions; | 18 struct PP_PNaClOptions; |
20 | 19 |
21 namespace nacl { | 20 namespace nacl { |
22 class DescWrapper; | 21 class DescWrapper; |
23 } | 22 } |
24 | 23 |
25 | 24 |
26 namespace plugin { | 25 namespace plugin { |
27 | 26 |
28 class NaClSubprocess; | 27 class NaClSubprocess; |
29 class Plugin; | |
30 class PnaclCoordinator; | 28 class PnaclCoordinator; |
31 class PnaclResources; | |
32 class TempFile; | 29 class TempFile; |
33 | 30 |
34 class PnaclTranslateThread { | 31 class PnaclTranslateThread { |
35 public: | 32 public: |
36 PnaclTranslateThread(); | 33 PnaclTranslateThread(); |
37 ~PnaclTranslateThread(); | 34 ~PnaclTranslateThread(); |
38 | 35 |
39 // Start the translation process. It will continue to run and consume data | 36 // Set up the state for RunCompile and RunLink. When an error is |
40 // as it is passed in with PutBytes. | 37 // encountered, or RunLink is complete the finish_callback is run |
41 void RunTranslate(const pp::CompletionCallback& finish_callback, | 38 // to notify the main thread. |
42 const std::vector<TempFile*>* obj_files, | 39 void SetupState(const pp::CompletionCallback& finish_callback, |
43 int num_threads, | 40 NaClSubprocess* compiler_subprocess, |
44 TempFile* nexe_file, | 41 NaClSubprocess* ld_subprocess, |
45 nacl::DescWrapper* invalid_desc_wrapper, | 42 const std::vector<TempFile*>* obj_files, |
46 ErrorInfo* error_info, | 43 int num_threads, |
47 PnaclResources* resources, | 44 TempFile* nexe_file, |
48 PP_PNaClOptions* pnacl_options, | 45 nacl::DescWrapper* invalid_desc_wrapper, |
49 const std::string& architecture_attributes, | 46 ErrorInfo* error_info, |
50 PnaclCoordinator* coordinator, | 47 PP_PNaClOptions* pnacl_options, |
51 Plugin* plugin); | 48 const std::string& architecture_attributes, |
| 49 PnaclCoordinator* coordinator); |
| 50 |
| 51 // Create a compile thread and run/command the compiler_subprocess. |
| 52 // It will continue to run and consume data as it is passed in with PutBytes. |
| 53 // On success, runs compile_finished_callback. |
| 54 // On error, runs finish_callback. |
| 55 // The compiler_subprocess must already be loaded. |
| 56 void RunCompile(const pp::CompletionCallback& compile_finished_callback); |
| 57 |
| 58 // Create a link thread and run/command the ld_subprocess. |
| 59 // On completion (success or error), runs finish_callback. |
| 60 // The ld_subprocess must already be loaded. |
| 61 void RunLink(); |
52 | 62 |
53 // Kill the llc and/or ld subprocesses. This happens by closing the command | 63 // 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 | 64 // 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 | 65 // 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 | 66 // on the main thread, the translation thread must not use the subprocess |
57 // objects without the lock, other than InvokeSrpcMethod, which does not | 67 // objects without the lock, other than InvokeSrpcMethod, which does not |
58 // race with service runtime shutdown. | 68 // race with service runtime shutdown. |
59 void AbortSubprocesses(); | 69 void AbortSubprocesses(); |
60 | 70 |
61 // Send bitcode bytes to the translator. Called from the main thread. | 71 // Send bitcode bytes to the translator. Called from the main thread. |
62 void PutBytes(const void* data, int count); | 72 void PutBytes(const void* data, int count); |
63 | 73 |
64 // Notify the translator that the end of the bitcode stream has been reached. | 74 // Notify the translator that the end of the bitcode stream has been reached. |
65 // Called from the main thread. | 75 // Called from the main thread. |
66 void EndStream(); | 76 void EndStream(); |
67 | 77 |
68 int64_t GetCompileTime() const { return compile_time_; } | 78 int64_t GetCompileTime() const { return compile_time_; } |
69 | 79 |
70 // Returns true if RunTranslate() has been called, false otherwise. | 80 // Returns true if the translation process is initiated via SetupState. |
71 bool started() const { return plugin_ != NULL; } | 81 bool started() const { return coordinator_ != NULL; } |
72 | 82 |
73 private: | 83 private: |
74 // Helper thread entry point for translation. Takes a pointer to | 84 // Helper thread entry point for compilation. Takes a pointer to |
75 // PnaclTranslateThread and calls DoTranslate(). | 85 // PnaclTranslateThread and calls DoCompile(). |
76 static void WINAPI DoTranslateThread(void* arg); | 86 static void WINAPI DoCompileThread(void* arg); |
77 // Runs the streaming translation. Called from the helper thread. | 87 // Runs the streaming compilation. Called from the helper thread. |
78 void DoTranslate() ; | 88 void DoCompile(); |
| 89 |
| 90 // Similar to DoCompile*, but for linking. |
| 91 static void WINAPI DoLinkThread(void* arg); |
| 92 void DoLink(); |
| 93 |
79 // Signal that Pnacl translation failed, from the translation thread only. | 94 // Signal that Pnacl translation failed, from the translation thread only. |
80 void TranslateFailed(PP_NaClError err_code, | 95 void TranslateFailed(PP_NaClError err_code, |
81 const std::string& error_string); | 96 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(); | |
85 | 97 |
| 98 // Callback to run when compile is completed and linking can start. |
| 99 pp::CompletionCallback compile_finished_callback_; |
86 | 100 |
87 // Callback to run when tasks are completed or an error has occurred. | 101 // Callback to run when tasks are completed or an error has occurred. |
88 pp::CompletionCallback report_translate_finished_; | 102 pp::CompletionCallback report_translate_finished_; |
89 | 103 |
90 nacl::scoped_ptr<NaClThread> translate_thread_; | 104 nacl::scoped_ptr<NaClThread> translate_thread_; |
91 | 105 |
92 // Used to guard compiler_subprocess and ld_subprocess | 106 // Used to guard compiler_subprocess, ld_subprocess, |
| 107 // compiler_subprocess_active_, and ld_subprocess_active_ |
| 108 // (touched by the main thread and the translate thread). |
93 struct NaClMutex subprocess_mu_; | 109 struct NaClMutex subprocess_mu_; |
94 nacl::scoped_ptr<NaClSubprocess> compiler_subprocess_; | 110 // The compiler_subprocess and ld_subprocess memory is owned by the |
95 nacl::scoped_ptr<NaClSubprocess> ld_subprocess_; | 111 // coordinator so we do not delete them. However, the main thread delegates |
96 // Used to ensure the subprocesses don't get shutdown more than once. | 112 // shutdown to this thread, since this thread may still be accessing the |
| 113 // subprocesses. The *_subprocess_active flags indicate which subprocesses |
| 114 // are active to ensure the subprocesses don't get shutdown more than once. |
| 115 // The subprocess_mu_ must be held when shutting down the subprocesses |
| 116 // or checking if it's already shut down (via the active flags). |
| 117 // There are some accesses to the subprocesses without locks held |
| 118 // (invoking SRPC methods client). |
| 119 NaClSubprocess* compiler_subprocess_; |
| 120 NaClSubprocess* ld_subprocess_; |
97 bool compiler_subprocess_active_; | 121 bool compiler_subprocess_active_; |
98 bool ld_subprocess_active_; | 122 bool ld_subprocess_active_; |
99 | 123 |
100 bool subprocesses_aborted_; | |
101 | |
102 // Condition variable to synchronize communication with the SRPC thread. | 124 // Condition variable to synchronize communication with the SRPC thread. |
103 // SRPC thread waits on this condvar if data_buffers_ is empty (meaning | 125 // 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 | 126 // 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. | 127 // appends to data_buffers_ and signals it when it receives bitcode. |
106 struct NaClCondVar buffer_cond_; | 128 struct NaClCondVar buffer_cond_; |
107 // Mutex for buffer_cond_. | 129 // Mutex for buffer_cond_. |
108 struct NaClMutex cond_mu_; | 130 struct NaClMutex cond_mu_; |
109 // Data buffers from FileDownloader are enqueued here to pass from the | 131 // Data buffers from FileDownloader are enqueued here to pass from the |
110 // main thread to the SRPC thread. Protected by cond_mu_ | 132 // main thread to the SRPC thread. Protected by cond_mu_ |
111 std::deque<std::vector<char> > data_buffers_; | 133 std::deque<std::vector<char> > data_buffers_; |
112 // Whether all data has been downloaded and copied to translation thread. | 134 // Whether all data has been downloaded and copied to translation thread. |
113 // Associated with buffer_cond_ | 135 // Associated with buffer_cond_ |
114 bool done_; | 136 bool done_; |
115 | 137 |
116 int64_t compile_time_; | 138 int64_t compile_time_; |
117 | 139 |
118 // Data about the translation files, owned by the coordinator | 140 // Data about the translation files, owned by the coordinator |
119 const std::vector<TempFile*>* obj_files_; | 141 const std::vector<TempFile*>* obj_files_; |
120 int num_threads_; | 142 int num_threads_; |
121 TempFile* nexe_file_; | 143 TempFile* nexe_file_; |
122 nacl::DescWrapper* invalid_desc_wrapper_; | 144 nacl::DescWrapper* invalid_desc_wrapper_; |
123 ErrorInfo* coordinator_error_info_; | 145 ErrorInfo* coordinator_error_info_; |
124 PnaclResources* resources_; | |
125 PP_PNaClOptions* pnacl_options_; | 146 PP_PNaClOptions* pnacl_options_; |
126 std::string architecture_attributes_; | 147 std::string architecture_attributes_; |
127 PnaclCoordinator* coordinator_; | 148 PnaclCoordinator* coordinator_; |
128 Plugin* plugin_; | |
129 private: | 149 private: |
130 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread); | 150 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread); |
131 }; | 151 }; |
132 | 152 |
133 } | 153 } |
134 #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ | 154 #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_ |
OLD | NEW |