blob: 87f04bbbf20bd5a00a71adf5d20e48da6cf504c7 [file] [log] [blame]
Ian Rogers57b86d42012-03-27 16:05:41 -07001/*
Elliott Hughes0f3c5532012-03-30 14:51:51 -07002 * Copyright (C) 2012 The Android Open Source Project
Ian Rogers57b86d42012-03-27 16:05:41 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080017#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070018#include "thread-inl.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080019#include "verify_object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070020
21namespace art {
22
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080024extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070026 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
28 env->local_ref_cookie = env->locals.GetSegmentState();
Andreas Gampecf4035a2014-05-28 22:43:01 -070029 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr();
Ian Rogers1eb512d2013-10-18 15:42:20 -070030 if (!native_method->IsFastNative()) {
31 // When not fast JNI we transition out of runnable.
32 self->TransitionFromRunnableToSuspended(kNative);
33 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 return saved_local_ref_cookie;
35}
Elliott Hughesb264f082012-04-06 17:10:10 -070036
Ian Rogers693ff612013-02-01 10:56:12 -080037extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070038 self->DecodeJObject(to_lock)->MonitorEnter(self);
39 return JniMethodStart(self);
40}
41
Ian Rogers1eb512d2013-10-18 15:42:20 -070042// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
43static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Andreas Gampecf4035a2014-05-28 22:43:01 -070044 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr();
Ian Rogers1eb512d2013-10-18 15:42:20 -070045 bool is_fast = native_method->IsFastNative();
46 if (!is_fast) {
47 self->TransitionFromSuspendedToRunnable();
48 } else if (UNLIKELY(self->TestAllFlags())) {
49 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
50 // is a flag raised.
51 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -070052 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -070053 }
54}
55
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +070056static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
57 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070058 JNIEnvExt* env = self->GetJniEnv();
59 env->locals.SetSegmentState(env->local_ref_cookie);
60 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070061 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070062}
63
Ian Rogers693ff612013-02-01 10:56:12 -080064extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070065 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070066 PopLocalReferences(saved_local_ref_cookie, self);
67}
68
69
Ian Rogers693ff612013-02-01 10:56:12 -080070extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
71 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070072 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
74 PopLocalReferences(saved_local_ref_cookie, self);
75}
76
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080077extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080078 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070079 GoToRunnable(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080080 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070081 PopLocalReferences(saved_local_ref_cookie, self);
82 // Process result.
83 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
84 if (self->IsExceptionPending()) {
85 return NULL;
86 }
87 CheckReferenceResult(o, self);
88 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -080089 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070090 return o;
91}
92
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080093extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
94 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080095 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070096 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 PopLocalReferences(saved_local_ref_cookie, self);
100 // Process result.
101 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
102 if (self->IsExceptionPending()) {
103 return NULL;
104 }
105 CheckReferenceResult(o, self);
106 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800107 VerifyObject(o);
Elliott Hughesb264f082012-04-06 17:10:10 -0700108 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700109}
110
Ian Rogers57b86d42012-03-27 16:05:41 -0700111} // namespace art