Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / bridges / source / jni_uno / nativethreadpool.cxx
blob1d14c47bb0ad7c8be3e7d68db571ae7afb935394
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <string.h>
22 #include <jvmaccess/virtualmachine.hxx>
23 #include <rtl/byteseq.h>
24 #include <rtl/byteseq.hxx>
25 #include <rtl/ref.hxx>
26 #include <sal/types.h>
27 #include <uno/threadpool.h>
29 #include <jni.h>
31 #include <new>
32 #include <utility>
34 /* The native implementation part of
35 * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
38 namespace {
40 struct Pool {
41 Pool(rtl::Reference< jvmaccess::VirtualMachine > theVirtualMachine,
42 jmethodID theExecute, uno_ThreadPool thePool):
43 virtualMachine(std::move(theVirtualMachine)), execute(theExecute), pool(thePool) {}
45 rtl::Reference< jvmaccess::VirtualMachine > virtualMachine;
46 jmethodID execute;
47 uno_ThreadPool pool;
50 struct Job {
51 Job(Pool * thePool, jobject theJob): pool(thePool), job(theJob) {}
53 Pool * pool;
54 jobject job;
57 void throwOutOfMemory(JNIEnv * env) {
58 jclass c = env->FindClass("java/lang/OutOfMemoryError");
59 if (c != nullptr) {
60 env->ThrowNew(c, "");
66 extern "C" {
68 static void executeRequest(void * data) {
69 Job * job = static_cast< Job * >(data);
70 try {
71 jvmaccess::VirtualMachine::AttachGuard guard(job->pool->virtualMachine);
72 JNIEnv * env = guard.getEnvironment();
73 // Failure of the following Job.execute Java call is ignored; if that
74 // call fails, it should be due to a java.lang.Error, which is not
75 // handled well, anyway:
76 env->CallObjectMethod(job->job, job->pool->execute);
77 env->DeleteGlobalRef(job->job);
78 delete job;
79 } catch (const jvmaccess::VirtualMachine::AttachGuard::CreationException &) {
80 //TODO: DeleteGlobalRef(job->job)
81 delete job;
87 extern "C" SAL_JNI_EXPORT jbyteArray JNICALL
88 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_threadId(
89 JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
91 sal_Sequence * s = nullptr;
92 uno_getIdOfCurrentThread(&s); //TODO: out of memory
93 uno_releaseIdFromCurrentThread();
94 rtl::ByteSequence seq(s);
95 rtl_byte_sequence_release(s);
96 sal_Int32 n = seq.getLength();
97 jbyteArray a = env->NewByteArray(n);
98 // sal_Int32 and jsize are compatible here
99 if (a == nullptr) {
100 return nullptr;
102 void * p = env->GetPrimitiveArrayCritical(a, nullptr);
103 if (p == nullptr) {
104 return nullptr;
106 memcpy(p, seq.getConstArray(), n);
107 // sal_Int8 and jbyte ought to be compatible
108 env->ReleasePrimitiveArrayCritical(a, p, 0);
109 return a;
112 extern "C" SAL_JNI_EXPORT jlong JNICALL
113 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_create(
114 JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
116 JavaVM * vm;
117 if (env->GetJavaVM(&vm) != JNI_OK) { //TODO: no Java exception raised?
118 jclass c = env->FindClass("java/lang/RuntimeException");
119 if (c != nullptr) {
120 env->ThrowNew(c, "JNI GetJavaVM failed");
122 return 0;
124 jclass c = env->FindClass("com/sun/star/lib/uno/environments/remote/Job");
125 if (c == nullptr) {
126 return 0;
128 jmethodID execute = env->GetMethodID(c, "execute", "()Ljava/lang/Object;");
129 if (execute == nullptr) {
130 return 0;
132 try {
133 return reinterpret_cast< jlong >(new Pool(
134 new jvmaccess::VirtualMachine(vm, env->GetVersion(), false, env),
135 execute, uno_threadpool_create()));
136 } catch (const std::bad_alloc &) {
137 throwOutOfMemory(env);
138 return 0;
142 extern "C" SAL_JNI_EXPORT void JNICALL
143 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_attach(
144 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
145 SAL_THROW_EXTERN_C()
147 uno_threadpool_attach(reinterpret_cast< Pool * >(pool)->pool);
150 extern "C" SAL_JNI_EXPORT jobject JNICALL
151 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_enter(
152 JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
154 jobject job;
155 uno_threadpool_enter(
156 reinterpret_cast< Pool * >(pool)->pool,
157 reinterpret_cast< void ** >(&job));
158 if (job == nullptr) {
159 return nullptr;
161 jobject ref = env->NewLocalRef(job);
162 env->DeleteGlobalRef(job);
163 return ref;
166 extern "C" SAL_JNI_EXPORT void JNICALL
167 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_detach(
168 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
169 SAL_THROW_EXTERN_C()
171 uno_threadpool_detach(reinterpret_cast< Pool * >(pool)->pool);
174 extern "C" SAL_JNI_EXPORT void JNICALL
175 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_putJob(
176 JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool, jbyteArray threadId,
177 jobject job, jboolean request, jboolean oneWay) SAL_THROW_EXTERN_C()
179 void * s = env->GetPrimitiveArrayCritical(threadId, nullptr);
180 if (s == nullptr) {
181 return;
183 rtl::ByteSequence seq(
184 static_cast< sal_Int8 * >(s), env->GetArrayLength(threadId));
185 // sal_Int8 and jbyte ought to be compatible; sal_Int32 and jsize are
186 // compatible here
187 //TODO: out of memory
188 env->ReleasePrimitiveArrayCritical(threadId, s, JNI_ABORT);
189 Pool * p = reinterpret_cast< Pool * >(pool);
190 jobject ref = env->NewGlobalRef(job);
191 if (ref == nullptr) {
192 return;
194 Job * j = nullptr;
195 if (request) {
196 j = new(std::nothrow) Job(p, ref);
197 if (j == nullptr) {
198 env->DeleteGlobalRef(ref);
199 throwOutOfMemory(env);
200 return;
203 uno_threadpool_putJob(
204 p->pool, seq.getHandle(),
205 request ? static_cast< void * >(j) : static_cast< void * >(ref),
206 request ? executeRequest : nullptr, oneWay);
209 extern "C" SAL_JNI_EXPORT void JNICALL
210 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_dispose(
211 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
212 SAL_THROW_EXTERN_C()
214 uno_threadpool_dispose(reinterpret_cast< Pool * >(pool)->pool);
217 extern "C" SAL_JNI_EXPORT void JNICALL
218 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_destroy(
219 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
220 SAL_THROW_EXTERN_C()
222 Pool * p = reinterpret_cast< Pool * >(pool);
223 uno_threadpool_destroy(p->pool);
224 delete p;
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */