OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/android/java_handler_thread.h" | 5 #include "base/android/java_handler_thread.h" |
6 | 6 |
7 #include <jni.h> | 7 #include <jni.h> |
8 | 8 |
9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/synchronization/waitable_event.h" | 12 #include "base/synchronization/waitable_event.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "jni/JavaHandlerThread_jni.h" | 14 #include "jni/JavaHandlerThread_jni.h" |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 | 17 |
18 namespace android { | 18 namespace android { |
19 | 19 |
20 JavaHandlerThread::JavaHandlerThread(const char* name) { | 20 JavaHandlerThread::JavaHandlerThread(const char* name) { |
21 JNIEnv* env = base::android::AttachCurrentThread(); | 21 JNIEnv* env = base::android::AttachCurrentThread(); |
22 | 22 |
23 java_thread_.Reset(Java_JavaHandlerThread_create( | 23 java_thread_.Reset( |
24 env, ConvertUTF8ToJavaString(env, name).obj())); | 24 Java_JavaHandlerThread_create(env, ConvertUTF8ToJavaString(env, name))); |
25 } | 25 } |
26 | 26 |
27 JavaHandlerThread::~JavaHandlerThread() { | 27 JavaHandlerThread::~JavaHandlerThread() { |
28 } | 28 } |
29 | 29 |
30 void JavaHandlerThread::Start() { | 30 void JavaHandlerThread::Start() { |
31 // Check the thread has not already been started. | 31 // Check the thread has not already been started. |
32 DCHECK(!message_loop_); | 32 DCHECK(!message_loop_); |
33 | 33 |
34 JNIEnv* env = base::android::AttachCurrentThread(); | 34 JNIEnv* env = base::android::AttachCurrentThread(); |
35 base::WaitableEvent initialize_event( | 35 base::WaitableEvent initialize_event( |
36 WaitableEvent::ResetPolicy::AUTOMATIC, | 36 WaitableEvent::ResetPolicy::AUTOMATIC, |
37 WaitableEvent::InitialState::NOT_SIGNALED); | 37 WaitableEvent::InitialState::NOT_SIGNALED); |
38 Java_JavaHandlerThread_start(env, | 38 Java_JavaHandlerThread_start(env, java_thread_, |
39 java_thread_.obj(), | |
40 reinterpret_cast<intptr_t>(this), | 39 reinterpret_cast<intptr_t>(this), |
41 reinterpret_cast<intptr_t>(&initialize_event)); | 40 reinterpret_cast<intptr_t>(&initialize_event)); |
42 // Wait for thread to be initialized so it is ready to be used when Start | 41 // Wait for thread to be initialized so it is ready to be used when Start |
43 // returns. | 42 // returns. |
44 base::ThreadRestrictions::ScopedAllowWait wait_allowed; | 43 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
45 initialize_event.Wait(); | 44 initialize_event.Wait(); |
46 } | 45 } |
47 | 46 |
48 void JavaHandlerThread::Stop() { | 47 void JavaHandlerThread::Stop() { |
49 JNIEnv* env = base::android::AttachCurrentThread(); | 48 JNIEnv* env = base::android::AttachCurrentThread(); |
50 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC, | 49 base::WaitableEvent shutdown_event(WaitableEvent::ResetPolicy::AUTOMATIC, |
51 WaitableEvent::InitialState::NOT_SIGNALED); | 50 WaitableEvent::InitialState::NOT_SIGNALED); |
52 Java_JavaHandlerThread_stop(env, | 51 Java_JavaHandlerThread_stop(env, java_thread_, |
53 java_thread_.obj(), | |
54 reinterpret_cast<intptr_t>(this), | 52 reinterpret_cast<intptr_t>(this), |
55 reinterpret_cast<intptr_t>(&shutdown_event)); | 53 reinterpret_cast<intptr_t>(&shutdown_event)); |
56 // Wait for thread to shut down before returning. | 54 // Wait for thread to shut down before returning. |
57 base::ThreadRestrictions::ScopedAllowWait wait_allowed; | 55 base::ThreadRestrictions::ScopedAllowWait wait_allowed; |
58 shutdown_event.Wait(); | 56 shutdown_event.Wait(); |
59 } | 57 } |
60 | 58 |
61 void JavaHandlerThread::InitializeThread(JNIEnv* env, | 59 void JavaHandlerThread::InitializeThread(JNIEnv* env, |
62 const JavaParamRef<jobject>& obj, | 60 const JavaParamRef<jobject>& obj, |
63 jlong event) { | 61 jlong event) { |
(...skipping 10 matching lines...) Expand all Loading... |
74 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); | 72 reinterpret_cast<base::WaitableEvent*>(event)->Signal(); |
75 } | 73 } |
76 | 74 |
77 // static | 75 // static |
78 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { | 76 bool JavaHandlerThread::RegisterBindings(JNIEnv* env) { |
79 return RegisterNativesImpl(env); | 77 return RegisterNativesImpl(env); |
80 } | 78 } |
81 | 79 |
82 } // namespace android | 80 } // namespace android |
83 } // namespace base | 81 } // namespace base |
OLD | NEW |