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_
11 #include "components/nacl/renderer/plugin/plugin_error.h"
12 #include "native_client/src/include/nacl_macros.h"
13 #include "native_client/src/include/nacl_scoped_ptr.h"
14 #include "native_client/src/shared/platform/nacl_sync_checked.h"
15 #include "native_client/src/shared/platform/nacl_threads.h"
16 #include "ppapi/cpp/completion_callback.h"
18 struct PP_PNaClOptions
;
28 class PnaclCoordinator
;
31 class PnaclTranslateThread
{
33 PnaclTranslateThread();
34 ~PnaclTranslateThread();
36 // Set up the state for RunCompile and RunLink. When an error is
37 // encountered, or RunLink is complete the finish_callback is run
38 // to notify the main thread.
39 void SetupState(const pp::CompletionCallback
& finish_callback
,
40 NaClSubprocess
* compiler_subprocess
,
41 NaClSubprocess
* ld_subprocess
,
42 const std::vector
<TempFile
*>* obj_files
,
45 nacl::DescWrapper
* invalid_desc_wrapper
,
46 ErrorInfo
* error_info
,
47 PP_PNaClOptions
* pnacl_options
,
48 const std::string
& architecture_attributes
,
49 PnaclCoordinator
* coordinator
);
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
);
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.
63 // Kill the llc and/or ld subprocesses. This happens by closing the command
64 // channel on the plugin side, which causes the trusted code in the nexe to
65 // exit, which will cause any pending SRPCs to error. Because this is called
66 // on the main thread, the translation thread must not use the subprocess
67 // objects without the lock, other than InvokeSrpcMethod, which does not
68 // race with service runtime shutdown.
69 void AbortSubprocesses();
71 // Send bitcode bytes to the translator. Called from the main thread.
72 void PutBytes(const void* data
, int count
);
74 // Notify the translator that the end of the bitcode stream has been reached.
75 // Called from the main thread.
78 int64_t GetCompileTime() const { return compile_time_
; }
80 // Returns true if the translation process is initiated via SetupState.
81 bool started() const { return coordinator_
!= NULL
; }
84 // Helper thread entry point for compilation. Takes a pointer to
85 // PnaclTranslateThread and calls DoCompile().
86 static void WINAPI
DoCompileThread(void* arg
);
87 // Runs the streaming compilation. Called from the helper thread.
90 // Similar to DoCompile*, but for linking.
91 static void WINAPI
DoLinkThread(void* arg
);
94 // Signal that Pnacl translation failed, from the translation thread only.
95 void TranslateFailed(PP_NaClError err_code
,
96 const std::string
& error_string
);
98 // Callback to run when compile is completed and linking can start.
99 pp::CompletionCallback compile_finished_callback_
;
101 // Callback to run when tasks are completed or an error has occurred.
102 pp::CompletionCallback report_translate_finished_
;
104 nacl::scoped_ptr
<NaClThread
> translate_thread_
;
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).
109 struct NaClMutex subprocess_mu_
;
110 // The compiler_subprocess and ld_subprocess memory is owned by the
111 // coordinator so we do not delete them. However, the main thread delegates
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 otherwise accessing the service_runtime component of the subprocess.
117 // There are some accesses to the subprocesses without locks held
118 // (invoking srpc_client methods -- in contrast to using the service_runtime).
119 NaClSubprocess
* compiler_subprocess_
;
120 NaClSubprocess
* ld_subprocess_
;
121 bool compiler_subprocess_active_
;
122 bool ld_subprocess_active_
;
124 // Condition variable to synchronize communication with the SRPC thread.
125 // SRPC thread waits on this condvar if data_buffers_ is empty (meaning
126 // there is no bitcode to send to the translator), and the main thread
127 // appends to data_buffers_ and signals it when it receives bitcode.
128 struct NaClCondVar buffer_cond_
;
129 // Mutex for buffer_cond_.
130 struct NaClMutex cond_mu_
;
131 // Data buffers from FileDownloader are enqueued here to pass from the
132 // main thread to the SRPC thread. Protected by cond_mu_
133 std::deque
<std::vector
<char> > data_buffers_
;
134 // Whether all data has been downloaded and copied to translation thread.
135 // Associated with buffer_cond_
138 int64_t compile_time_
;
140 // Data about the translation files, owned by the coordinator
141 const std::vector
<TempFile
*>* obj_files_
;
143 TempFile
* nexe_file_
;
144 nacl::DescWrapper
* invalid_desc_wrapper_
;
145 ErrorInfo
* coordinator_error_info_
;
146 PP_PNaClOptions
* pnacl_options_
;
147 std::string architecture_attributes_
;
148 PnaclCoordinator
* coordinator_
;
150 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread
);
154 #endif // COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_