blob: 737fa3e7358f2b44714d34130d1d3c2a0f04b7c4 [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"
Ian Rogers7655f292013-07-29 11:07:13 -070018#include "entrypoints/entrypoint_utils.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 Rogers57b86d42012-03-27 16:05:41 -070024#include "object_utils.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070026#include "thread.h"
27
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();
Ian Rogers1eb512d2013-10-18 15:42:20 -070036 mirror::ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
37 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 {
51 mirror::ArtMethod* native_method = *self->GetManagedStack()->GetTopQuickFrame();
52 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
Ian Rogers00f7d0e2012-07-19 15:28:27 -070063static void PopLocalReferences(uint32_t saved_local_ref_cookie, Thread* self) {
64 JNIEnvExt* env = self->GetJniEnv();
65 env->locals.SetSegmentState(env->local_ref_cookie);
66 env->local_ref_cookie = saved_local_ref_cookie;
67 self->PopSirt();
68}
69
Ian Rogers693ff612013-02-01 10:56:12 -080070extern void JniMethodEnd(uint32_t saved_local_ref_cookie, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070071 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070072 PopLocalReferences(saved_local_ref_cookie, self);
73}
74
75
Ian Rogers693ff612013-02-01 10:56:12 -080076extern void JniMethodEndSynchronized(uint32_t saved_local_ref_cookie, jobject locked,
77 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070078 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070079 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
80 PopLocalReferences(saved_local_ref_cookie, self);
81}
82
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080083extern mirror::Object* JniMethodEndWithReference(jobject result, uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -080084 Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -070085 GoToRunnable(self);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080086 mirror::Object* o = self->DecodeJObject(result); // Must decode before pop.
Ian Rogers00f7d0e2012-07-19 15:28:27 -070087 PopLocalReferences(saved_local_ref_cookie, self);
88 // Process result.
89 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
90 if (self->IsExceptionPending()) {
91 return NULL;
92 }
93 CheckReferenceResult(o, self);
94 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -080095 VerifyObject(o);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070096 return o;
97}
98
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099extern mirror::Object* JniMethodEndWithReferenceSynchronized(jobject result,
100 uint32_t saved_local_ref_cookie,
Ian Rogers693ff612013-02-01 10:56:12 -0800101 jobject locked, Thread* self) {
Ian Rogers1eb512d2013-10-18 15:42:20 -0700102 GoToRunnable(self);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700103 UnlockJniSynchronizedMethod(locked, self); // Must decode before pop.
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 mirror::Object* o = self->DecodeJObject(result);
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700105 PopLocalReferences(saved_local_ref_cookie, self);
106 // Process result.
107 if (UNLIKELY(self->GetJniEnv()->check_jni)) {
108 if (self->IsExceptionPending()) {
109 return NULL;
110 }
111 CheckReferenceResult(o, self);
112 }
Mathieu Chartier9e8b3672014-02-28 17:06:40 -0800113 VerifyObject(o);
Elliott Hughesb264f082012-04-06 17:10:10 -0700114 return o;
Ian Rogers57b86d42012-03-27 16:05:41 -0700115}
116
Ian Rogers57b86d42012-03-27 16:05:41 -0700117} // namespace art