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 
33 /* The native implementation part of
34  * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
35  */
36 
37 namespace {
38 
39 struct Pool {
40  Pool(rtl::Reference< jvmaccess::VirtualMachine > const & theVirtualMachine,
41  jmethodID theExecute, uno_ThreadPool thePool):
42  virtualMachine(theVirtualMachine), execute(theExecute), pool(thePool) {}
43 
45  jmethodID execute;
46  uno_ThreadPool pool;
47 };
48 
49 struct Job {
50  Job(Pool * thePool, jobject theJob): pool(thePool), job(theJob) {}
51 
52  Pool * pool;
53  jobject job;
54 };
55 
56 void throwOutOfMemory(JNIEnv * env) {
57  jclass c = env->FindClass("java/lang/OutOfMemoryError");
58  if (c != nullptr) {
59  env->ThrowNew(c, "");
60  }
61 }
62 
63 }
64 
65 extern "C" {
66 
67 static void executeRequest(void * data) {
68  Job * job = static_cast< Job * >(data);
69  try {
70  jvmaccess::VirtualMachine::AttachGuard guard(job->pool->virtualMachine);
71  JNIEnv * env = guard.getEnvironment();
72  // Failure of the following Job.execute Java call is ignored; if that
73  // call fails, it should be due to a java.lang.Error, which is not
74  // handled well, anyway:
75  env->CallObjectMethod(job->job, job->pool->execute);
76  env->DeleteGlobalRef(job->job);
77  delete job;
79  //TODO: DeleteGlobalRef(job->job)
80  delete job;
81  }
82 }
83 
84 }
85 
86 extern "C" SAL_JNI_EXPORT jbyteArray JNICALL
88  JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
89 {
90  sal_Sequence * s = nullptr;
91  uno_getIdOfCurrentThread(&s); //TODO: out of memory
93  rtl::ByteSequence seq(s);
94  rtl_byte_sequence_release(s);
95  sal_Int32 n = seq.getLength();
96  jbyteArray a = env->NewByteArray(n);
97  // sal_Int32 and jsize are compatible here
98  if (a == nullptr) {
99  return nullptr;
100  }
101  void * p = env->GetPrimitiveArrayCritical(a, nullptr);
102  if (p == nullptr) {
103  return nullptr;
104  }
105  memcpy(p, seq.getConstArray(), n);
106  // sal_Int8 and jbyte ought to be compatible
107  env->ReleasePrimitiveArrayCritical(a, p, 0);
108  return a;
109 }
110 
111 extern "C" SAL_JNI_EXPORT jlong JNICALL
113  JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
114 {
115  JavaVM * vm;
116  if (env->GetJavaVM(&vm) != JNI_OK) { //TODO: no Java exception raised?
117  jclass c = env->FindClass("java/lang/RuntimeException");
118  if (c != nullptr) {
119  env->ThrowNew(c, "JNI GetJavaVM failed");
120  }
121  return 0;
122  }
123  jclass c = env->FindClass("com/sun/star/lib/uno/environments/remote/Job");
124  if (c == nullptr) {
125  return 0;
126  }
127  jmethodID execute = env->GetMethodID(c, "execute", "()Ljava/lang/Object;");
128  if (execute == nullptr) {
129  return 0;
130  }
131  try {
132  return reinterpret_cast< jlong >(new Pool(
133  new jvmaccess::VirtualMachine(vm, env->GetVersion(), false, env),
134  execute, uno_threadpool_create()));
135  } catch (const std::bad_alloc &) {
136  throwOutOfMemory(env);
137  return 0;
138  }
139 }
140 
141 extern "C" SAL_JNI_EXPORT void JNICALL
143  SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
145 {
146  uno_threadpool_attach(reinterpret_cast< Pool * >(pool)->pool);
147 }
148 
149 extern "C" SAL_JNI_EXPORT jobject JNICALL
151  JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
152 {
153  jobject job;
155  reinterpret_cast< Pool * >(pool)->pool,
156  reinterpret_cast< void ** >(&job));
157  if (job == nullptr) {
158  return nullptr;
159  }
160  jobject ref = env->NewLocalRef(job);
161  env->DeleteGlobalRef(job);
162  return ref;
163 }
164 
165 extern "C" SAL_JNI_EXPORT void JNICALL
167  SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
169 {
170  uno_threadpool_detach(reinterpret_cast< Pool * >(pool)->pool);
171 }
172 
173 extern "C" SAL_JNI_EXPORT void JNICALL
175  JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool, jbyteArray threadId,
176  jobject job, jboolean request, jboolean oneWay) SAL_THROW_EXTERN_C()
177 {
178  void * s = env->GetPrimitiveArrayCritical(threadId, nullptr);
179  if (s == nullptr) {
180  return;
181  }
182  rtl::ByteSequence seq(
183  static_cast< sal_Int8 * >(s), env->GetArrayLength(threadId));
184  // sal_Int8 and jbyte ought to be compatible; sal_Int32 and jsize are
185  // compatible here
186  //TODO: out of memory
187  env->ReleasePrimitiveArrayCritical(threadId, s, JNI_ABORT);
188  Pool * p = reinterpret_cast< Pool * >(pool);
189  jobject ref = env->NewGlobalRef(job);
190  if (ref == nullptr) {
191  return;
192  }
193  Job * j = nullptr;
194  if (request) {
195  j = new(std::nothrow) Job(p, ref);
196  if (j == nullptr) {
197  env->DeleteGlobalRef(ref);
198  throwOutOfMemory(env);
199  return;
200  }
201  }
203  p->pool, seq.getHandle(),
204  request ? static_cast< void * >(j) : static_cast< void * >(ref),
205  request ? executeRequest : nullptr, oneWay);
206 }
207 
208 extern "C" SAL_JNI_EXPORT void JNICALL
210  SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
212 {
213  uno_threadpool_dispose(reinterpret_cast< Pool * >(pool)->pool);
214 }
215 
216 extern "C" SAL_JNI_EXPORT void JNICALL
218  SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
220 {
221  Pool * p = reinterpret_cast< Pool * >(pool);
222  uno_threadpool_destroy(p->pool);
223  delete p;
224 }
225 
226 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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_dispose(SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool) 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()
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()
sal_Int64 n
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()
void SAL_CALL uno_threadpool_detach(SAL_UNUSED_PARAMETER uno_ThreadPool) SAL_THROW_EXTERN_C()
void SAL_CALL uno_releaseIdFromCurrentThread() SAL_THROW_EXTERN_C()
#define SAL_THROW_EXTERN_C()
Job
void SAL_CALL uno_getIdOfCurrentThread(sal_Sequence **ppThreadId) SAL_THROW_EXTERN_C()
static void executeRequest(void *data)
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()
void SAL_CALL uno_threadpool_enter(uno_ThreadPool hPool, void **ppJob) SAL_THROW_EXTERN_C()
uno_Any a
void SAL_CALL uno_threadpool_destroy(uno_ThreadPool hPool) 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()
uno_ThreadPool SAL_CALL uno_threadpool_create() SAL_THROW_EXTERN_C()
void * p
void SAL_CALL uno_threadpool_dispose(uno_ThreadPool hPool) SAL_THROW_EXTERN_C()
void SAL_CALL uno_threadpool_attach(uno_ThreadPool hPool) SAL_THROW_EXTERN_C()
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 jlong JNICALL Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_create(JNIEnv *env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()