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 NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_
6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_
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"
17 #include "ppapi/cpp/completion_callback.h"
19 #include "ppapi/native_client/src/trusted/plugin/plugin_error.h"
20 #include "ppapi/native_client/src/trusted/plugin/service_runtime.h"
32 class PnaclCoordinator
;
37 struct PnaclTimeStats
{
38 int64_t pnacl_llc_load_time
;
39 int64_t pnacl_compile_time
;
40 int64_t pnacl_ld_load_time
;
41 int64_t pnacl_link_time
;
44 class PnaclTranslateThread
{
46 PnaclTranslateThread();
47 ~PnaclTranslateThread();
49 // Start the translation process. It will continue to run and consume data
50 // as it is passed in with PutBytes.
51 void RunTranslate(const pp::CompletionCallback
& finish_callback
,
52 const Manifest
* manifest
,
55 ErrorInfo
* error_info
,
56 PnaclResources
* resources
,
57 PnaclOptions
* pnacl_options
,
58 PnaclCoordinator
* coordinator
,
61 // Kill the llc and/or ld subprocesses. This happens by closing the command
62 // channel on the plugin side, which causes the trusted code in the nexe to
63 // exit, which will cause any pending SRPCs to error. Because this is called
64 // on the main thread, the translation thread must not use the subprocess
65 // objects without the lock, other than InvokeSrpcMethod, which does not
66 // race with service runtime shutdown.
67 void AbortSubprocesses();
69 // Send bitcode bytes to the translator. Called from the main thread.
70 void PutBytes(std::vector
<char>* data
, int count
);
72 const PnaclTimeStats
& GetTimeStats() const { return time_stats_
; }
75 // Starts an individual llc or ld subprocess used for translation.
76 NaClSubprocess
* StartSubprocess(const nacl::string
& url
,
77 const Manifest
* manifest
,
78 ErrorInfo
* error_info
);
79 // Helper thread entry point for translation. Takes a pointer to
80 // PnaclTranslateThread and calls DoTranslate().
81 static void WINAPI
DoTranslateThread(void* arg
);
82 // Runs the streaming translation. Called from the helper thread.
84 // Signal that Pnacl translation failed, from the translation thread only.
85 void TranslateFailed(enum PluginErrorCode err_code
,
86 const nacl::string
& error_string
);
87 // Run the LD subprocess, returning true on success
88 bool RunLdSubprocess(int is_shared_library
,
89 const nacl::string
& soname
,
90 const nacl::string
& lib_dependencies
);
93 // Callback to run when tasks are completed or an error has occurred.
94 pp::CompletionCallback report_translate_finished_
;
96 nacl::scoped_ptr
<NaClThread
> translate_thread_
;
98 // Used to guard llc_subprocess and ld_subprocess
99 struct NaClMutex subprocess_mu_
;
100 nacl::scoped_ptr
<NaClSubprocess
> llc_subprocess_
;
101 nacl::scoped_ptr
<NaClSubprocess
> ld_subprocess_
;
102 // Used to ensure the subprocesses don't get shutdown more than once.
103 bool llc_subprocess_active_
;
104 bool ld_subprocess_active_
;
106 // Condition variable to synchronize communication with the SRPC thread.
107 // SRPC thread waits on this condvar if data_buffers_ is empty (meaning
108 // there is no bitcode to send to the translator), and the main thread
109 // appends to data_buffers_ and signals it when it receives bitcode.
110 struct NaClCondVar buffer_cond_
;
111 // Mutex for buffer_cond_.
112 struct NaClMutex cond_mu_
;
113 // Data buffers from FileDownloader are enqueued here to pass from the
114 // main thread to the SRPC thread. Protected by cond_mu_
115 std::deque
<std::vector
<char> > data_buffers_
;
116 // Whether all data has been downloaded and copied to translation thread.
117 // Associated with buffer_cond_
120 PnaclTimeStats time_stats_
;
122 // Data about the translation files, owned by the coordinator
123 const Manifest
* manifest_
;
125 TempFile
* nexe_file_
;
126 ErrorInfo
* coordinator_error_info_
;
127 PnaclResources
* resources_
;
128 PnaclOptions
* pnacl_options_
;
129 PnaclCoordinator
* coordinator_
;
132 NACL_DISALLOW_COPY_AND_ASSIGN(PnaclTranslateThread
);
136 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_PNACL_TRANSLATE_THREAD_H_