Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2017-09-01 18:03:41 +0300
committerAnna Henningsen <anna@addaleax.net>2018-06-06 20:43:52 +0300
commit0df031acadcc6490379d72676203a980c8d60592 (patch)
tree3f49864e72b0193ea9af937874f62c6316877ec4 /src/node_worker.h
parent8939f36630bd718fc0b0b8557cf7f2ed9ecab312 (diff)
worker: initial implementation
Implement multi-threading support for most of the API. Thanks to Stephen Belanger for reviewing this change in its original form, to Olivia Hugger for reviewing the documentation and some of the tests coming along with it, and to Alexey Orlenko and Timothy Gu for reviewing other parts of the tests. Refs: https://github.com/ayojs/ayo/pull/110 Refs: https://github.com/ayojs/ayo/pull/114 Refs: https://github.com/ayojs/ayo/pull/117 PR-URL: https://github.com/nodejs/node/pull/20876 Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com> Reviewed-By: John-David Dalton <john.david.dalton@gmail.com> Reviewed-By: Gus Caplan <me@gus.host>
Diffstat (limited to 'src/node_worker.h')
-rw-r--r--src/node_worker.h83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/node_worker.h b/src/node_worker.h
new file mode 100644
index 00000000000..0a98d2f11ef
--- /dev/null
+++ b/src/node_worker.h
@@ -0,0 +1,83 @@
+#ifndef SRC_NODE_WORKER_H_
+#define SRC_NODE_WORKER_H_
+
+#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
+
+#include "node_messaging.h"
+#include <unordered_map>
+
+namespace node {
+namespace worker {
+
+// A worker thread, as represented in its parent thread.
+class Worker : public AsyncWrap {
+ public:
+ Worker(Environment* env, v8::Local<v8::Object> wrap);
+ ~Worker();
+
+ // Run the worker. This is only called from the worker thread.
+ void Run();
+
+ // Forcibly exit the thread with a specified exit code. This may be called
+ // from any thread.
+ void Exit(int code);
+
+ // Wait for the worker thread to stop (in a blocking manner).
+ void JoinThread();
+
+ size_t self_size() const override;
+ bool is_stopped() const;
+
+ static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void StartThread(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void StopThread(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void GetMessagePort(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void Ref(const v8::FunctionCallbackInfo<v8::Value>& args);
+ static void Unref(const v8::FunctionCallbackInfo<v8::Value>& args);
+
+ private:
+ void OnThreadStopped();
+ void DisposeIsolate();
+
+ uv_loop_t loop_;
+ DeleteFnPtr<IsolateData, FreeIsolateData> isolate_data_;
+ DeleteFnPtr<Environment, FreeEnvironment> env_;
+ v8::Isolate* isolate_ = nullptr;
+ DeleteFnPtr<ArrayBufferAllocator, FreeArrayBufferAllocator>
+ array_buffer_allocator_;
+ uv_thread_t tid_;
+
+ // This mutex protects access to all variables listed below it.
+ mutable Mutex mutex_;
+
+ // Currently only used for telling the parent thread that the child
+ // thread exited.
+ std::unique_ptr<uv_async_t> thread_exit_async_;
+ bool scheduled_on_thread_stopped_ = false;
+
+ // This mutex only protects stopped_. If both locks are acquired, this needs
+ // to be the latter one.
+ mutable Mutex stopped_mutex_;
+ bool stopped_ = true;
+
+ bool thread_joined_ = true;
+ int exit_code_ = 0;
+ double thread_id_ = -1;
+
+ std::unique_ptr<MessagePortData> child_port_data_;
+
+ // The child port is always kept alive by the child Environment's persistent
+ // handle to it.
+ MessagePort* child_port_ = nullptr;
+ // This is always kept alive because the JS object associated with the Worker
+ // instance refers to it via its [kPort] property.
+ MessagePort* parent_port_ = nullptr;
+};
+
+} // namespace worker
+} // namespace node
+
+#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
+
+
+#endif // SRC_NODE_WORKER_H_