Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/macosx/native_NOTIOS/com/apple/concurrent/Dispatch.m
38902 views
/*1* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#import "com_apple_concurrent_LibDispatchNative.h"2627#import <dispatch/dispatch.h>28#import <JavaNativeFoundation/JavaNativeFoundation.h>293031/*32* Class: com_apple_concurrent_LibDispatchNative33* Method: nativeIsDispatchSupported34* Signature: ()Z35*/36JNIEXPORT jboolean JNICALL Java_com_apple_concurrent_LibDispatchNative_nativeIsDispatchSupported37(JNIEnv *env, jclass clazz)38{39return JNI_TRUE;40}414243/*44* Class: com_apple_concurrent_LibDispatchNative45* Method: nativeGetMainQueue46* Signature: ()J47*/48JNIEXPORT jlong JNICALL Java_com_apple_concurrent_LibDispatchNative_nativeGetMainQueue49(JNIEnv *env, jclass clazz)50{51dispatch_queue_t queue = dispatch_get_main_queue();52return ptr_to_jlong(queue);53}545556/*57* Class: com_apple_concurrent_LibDispatchNative58* Method: nativeCreateConcurrentQueue59* Signature: (I)J60*/61JNIEXPORT jlong JNICALL Java_com_apple_concurrent_LibDispatchNative_nativeCreateConcurrentQueue62(JNIEnv *env, jclass clazz, jint priority)63{64dispatch_queue_t queue = dispatch_get_global_queue((long)priority, 0);65return ptr_to_jlong(queue);66}676869/*70* Class: com_apple_concurrent_LibDispatchNative71* Method: nativeCreateSerialQueue72* Signature: (Ljava/lang/String;)J73*/74JNIEXPORT jlong JNICALL Java_com_apple_concurrent_LibDispatchNative_nativeCreateSerialQueue75(JNIEnv *env, jclass clazz, jstring name)76{77if (name == NULL) return 0L;7879jboolean isCopy;80const char *queue_name = (*env)->GetStringUTFChars(env, name, &isCopy);81dispatch_queue_t queue = dispatch_queue_create(queue_name, NULL);82(*env)->ReleaseStringUTFChars(env, name, queue_name);8384return ptr_to_jlong(queue);85}868788/*89* Class: com_apple_concurrent_LibDispatchNative90* Method: nativeReleaseQueue91* Signature: (J)V92*/93JNIEXPORT void JNICALL Java_com_apple_concurrent_LibDispatchNative_nativeReleaseQueue94(JNIEnv *env, jclass clazz, jlong nativeQueue)95{96if (nativeQueue == 0L) return;97dispatch_release((dispatch_queue_t)jlong_to_ptr(nativeQueue));98}99100101static JNF_CLASS_CACHE(jc_Runnable, "java/lang/Runnable");102static JNF_MEMBER_CACHE(jm_run, jc_Runnable, "run", "()V");103104static void perform_dispatch(JNIEnv *env, jlong nativeQueue, jobject runnable, void (*dispatch_fxn)(dispatch_queue_t, dispatch_block_t))105{106JNF_COCOA_ENTER(env);107dispatch_queue_t queue = (dispatch_queue_t)jlong_to_ptr(nativeQueue);108if (queue == NULL) return; // shouldn't happen109110// create a global-ref around the Runnable, so it can be safely passed to the dispatch thread111JNFJObjectWrapper *wrappedRunnable = [[JNFJObjectWrapper alloc] initWithJObject:runnable withEnv:env];112113dispatch_fxn(queue, ^{114// attach the dispatch thread to the JVM if necessary, and get an env115JNFThreadContext ctx = JNFThreadDetachOnThreadDeath | JNFThreadSetSystemClassLoaderOnAttach | JNFThreadAttachAsDaemon;116JNIEnv *blockEnv = JNFObtainEnv(&ctx);117118JNF_COCOA_ENTER(blockEnv);119120// call the user's runnable121JNFCallObjectMethod(blockEnv, [wrappedRunnable jObject], jm_run);122123// explicitly clear object while we have an env (it's cheaper that way)124[wrappedRunnable setJObject:NULL withEnv:blockEnv];125126JNF_COCOA_EXIT(blockEnv);127128// let the env go, but leave the thread attached as a daemon129JNFReleaseEnv(blockEnv, &ctx);130});131132// release this thread's interest in the Runnable, the block133// will have retained the it's own interest above134[wrappedRunnable release];135136JNF_COCOA_EXIT(env);137}138139140/*141* Class: com_apple_concurrent_LibDispatchNative142* Method: nativeExecuteAsync143* Signature: (JLjava/lang/Runnable;)V144*/145JNIEXPORT void JNICALL Java_com_apple_concurrent_LibDispatchNative_nativeExecuteAsync146(JNIEnv *env, jclass clazz, jlong nativeQueue, jobject runnable)147{148// enqueues and returns149perform_dispatch(env, nativeQueue, runnable, dispatch_async);150}151152153/*154* Class: com_apple_concurrent_LibDispatchNative155* Method: nativeExecuteSync156* Signature: (JLjava/lang/Runnable;)V157*/158JNIEXPORT void JNICALL Java_com_apple_concurrent_LibDispatchNative_nativeExecuteSync159(JNIEnv *env, jclass clazz, jlong nativeQueue, jobject runnable)160{161// blocks until the Runnable completes162perform_dispatch(env, nativeQueue, runnable, dispatch_sync);163}164165166