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