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 NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_ | 5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_ |
6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_ | 6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_ |
7 | 7 |
| 8 #include <deque> |
| 9 #include <vector> |
| 10 |
8 #include "native_client/src/include/nacl_macros.h" | 11 #include "native_client/src/include/nacl_macros.h" |
9 #include "native_client/src/include/nacl_scoped_ptr.h" | 12 #include "native_client/src/include/nacl_scoped_ptr.h" |
10 #include "native_client/src/include/nacl_string.h" | 13 #include "native_client/src/include/nacl_string.h" |
11 #include "native_client/src/shared/platform/nacl_threads.h" | 14 #include "native_client/src/shared/platform/nacl_threads.h" |
12 #include "native_client/src/shared/platform/nacl_sync_checked.h" | 15 #include "native_client/src/shared/platform/nacl_sync_checked.h" |
13 #include "native_client/src/trusted/plugin/plugin_error.h" | 16 #include "native_client/src/trusted/plugin/plugin_error.h" |
14 #include "native_client/src/trusted/plugin/service_runtime.h" | 17 #include "native_client/src/trusted/plugin/service_runtime.h" |
15 | 18 |
16 #include "ppapi/cpp/completion_callback.h" | 19 #include "ppapi/cpp/completion_callback.h" |
17 | 20 |
18 namespace nacl { | 21 namespace nacl { |
19 class DescWrapper; | 22 class DescWrapper; |
20 } | 23 } |
21 | 24 |
22 | 25 |
23 namespace plugin { | 26 namespace plugin { |
24 | 27 |
25 class LocalTempFile; | 28 class LocalTempFile; |
26 class Manifest; | 29 class Manifest; |
27 class NaClSubprocess; | 30 class NaClSubprocess; |
28 class Plugin; | 31 class Plugin; |
29 class PnaclResources; | 32 class PnaclResources; |
30 class TempFile; | 33 class TempFile; |
31 | 34 |
32 class PnaclTranslateThread { | 35 class PnaclTranslateThread { |
33 public: | 36 public: |
34 PnaclTranslateThread(); | 37 PnaclTranslateThread(); |
35 virtual ~PnaclTranslateThread(); | 38 ~PnaclTranslateThread(); |
36 | 39 |
37 virtual void RunTranslate(const pp::CompletionCallback& finish_callback, | 40 // Start the translation process. It will continue to run and consume data |
| 41 // as it is passed in with PutBytes. |
| 42 void RunTranslate(const pp::CompletionCallback& finish_callback, |
38 const Manifest* manifest, | 43 const Manifest* manifest, |
39 const Manifest* ld_manifest, | 44 const Manifest* ld_manifest, |
40 TempFile* obj_file, | 45 TempFile* obj_file, |
41 LocalTempFile* nexe_file, | 46 LocalTempFile* nexe_file, |
42 ErrorInfo* error_info, | 47 ErrorInfo* error_info, |
43 PnaclResources* resources, | 48 PnaclResources* resources, |
44 Plugin* plugin) = 0; | 49 Plugin* plugin); |
| 50 |
| 51 // Signal the translate thread and subprocesses that they should stop. |
| 52 void SetSubprocessesShouldDie(); |
| 53 |
| 54 // Send bitcode bytes to the translator. Called from the main thread. |
| 55 void PutBytes(std::vector<char>* data, int count); |
| 56 |
| 57 private: |
45 // Returns true if the translate thread and subprocesses should stop. | 58 // Returns true if the translate thread and subprocesses should stop. |
46 bool SubprocessesShouldDie(); | 59 bool SubprocessesShouldDie(); |
47 // Signal the translate thread and subprocesses that they should stop. | |
48 virtual void SetSubprocessesShouldDie(); | |
49 | 60 |
50 protected: | |
51 // Starts an individual llc or ld subprocess used for translation. | 61 // Starts an individual llc or ld subprocess used for translation. |
52 NaClSubprocess* StartSubprocess(const nacl::string& url, | 62 NaClSubprocess* StartSubprocess(const nacl::string& url, |
53 const Manifest* manifest, | 63 const Manifest* manifest, |
54 ErrorInfo* error_info); | 64 ErrorInfo* error_info); |
55 // Helper thread entry point for translation. Takes a pointer to | 65 // Helper thread entry point for translation. Takes a pointer to |
56 // PnaclTranslateThread and calls DoTranslate(). | 66 // PnaclTranslateThread and calls DoTranslate(). |
57 static void WINAPI DoTranslateThread(void* arg); | 67 static void WINAPI DoTranslateThread(void* arg); |
58 // Runs the SRPCs that control translation. Called from the helper thread. | 68 // Runs the streaming translation. Called from the helper thread. |
59 virtual void DoTranslate() = 0; | 69 void DoTranslate() ; |
60 // Signal that Pnacl translation failed, from the translation thread only. | 70 // Signal that Pnacl translation failed, from the translation thread only. |
61 void TranslateFailed(const nacl::string& error_string); | 71 void TranslateFailed(const nacl::string& error_string); |
62 // Run the LD subprocess, returning true on success | 72 // Run the LD subprocess, returning true on success |
63 bool RunLdSubprocess(int is_shared_library, | 73 bool RunLdSubprocess(int is_shared_library, |
64 const nacl::string& soname, | 74 const nacl::string& soname, |
65 const nacl::string& lib_dependencies); | 75 const nacl::string& lib_dependencies); |
66 | 76 |
67 // Register a reverse service interface which will be shutdown if the | 77 // Register a reverse service interface which will be shutdown if the |
68 // plugin is shutdown. The reverse service pointer must be available on the | 78 // plugin is shutdown. The reverse service pointer must be available on the |
69 // main thread because the translation thread could be blocked on SRPC | 79 // main thread because the translation thread could be blocked on SRPC |
70 // waiting for the translator, which could be waiting on a reverse | 80 // waiting for the translator, which could be waiting on a reverse |
71 // service call. | 81 // service call. |
72 // (see also the comments in Plugin::~Plugin about ShutdownSubprocesses, | 82 // (see also the comments in Plugin::~Plugin about ShutdownSubprocesses, |
73 // but that only handles the main nexe and not the translator nexes.) | 83 // but that only handles the main nexe and not the translator nexes.) |
74 void RegisterReverseInterface(PluginReverseInterface *interface); | 84 void RegisterReverseInterface(PluginReverseInterface *interface); |
75 | 85 |
76 // Callback to run when tasks are completed or an error has occurred. | 86 // Callback to run when tasks are completed or an error has occurred. |
77 pp::CompletionCallback report_translate_finished_; | 87 pp::CompletionCallback report_translate_finished_; |
78 // True if the translation thread and related subprocesses should exit. | 88 // True if the translation thread and related subprocesses should exit. |
79 bool subprocesses_should_die_; | 89 bool subprocesses_should_die_; |
80 // Used to guard and publish subprocesses_should_die_. | 90 // Used to guard and publish subprocesses_should_die_. |
81 struct NaClMutex subprocess_mu_; | 91 struct NaClMutex subprocess_mu_; |
82 | 92 |
83 nacl::scoped_ptr<NaClThread> translate_thread_; | 93 nacl::scoped_ptr<NaClThread> translate_thread_; |
84 | 94 |
85 // Reverse interface to shutdown on SetSubprocessesShouldDie | 95 // Reverse interface to shutdown on SetSubprocessesShouldDie |
86 PluginReverseInterface* current_rev_interface_; | 96 PluginReverseInterface* current_rev_interface_; |
87 | 97 |
| 98 |
| 99 |
| 100 // Condition variable to synchronize communication with the SRPC thread. |
| 101 // SRPC thread waits on this condvar if data_buffers_ is empty (meaning |
| 102 // there is no bitcode to send to the translator), and the main thread |
| 103 // appends to data_buffers_ and signals it when it receives bitcode. |
| 104 struct NaClCondVar buffer_cond_; |
| 105 // Mutex for buffer_cond_. |
| 106 struct NaClMutex cond_mu_; |
| 107 // Data buffers from FileDownloader are enqueued here to pass from the |
| 108 // main thread to the SRPC thread. Protected by cond_mu_ |
| 109 std::deque<std::vector<char> > data_buffers_; |
| 110 // Whether all data has been downloaded and copied to translation thread. |
| 111 // Associated with buffer_cond_ |
| 112 bool done_; |
| 113 |
88 // Data about the translation files, owned by the coordinator | 114 // Data about the translation files, owned by the coordinator |
89 const Manifest* manifest_; | 115 const Manifest* manifest_; |
90 const Manifest* ld_manifest_; | 116 const Manifest* ld_manifest_; |
91 TempFile* obj_file_; | 117 TempFile* obj_file_; |
92 LocalTempFile* nexe_file_; | 118 LocalTempFile* nexe_file_; |
93 ErrorInfo* coordinator_error_info_; | 119 ErrorInfo* coordinator_error_info_; |
94 PnaclResources* resources_; | 120 PnaclResources* resources_; |
95 Plugin* plugin_; | 121 Plugin* plugin_; |
96 private: | 122 private: |
97 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread); | 123 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread); |
98 }; | 124 }; |
99 | 125 |
100 } | 126 } |
101 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_ | 127 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_ |
OLD | NEW |