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