blob: 653724989a420c9780b18b68b99e20c387243f66 [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 Rogers4f6ad8a2013-03-18 15:27:28 -070017#include "dex_file-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070018#include "entrypoints/entrypoint_utils-inl.h"
Brian Carlstromea46f952013-07-30 01:26:50 -070019#include "mirror/art_method-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/object.h"
22#include "mirror/object-inl.h"
23#include "mirror/object_array-inl.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070024#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070025#include "thread.h"
Mathieu Chartierc645f1d2014-03-06 18:11:53 -080026#include "verify_object-inl.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070027
28namespace art {
29
Ian Rogers00f7d0e2012-07-19 15:28:27 -070030// Called on entry to JNI, transition out of Runnable and release share of mutator_lock_.
Ian Rogers693ff612013-02-01 10:56:12 -080031extern uint32_t JniMethodStart(Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070032 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers1eb512d2013-10-18 15:42:20 -070033 DCHECK(env != nullptr);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070034 uint32_t saved_local_ref_cookie = env->local_ref_cookie;
35 env->local_ref_cookie = env->locals.GetSegmentState();
Andreas Gampecf4035a2014-05-28 22:43:01 -070036 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr();
Ian Rogers1eb512d2013-10-18 15:42:20 -070037 if (!native_method->IsFastNative()) {
38 // When not fast JNI we transition out of runnable.
39 self->TransitionFromRunnableToSuspended(kNative);
40 }
Ian Rogers00f7d0e2012-07-19 15:28:27 -070041 return saved_local_ref_cookie;
42}
Elliott Hughesb264f082012-04-06 17:10:10 -070043
Ian Rogers693ff612013-02-01 10:56:12 -080044extern uint32_t JniMethodStartSynchronized(jobject to_lock, Thread* self) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070045 self->DecodeJObject(to_lock)->MonitorEnter(self);
46 return JniMethodStart(self);
47}
48
Ian Rogers1eb512d2013-10-18 15:42:20 -070049// TODO: NO_THREAD_SAFETY_ANALYSIS due to different control paths depending on fast JNI.
50static void GoToRunnable(Thread* self) NO_THREAD_SAFETY_ANALYSIS {
Andreas Gampecf4035a2014-05-28 22:43:01 -070051 mirror::ArtMethod* native_method = self->GetManagedStack()->GetTopQuickFrame()->AsMirrorPtr();
Ian Rogers1eb512d2013-10-18 15:42:20 -070052 bool is_fast = native_method->IsFastNative();
53 if (!is_fast) {
54 self->TransitionFromSuspendedToRunnable();
55 } else if (UNLIKELY(self->TestAllFlags())) {
56 // In fast JNI mode we never transitioned out of runnable. Perform a suspend check if there
57 // is a flag raised.
58 DCHECK(Locks::mutator_lock_->IsSharedHeld(self));
59 CheckSuspend(self);
60 }
61}
62
Yevgeny Rouban35aef2c2014-05-19 16:19:36 +070063static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self)
64 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070065 JNIEnvExt* env = self->GetJniEnv();
66 env->locals.SetSegmentState(env->local_ref_cookie);
67 env->local_ref_cookie = saved_local_ref_cookie;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070068 self->PopHandleScope();
Ian Rogers00f7d0e2012-07-19 15:28:27 -070069}
70
Ian Rogers693ff612013-02-01 10:56:12 -080071extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070072 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070073 PopLocalReferences(saved_local_ref_cookie, self);
74}
75
76
Ian Rogers693ff612013-02-01 10:56:12 -080077extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
78 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070079 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070080 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
81 PopLocalReferences(saved_local_ref_cookie, self);
82}
83
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080084extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080085 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070086 GoToRunnable(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080087 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070088 PopLocalReferences(saved_local_ref_cookie, self);
89 // Process result.
90 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
91 if (self->IsExceptionPending()) {
92 return NULL;
93 }
94 CheckReferenceResult(o, self);
95 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -080096 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070097 return o;
98}
99
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800100extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
101 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800102 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700103 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700104 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800105 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700106 PopLocalReferences(saved_local_ref_cookie, self);
107 // Process result.
108 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
109 if (self->IsExceptionPending()) {
110 return NULL;
111 }
112 CheckReferenceResult(o, self);
113 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800114 VerifyObject(o);
Elliott Hughesb264f082012-04-06 17:10:10 -0700115 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700116}
117
Ian Rogers57b86d42012-03-27 16:05:41 -0700118} // namespace art