LibreOffice Module bridges (master) 1
nativethreadpool.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20
21#include <string.h>
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>
28
29#include <jni.h>
30
31#include <new>
32#include <utility>
33
34/* The native implementation part of
35 * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
36 */
37
38namespace {
39
40struct Pool {
42 jmethodID theExecute, uno_ThreadPool thePool):
43 virtualMachine(std::move(theVirtualMachine)), execute(theExecute), pool(thePool) {}
44
46 jmethodID execute;
47 uno_ThreadPool pool;
48};
49
50struct Job {
51 Job(Pool * thePool, jobject theJob): pool(thePool), job(theJob) {}
52
53 Pool * pool;
54 jobject job;
55};
56
57void throwOutOfMemory(JNIEnv * env) {
58 jclass c = env->FindClass("java/lang/OutOfMemoryError");
59 if (c != nullptr) {
60 env->ThrowNew(c, "");
61 }
62}
63
64}
65
66extern "C" {
67
68static 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;
80 //TODO: DeleteGlobalRef(job->job)
81 delete job;
82 }
83}
84
85}
86
87extern "C" SAL_JNI_EXPORT jbyteArray JNICALL
89 JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
90{
91 sal_Sequence * s = nullptr;
92 uno_getIdOfCurrentThread(&s); //TODO: out of memory
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;
101 }
102 void * p = env->GetPrimitiveArrayCritical(a, nullptr);
103 if (p == nullptr) {
104 return nullptr;
105 }
106 memcpy(p, seq.getConstArray(), n);
107 // sal_Int8 and jbyte ought to be compatible
108 env->ReleasePrimitiveArrayCritical(a, p, 0);
109 return a;
110}
111
112extern "C" SAL_JNI_EXPORT jlong JNICALL
114 JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
115{
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");
121 }
122 return 0;
123 }
124 jclass c = env->FindClass("com/sun/star/lib/uno/environments/remote/Job");
125 if (c == nullptr) {
126 return 0;
127 }
128 jmethodID execute = env->GetMethodID(c, "execute", "()Ljava/lang/Object;");
129 if (execute == nullptr) {
130 return 0;
131 }
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;
139 }
140}
141
142extern "C" SAL_JNI_EXPORT void JNICALL
144 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
146{
147 uno_threadpool_attach(reinterpret_cast< Pool * >(pool)->pool);
148}
149
150extern "C" SAL_JNI_EXPORT jobject JNICALL
152 JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
153{
154 jobject job;
156 reinterpret_cast< Pool * >(pool)->pool,
157 reinterpret_cast< void ** >(&job));
158 if (job == nullptr) {
159 return nullptr;
160 }
161 jobject ref = env->NewLocalRef(job);
162 env->DeleteGlobalRef(job);
163 return ref;
164}
165
166extern "C" SAL_JNI_EXPORT void JNICALL
168 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
170{
171 uno_threadpool_detach(reinterpret_cast< Pool * >(pool)->pool);
172}
173
174extern "C" SAL_JNI_EXPORT void JNICALL
176 JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool, jbyteArray threadId,
177 jobject job, jboolean request, jboolean oneWay) SAL_THROW_EXTERN_C()
178{
179 void * s = env->GetPrimitiveArrayCritical(threadId, nullptr);
180 if (s == nullptr) {
181 return;
182 }
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;
193 }
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;
201 }
202 }
204 p->pool, seq.getHandle(),
205 request ? static_cast< void * >(j) : static_cast< void * >(ref),
206 request ? executeRequest : nullptr, oneWay);
207}
208
209extern "C" SAL_JNI_EXPORT void JNICALL
211 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
213{
214 uno_threadpool_dispose(reinterpret_cast< Pool * >(pool)->pool);
215}
216
217extern "C" SAL_JNI_EXPORT void JNICALL
219 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
221{
222 Pool * p = reinterpret_cast< Pool * >(pool);
224 delete p;
225}
226
227/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void * p
sal_Int64 n
uno_Any a
const css::uno::Reference< css::xml::crypto::XSecurityEnvironment > & env
SAL_JNI_EXPORT jobject JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_enter(JNIEnv *env, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
static void executeRequest(void *data)
SAL_JNI_EXPORT jbyteArray JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_threadId(JNIEnv *env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_detach(SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_dispose(SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
SAL_JNI_EXPORT jlong JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_create(JNIEnv *env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_putJob(JNIEnv *env, SAL_UNUSED_PARAMETER jclass, jlong pool, jbyteArray threadId, jobject job, jboolean request, jboolean oneWay) SAL_THROW_EXTERN_C()
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_destroy(SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
SAL_JNI_EXPORT void JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_attach(SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
void SAL_CALL uno_releaseIdFromCurrentThread() SAL_THROW_EXTERN_C()
void SAL_CALL uno_getIdOfCurrentThread(sal_Sequence **ppThreadId) SAL_THROW_EXTERN_C()
void SAL_CALL uno_threadpool_enter(uno_ThreadPool hPool, void **ppJob) SAL_THROW_EXTERN_C()
uno_ThreadPool SAL_CALL uno_threadpool_create() SAL_THROW_EXTERN_C()
void SAL_CALL uno_threadpool_detach(SAL_UNUSED_PARAMETER uno_ThreadPool) SAL_THROW_EXTERN_C()
void SAL_CALL uno_threadpool_putJob(uno_ThreadPool hPool, sal_Sequence *pThreadId, void *pJob, void(SAL_CALL *doRequest)(void *pThreadSpecificData), sal_Bool bIsOneway) SAL_THROW_EXTERN_C()
void SAL_CALL uno_threadpool_attach(uno_ThreadPool hPool) SAL_THROW_EXTERN_C()
void SAL_CALL uno_threadpool_destroy(uno_ThreadPool hPool) SAL_THROW_EXTERN_C()
void SAL_CALL uno_threadpool_dispose(uno_ThreadPool hPool) SAL_THROW_EXTERN_C()
#define SAL_THROW_EXTERN_C()
signed char sal_Int8