blob: c1276b5bf21914c62a7cfea711d83b67a79a5cfb [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
Mathieu Chartier76433272014-09-26 14:32:37 -070017#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogersc7dd2952014-10-21 23:31:19 -070018#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/object-inl.h"
Ian Rogers7b078e82014-09-10 14:44:24 -070020#include "thread-inl.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080021#include "verify_object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070022
23namespace art {
24
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080026extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070027 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070028 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070029 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
30 env->local_ref_cookie = env->locals.GetSegmentState();
Andreas Gampecf4035a2014-05-28 22:43:01 -070031 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr();
Ian Rogers1eb512d2013-10-18 15:42:20 -070032 if (!native_method->IsFastNative()) {
33 // When not fast JNI we transition out of runnable.
34 self->TransitionFromRunnableToSuspended(kNative);
35 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 return saved_local_ref_cookie;
37}
Elliott Hughesb264f082012-04-06 17:10:10 -070038
Ian Rogers693ff612013-02-01 10:56:12 -080039extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070040 self->DecodeJObject(to_lock)->MonitorEnter(self);
41 return JniMethodStart(self);
42}
43
Ian Rogers1eb512d2013-10-18 15:42:20 -070044// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
45static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Andreas Gampecf4035a2014-05-28 22:43:01 -070046 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr();
Ian Rogers1eb512d2013-10-18 15:42:20 -070047 bool is_fast = native_method->IsFastNative();
48 if (!is_fast) {
49 self->TransitionFromSuspendedToRunnable();
50 } else if (UNLIKELY(self->TestAllFlags())) {
51 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
52 // is a flag raised.
53 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
Ian Rogers7b078e82014-09-10 14:44:24 -070054 self->CheckSuspend();
Ian Rogers1eb512d2013-10-18 15:42:20 -070055 }
56}
57
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +070058static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
59 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070060 JNIEnvExt* env = self->GetJniEnv();
61 env->locals.SetSegmentState(env->local_ref_cookie);
62 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070063 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070064}
65
Ian Rogers693ff612013-02-01 10:56:12 -080066extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070067 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070068 PopLocalReferences(saved_local_ref_cookie, self);
69}
70
71
Ian Rogers693ff612013-02-01 10:56:12 -080072extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
73 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070074 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070075 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
76 PopLocalReferences(saved_local_ref_cookie, self);
77}
78
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080079extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080080 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070081 GoToRunnable(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080082 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070083 PopLocalReferences(saved_local_ref_cookie, self);
84 // Process result.
85 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
86 if (self->IsExceptionPending()) {
87 return NULL;
88 }
89 CheckReferenceResult(o, self);
90 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -080091 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070092 return o;
93}
94
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080095extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
96 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080097 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070098 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070099 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700101 PopLocalReferences(saved_local_ref_cookie, self);
102 // Process result.
103 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
104 if (self->IsExceptionPending()) {
105 return NULL;
106 }
107 CheckReferenceResult(o, self);
108 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800109 VerifyObject(o);
Elliott Hughesb264f082012-04-06 17:10:10 -0700110 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700111}
112
Ian Rogers57b86d42012-03-27 16:05:41 -0700113} // namespace art