Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 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 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 17 | #define LOG_TAG "MessageQueue-JNI" |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 18 | |
| 19 | #include "JNIHelp.h" |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 20 | #include <android_runtime/AndroidRuntime.h> |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 21 | |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 22 | #include <utils/Looper.h> |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 23 | #include <utils/Log.h> |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 24 | #include "android_os_MessageQueue.h" |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 27 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 28 | static struct { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 29 | jfieldID mPtr; // native object attached to the DVM MessageQueue |
| 30 | } gMessageQueueClassInfo; |
| 31 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 32 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 33 | class NativeMessageQueue : public MessageQueue { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 34 | public: |
| 35 | NativeMessageQueue(); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 36 | virtual ~NativeMessageQueue(); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 37 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 38 | virtual void raiseException(JNIEnv* env, const char* msg, jthrowable exceptionObj); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 39 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 40 | void pollOnce(JNIEnv* env, int timeoutMillis); |
| 41 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 42 | void wake(); |
| 43 | |
| 44 | private: |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 45 | bool mInCallback; |
| 46 | jthrowable mExceptionObj; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 49 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 50 | MessageQueue::MessageQueue() { |
| 51 | } |
| 52 | |
| 53 | MessageQueue::~MessageQueue() { |
| 54 | } |
| 55 | |
| 56 | bool MessageQueue::raiseAndClearException(JNIEnv* env, const char* msg) { |
| 57 | jthrowable exceptionObj = env->ExceptionOccurred(); |
| 58 | if (exceptionObj) { |
| 59 | env->ExceptionClear(); |
| 60 | raiseException(env, msg, exceptionObj); |
| 61 | env->DeleteLocalRef(exceptionObj); |
| 62 | return true; |
| 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | NativeMessageQueue::NativeMessageQueue() : mInCallback(false), mExceptionObj(NULL) { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 68 | mLooper = Looper::getForThread(); |
| 69 | if (mLooper == NULL) { |
| 70 | mLooper = new Looper(false); |
| 71 | Looper::setForThread(mLooper); |
Dianne Hackborn | 6826741 | 2010-07-02 18:52:01 -0700 | [diff] [blame] | 72 | } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 75 | NativeMessageQueue::~NativeMessageQueue() { |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 78 | void NativeMessageQueue::raiseException(JNIEnv* env, const char* msg, jthrowable exceptionObj) { |
| 79 | if (exceptionObj) { |
| 80 | if (mInCallback) { |
| 81 | if (mExceptionObj) { |
| 82 | env->DeleteLocalRef(mExceptionObj); |
| 83 | } |
| 84 | mExceptionObj = jthrowable(env->NewLocalRef(exceptionObj)); |
| 85 | ALOGE("Exception in MessageQueue callback: %s", msg); |
| 86 | jniLogException(env, ANDROID_LOG_ERROR, LOG_TAG, exceptionObj); |
| 87 | } else { |
| 88 | ALOGE("Exception: %s", msg); |
| 89 | jniLogException(env, ANDROID_LOG_ERROR, LOG_TAG, exceptionObj); |
| 90 | LOG_ALWAYS_FATAL("raiseException() was called when not in a callback, exiting."); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | void NativeMessageQueue::pollOnce(JNIEnv* env, int timeoutMillis) { |
| 96 | mInCallback = true; |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 97 | mLooper->pollOnce(timeoutMillis); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 98 | mInCallback = false; |
| 99 | if (mExceptionObj) { |
| 100 | env->Throw(mExceptionObj); |
| 101 | env->DeleteLocalRef(mExceptionObj); |
| 102 | mExceptionObj = NULL; |
| 103 | } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 106 | void NativeMessageQueue::wake() { |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 107 | mLooper->wake(); |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // ---------------------------------------------------------------------------- |
| 111 | |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 112 | sp<MessageQueue> android_os_MessageQueue_getMessageQueue(JNIEnv* env, jobject messageQueueObj) { |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 113 | jlong ptr = env->GetLongField(messageQueueObj, gMessageQueueClassInfo.mPtr); |
| 114 | return reinterpret_cast<NativeMessageQueue*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 117 | static jlong android_os_MessageQueue_nativeInit(JNIEnv* env, jclass clazz) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 118 | NativeMessageQueue* nativeMessageQueue = new NativeMessageQueue(); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 119 | if (!nativeMessageQueue) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 120 | jniThrowRuntimeException(env, "Unable to allocate native queue"); |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 121 | return 0; |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 124 | nativeMessageQueue->incStrong(env); |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 125 | return reinterpret_cast<jlong>(nativeMessageQueue); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 128 | static void android_os_MessageQueue_nativeDestroy(JNIEnv* env, jclass clazz, jlong ptr) { |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 129 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
| 130 | nativeMessageQueue->decStrong(env); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 131 | } |
| 132 | |
Jeff Brown | 86469346 | 2013-01-28 14:25:53 -0800 | [diff] [blame] | 133 | static void android_os_MessageQueue_nativePollOnce(JNIEnv* env, jclass clazz, |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 134 | jlong ptr, jint timeoutMillis) { |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 135 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
Jeff Brown | 603b445 | 2012-04-06 17:39:41 -0700 | [diff] [blame] | 136 | nativeMessageQueue->pollOnce(env, timeoutMillis); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 139 | static void android_os_MessageQueue_nativeWake(JNIEnv* env, jclass clazz, jlong ptr) { |
Jeff Brown | 415d8c3 | 2010-10-05 15:35:37 -0700 | [diff] [blame] | 140 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 141 | return nativeMessageQueue->wake(); |
| 142 | } |
| 143 | |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 144 | static jboolean android_os_MessageQueue_nativeIsIdling(JNIEnv* env, jclass clazz, jlong ptr) { |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 145 | NativeMessageQueue* nativeMessageQueue = reinterpret_cast<NativeMessageQueue*>(ptr); |
| 146 | return nativeMessageQueue->getLooper()->isIdling(); |
| 147 | } |
| 148 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 149 | // ---------------------------------------------------------------------------- |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 150 | |
| 151 | static JNINativeMethod gMessageQueueMethods[] = { |
| 152 | /* name, signature, funcPtr */ |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 153 | { "nativeInit", "()J", (void*)android_os_MessageQueue_nativeInit }, |
| 154 | { "nativeDestroy", "(J)V", (void*)android_os_MessageQueue_nativeDestroy }, |
| 155 | { "nativePollOnce", "(JI)V", (void*)android_os_MessageQueue_nativePollOnce }, |
| 156 | { "nativeWake", "(J)V", (void*)android_os_MessageQueue_nativeWake }, |
| 157 | { "nativeIsIdling", "(J)Z", (void*)android_os_MessageQueue_nativeIsIdling } |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 158 | }; |
| 159 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 160 | #define FIND_CLASS(var, className) \ |
| 161 | var = env->FindClass(className); \ |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 162 | LOG_FATAL_IF(! var, "Unable to find class " className); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 163 | |
| 164 | #define GET_FIELD_ID(var, clazz, fieldName, fieldDescriptor) \ |
| 165 | var = env->GetFieldID(clazz, fieldName, fieldDescriptor); \ |
| 166 | LOG_FATAL_IF(! var, "Unable to find field " fieldName); |
| 167 | |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 168 | int register_android_os_MessageQueue(JNIEnv* env) { |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 169 | int res = jniRegisterNativeMethods(env, "android/os/MessageQueue", |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 170 | gMessageQueueMethods, NELEM(gMessageQueueMethods)); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 171 | LOG_FATAL_IF(res < 0, "Unable to register native methods."); |
| 172 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 173 | jclass clazz; |
| 174 | FIND_CLASS(clazz, "android/os/MessageQueue"); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 175 | |
Carl Shapiro | 17cc33a | 2011-03-05 20:53:16 -0800 | [diff] [blame] | 176 | GET_FIELD_ID(gMessageQueueClassInfo.mPtr, clazz, |
Ashok Bhat | 63a3715 | 2014-01-10 14:15:21 +0000 | [diff] [blame] | 177 | "mPtr", "J"); |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 178 | |
| 179 | return 0; |
Christopher Tate | fa9e7c0 | 2010-05-06 12:07:10 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Jeff Brown | 46b9ac0 | 2010-04-22 18:58:52 -0700 | [diff] [blame] | 182 | } // namespace android |