Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1231)

Unified Diff: src/untrusted/init/thread.cc

Issue 25147002: nacl_init Base URL: svn://svn.chromium.org/native_client/trunk/src/native_client
Patch Set: Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/untrusted/init/thread.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/untrusted/init/thread.cc
diff --git a/src/untrusted/init/thread.cc b/src/untrusted/init/thread.cc
new file mode 100644
index 0000000000000000000000000000000000000000..706fb2ca710c0e0dc097e98e29319ed991501c97
--- /dev/null
+++ b/src/untrusted/init/thread.cc
@@ -0,0 +1,106 @@
+// Copyright (c) 2013 The Native Client Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "native_client/src/untrusted/init/thread_interface.h"
+
+#include <pthread.c>
+
+namespace nacl {
+
+Thread::Thread(ThreadFactoryFunction factory,
+ void *factory_data,
+ ThreadStartFunction start_routine,
+ void *startup_data)
+ : started_(false),
+ stopping_(false),
+ running_(false),
+ startup_data_(NULL),
+ factory_(factory),
+ factory_data_(factory_data),
+ start_routine_(start_routine),
+ startup_data_(startup_data) {
+}
+
+Thread::~Thread() {
+ //CHECK(!thread_started_);
+ //start_routine_ = NULL;
+ //thread_data_ = NULL;
+ Stop();
+}
+
+static void *ThreadStart(void *data) {
+ Thread *interface =
+ reinterpret_cast<Thread *>(data);
+ interface->LaunchCallback();
+ void *thread_return = interface->start_routine_();
+ interface->Exit(thread_return);
+}
+
+int Thread::Start() {
+ CHECK(!thread_started_);
+ int retval = pthread_create(&thread_, NULL, ThreadStart, this);
+ if (retval == 0) {
+ thread_started_ = true;
+ }
+ return retval;
+}
+
+void Thread::Stop() {
+ if (!started_) {
+ return;
+ }
+
+ // Wait for the thread to exit.
+ pthread_join(thread_);
+
+ started_ = false;
+ stopping_ = false;
+}
+
+void Thread::StopSoon() {
+ if (stopping_) {
+ return;
+ }
+
+ stopping_ = true;
+}
+
+void Thread::Exit(void *thread_return) {
+ thread_started_ = 0;
+ Unref();
+ pthread_exit(thread_return);
+}
+
+Thread *ThreadConstructAndStartThread(
+ ThreadFactoryFunction factory,
+ void *factory_data,
+ ThreadStartFunction start_routine,
+ void *thread_data) {
+ Thread *thread;
+ if ((thread = (*factory)(factory_data,
+ start_routine,
+ thread_data)) == NULL) {
+ goto out;
+ }
+ if (!thread->Start()) {
+ thread->Unref();
+ goto out;
+ }
+ out:
+ return thread;
+}
+
+Thread *ThreadThreadFactory(
+ void *factory_data,
+ ThreadStartFunction start_routine,
+ void *thread_data) {
+ Thread *thread = new Thread(
+ ThreadThreadFactory,
+ factory_data,
+ start_routine,
+ thread_data);
+ return thread;
+}
+
+} // namespace nacl
« no previous file with comments | « src/untrusted/init/thread.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698