File: pnacl_translate_thread.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (184 lines) | stat: -rw-r--r-- 7,286 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_
#define COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_

#include <stdint.h>

#include <memory>
#include <vector>

#include "base/containers/circular_deque.h"
#include "base/files/file.h"
#include "base/memory/raw_ptr.h"
#include "base/synchronization/condition_variable.h"
#include "base/synchronization/lock.h"
#include "base/threading/simple_thread.h"
#include "components/nacl/renderer/plugin/plugin_error.h"
#include "ppapi/cpp/completion_callback.h"
#include "ppapi/proxy/serialized_handle.h"

struct PP_PNaClOptions;

namespace plugin {

class NaClSubprocess;
class PnaclCoordinator;

class PnaclTranslateThread {
 public:
  PnaclTranslateThread();

  PnaclTranslateThread(const PnaclTranslateThread&) = delete;
  PnaclTranslateThread& operator=(const PnaclTranslateThread&) = delete;

  ~PnaclTranslateThread();

  // Set up the state for RunCompile and RunLink. When an error is
  // encountered, or RunLink is complete the finish_callback is run
  // to notify the main thread.
  void SetupState(const pp::CompletionCallback& finish_callback,
                  NaClSubprocess* compiler_subprocess,
                  NaClSubprocess* ld_subprocess,
                  std::vector<base::File>* obj_files,
                  int num_threads,
                  base::File* nexe_file,
                  ErrorInfo* error_info,
                  PP_PNaClOptions* pnacl_options,
                  const std::string& architecture_attributes,
                  PnaclCoordinator* coordinator);

  // Create a compile thread and run/command the compiler_subprocess.
  // It will continue to run and consume data as it is passed in with PutBytes.
  // On success, runs compile_finished_callback.
  // On error, runs finish_callback.
  // The compiler_subprocess must already be loaded.
  void RunCompile(const pp::CompletionCallback& compile_finished_callback);

  // Create a link thread and run/command the ld_subprocess.
  // On completion (success or error), runs finish_callback.
  // The ld_subprocess must already be loaded.
  void RunLink();

  // Kill the llc and/or ld subprocesses. This happens by closing the command
  // channel on the plugin side, which causes the trusted code in the nexe to
  // exit, which will cause any pending SRPCs to error. Because this is called
  // on the main thread, the translation thread must not use the subprocess
  // objects without the lock, other than InvokeSrpcMethod, which does not
  // race with service runtime shutdown.
  void AbortSubprocesses();

  // Send bitcode bytes to the translator. Called from the main thread.
  void PutBytes(const void* data, int count);

  // Notify the translator that the end of the bitcode stream has been reached.
  // Called from the main thread.
  void EndStream();

  int64_t GetCompileTime() const { return compile_time_; }

  // Returns true if the translation process is initiated via SetupState.
  bool started() const { return !!coordinator_; }

 private:
  ppapi::proxy::SerializedHandle GetHandleForSubprocess(base::File* file,
                                                        int32_t open_flags);

  // Runs the streaming compilation. Called from the helper thread.
  void DoCompile();
  // Similar to DoCompile(), but for linking.
  void DoLink();

  class CompileThread : public base::SimpleThread {
   public:
    CompileThread(PnaclTranslateThread* obj)
      : base::SimpleThread("pnacl_compile"), pnacl_translate_thread_(obj) {}

    CompileThread(const CompileThread&) = delete;
    CompileThread& operator=(const CompileThread&) = delete;

   private:
    raw_ptr<PnaclTranslateThread> pnacl_translate_thread_;
    void Run() override;
  };

  class LinkThread : public base::SimpleThread {
   public:
    LinkThread(PnaclTranslateThread* obj)
      : base::SimpleThread("pnacl_link"), pnacl_translate_thread_(obj) {}

    LinkThread(const LinkThread&) = delete;
    LinkThread& operator=(const LinkThread&) = delete;

   private:
    raw_ptr<PnaclTranslateThread> pnacl_translate_thread_;
    void Run() override;
  };

  // Signal that Pnacl translation failed, from the translation thread only.
  void TranslateFailed(PP_NaClError err_code,
                       const std::string& error_string);

  // Callback to run when compile is completed and linking can start.
  pp::CompletionCallback compile_finished_callback_;

  // Callback to run when tasks are completed or an error has occurred.
  pp::CompletionCallback report_translate_finished_;

  std::unique_ptr<base::SimpleThread> translate_thread_;

  // Used to guard compiler_subprocess, ld_subprocess,
  // compiler_subprocess_active_, and ld_subprocess_active_
  // (touched by the main thread and the translate thread).
  base::Lock subprocess_mu_;
  // The compiler_subprocess and ld_subprocess memory is owned by the
  // coordinator so we do not delete them. However, the main thread delegates
  // shutdown to this thread, since this thread may still be accessing the
  // subprocesses. The *_subprocess_active flags indicate which subprocesses
  // are active to ensure the subprocesses don't get shutdown more than once.
  // The subprocess_mu_ must be held when shutting down the subprocesses
  // or otherwise accessing the service_runtime component of the subprocess.
  // There are some accesses to the subprocesses without locks held
  // (invoking srpc_client methods -- in contrast to using the service_runtime).
  raw_ptr<NaClSubprocess> compiler_subprocess_;
  raw_ptr<NaClSubprocess> ld_subprocess_;
  bool compiler_subprocess_active_;
  bool ld_subprocess_active_;

  // Mutex for buffer_cond_.
  base::Lock cond_mu_;
  // Condition variable to synchronize communication with the SRPC thread.
  // SRPC thread waits on this condvar if data_buffers_ is empty (meaning
  // there is no bitcode to send to the translator), and the main thread
  // appends to data_buffers_ and signals it when it receives bitcode.
  base::ConditionVariable buffer_cond_;
  // Data buffers from FileDownloader are enqueued here to pass from the
  // main thread to the SRPC thread. Protected by cond_mu_
  base::circular_deque<std::string> data_buffers_;
  // Whether all data has been downloaded and copied to translation thread.
  // Associated with buffer_cond_
  bool done_;

  int64_t compile_time_;

  // Data about the translation files, owned by the coordinator
  raw_ptr<std::vector<base::File>> obj_files_;
  int num_threads_;
  raw_ptr<base::File> nexe_file_;
  raw_ptr<ErrorInfo> coordinator_error_info_;
  raw_ptr<PP_PNaClOptions> pnacl_options_;
  std::string architecture_attributes_;
  raw_ptr<PnaclCoordinator> coordinator_;

  // These IPC::SyncChannels can only be used and freed by the parent thread.
  std::unique_ptr<IPC::SyncChannel> compiler_channel_;
  std::unique_ptr<IPC::SyncChannel> ld_channel_;
  // These IPC::SyncMessageFilters can be used by the child thread.
  scoped_refptr<IPC::SyncMessageFilter> compiler_channel_filter_;
  scoped_refptr<IPC::SyncMessageFilter> ld_channel_filter_;
};

}
#endif // COMPONENTS_NACL_RENDERER_PLUGIN_PNACL_TRANSLATE_THREAD_H_