1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski // MARKER(update_precomp.py): autogen include statement, do not remove
25*b1cdbd2cSJim Jagielski #include "precompiled_bridges.hxx"
26*b1cdbd2cSJim Jagielski
27*b1cdbd2cSJim Jagielski #include "jvmaccess/virtualmachine.hxx"
28*b1cdbd2cSJim Jagielski #include "rtl/byteseq.h"
29*b1cdbd2cSJim Jagielski #include "rtl/byteseq.hxx"
30*b1cdbd2cSJim Jagielski #include "rtl/memory.h"
31*b1cdbd2cSJim Jagielski #include "rtl/ref.hxx"
32*b1cdbd2cSJim Jagielski #include "sal/types.h"
33*b1cdbd2cSJim Jagielski #include "uno/threadpool.h"
34*b1cdbd2cSJim Jagielski
35*b1cdbd2cSJim Jagielski #include "jni.h"
36*b1cdbd2cSJim Jagielski
37*b1cdbd2cSJim Jagielski #include <new>
38*b1cdbd2cSJim Jagielski
39*b1cdbd2cSJim Jagielski /* The native implementation part of
40*b1cdbd2cSJim Jagielski * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
41*b1cdbd2cSJim Jagielski */
42*b1cdbd2cSJim Jagielski
43*b1cdbd2cSJim Jagielski namespace {
44*b1cdbd2cSJim Jagielski
45*b1cdbd2cSJim Jagielski struct Pool {
Pool__anon390481890111::Pool46*b1cdbd2cSJim Jagielski Pool(rtl::Reference< jvmaccess::VirtualMachine > const & theVirtualMachine,
47*b1cdbd2cSJim Jagielski jmethodID theExecute, uno_ThreadPool thePool):
48*b1cdbd2cSJim Jagielski virtualMachine(theVirtualMachine), execute(theExecute), pool(thePool) {}
49*b1cdbd2cSJim Jagielski
50*b1cdbd2cSJim Jagielski rtl::Reference< jvmaccess::VirtualMachine > virtualMachine;
51*b1cdbd2cSJim Jagielski jmethodID execute;
52*b1cdbd2cSJim Jagielski uno_ThreadPool pool;
53*b1cdbd2cSJim Jagielski };
54*b1cdbd2cSJim Jagielski
55*b1cdbd2cSJim Jagielski struct Job {
Job__anon390481890111::Job56*b1cdbd2cSJim Jagielski Job(Pool * thePool, jobject theJob): pool(thePool), job(theJob) {}
57*b1cdbd2cSJim Jagielski
58*b1cdbd2cSJim Jagielski Pool * pool;
59*b1cdbd2cSJim Jagielski jobject job;
60*b1cdbd2cSJim Jagielski };
61*b1cdbd2cSJim Jagielski
throwOutOfMemory(JNIEnv * env)62*b1cdbd2cSJim Jagielski void throwOutOfMemory(JNIEnv * env) {
63*b1cdbd2cSJim Jagielski jclass c = env->FindClass("java/lang/OutOfMemoryError");
64*b1cdbd2cSJim Jagielski if (c != 0) {
65*b1cdbd2cSJim Jagielski env->ThrowNew(c, "");
66*b1cdbd2cSJim Jagielski }
67*b1cdbd2cSJim Jagielski }
68*b1cdbd2cSJim Jagielski
69*b1cdbd2cSJim Jagielski }
70*b1cdbd2cSJim Jagielski
71*b1cdbd2cSJim Jagielski extern "C" {
72*b1cdbd2cSJim Jagielski
executeRequest(void * data)73*b1cdbd2cSJim Jagielski static void SAL_CALL executeRequest(void * data) {
74*b1cdbd2cSJim Jagielski Job * job = static_cast< Job * >(data);
75*b1cdbd2cSJim Jagielski try {
76*b1cdbd2cSJim Jagielski jvmaccess::VirtualMachine::AttachGuard guard(job->pool->virtualMachine);
77*b1cdbd2cSJim Jagielski JNIEnv * env = guard.getEnvironment();
78*b1cdbd2cSJim Jagielski // Failure of the following Job.execute Java call is ignored; if that
79*b1cdbd2cSJim Jagielski // call fails, it should be due to a java.lang.Error, which is not
80*b1cdbd2cSJim Jagielski // handled well, anyway:
81*b1cdbd2cSJim Jagielski env->CallObjectMethod(job->job, job->pool->execute);
82*b1cdbd2cSJim Jagielski env->DeleteGlobalRef(job->job);
83*b1cdbd2cSJim Jagielski delete job;
84*b1cdbd2cSJim Jagielski } catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &) {
85*b1cdbd2cSJim Jagielski //TODO: DeleteGlobalRef(job->job)
86*b1cdbd2cSJim Jagielski delete job;
87*b1cdbd2cSJim Jagielski }
88*b1cdbd2cSJim Jagielski }
89*b1cdbd2cSJim Jagielski
90*b1cdbd2cSJim Jagielski }
91*b1cdbd2cSJim Jagielski
92*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT jbyteArray JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_threadId(JNIEnv * env,jclass)93*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_threadId(
94*b1cdbd2cSJim Jagielski JNIEnv * env, jclass) SAL_THROW_EXTERN_C()
95*b1cdbd2cSJim Jagielski {
96*b1cdbd2cSJim Jagielski sal_Sequence * s = 0;
97*b1cdbd2cSJim Jagielski uno_getIdOfCurrentThread(&s); //TODO: out of memory
98*b1cdbd2cSJim Jagielski uno_releaseIdFromCurrentThread();
99*b1cdbd2cSJim Jagielski rtl::ByteSequence seq(s);
100*b1cdbd2cSJim Jagielski rtl_byte_sequence_release(s);
101*b1cdbd2cSJim Jagielski sal_Int32 n = seq.getLength();
102*b1cdbd2cSJim Jagielski jbyteArray a = env->NewByteArray(n);
103*b1cdbd2cSJim Jagielski // sal_Int32 and jsize are compatible here
104*b1cdbd2cSJim Jagielski if (a == 0) {
105*b1cdbd2cSJim Jagielski return 0;
106*b1cdbd2cSJim Jagielski }
107*b1cdbd2cSJim Jagielski void * p = env->GetPrimitiveArrayCritical(a, 0);
108*b1cdbd2cSJim Jagielski if (p == 0) {
109*b1cdbd2cSJim Jagielski return 0;
110*b1cdbd2cSJim Jagielski }
111*b1cdbd2cSJim Jagielski rtl_copyMemory(p, seq.getConstArray(), n);
112*b1cdbd2cSJim Jagielski // sal_Int8 and jbyte ought to be compatible
113*b1cdbd2cSJim Jagielski env->ReleasePrimitiveArrayCritical(a, p, 0);
114*b1cdbd2cSJim Jagielski return a;
115*b1cdbd2cSJim Jagielski }
116*b1cdbd2cSJim Jagielski
117*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT jlong JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_create(JNIEnv * env,jclass)118*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_create(
119*b1cdbd2cSJim Jagielski JNIEnv * env, jclass) SAL_THROW_EXTERN_C()
120*b1cdbd2cSJim Jagielski {
121*b1cdbd2cSJim Jagielski JavaVM * vm;
122*b1cdbd2cSJim Jagielski if (env->GetJavaVM(&vm) != JNI_OK) { //TODO: no Java exception raised?
123*b1cdbd2cSJim Jagielski jclass c = env->FindClass("java/lang/RuntimeException");
124*b1cdbd2cSJim Jagielski if (c != 0) {
125*b1cdbd2cSJim Jagielski env->ThrowNew(c, "JNI GetJavaVM failed");
126*b1cdbd2cSJim Jagielski }
127*b1cdbd2cSJim Jagielski return 0;
128*b1cdbd2cSJim Jagielski }
129*b1cdbd2cSJim Jagielski jclass c = env->FindClass("com/sun/star/lib/uno/environments/remote/Job");
130*b1cdbd2cSJim Jagielski if (c == 0) {
131*b1cdbd2cSJim Jagielski return 0;
132*b1cdbd2cSJim Jagielski }
133*b1cdbd2cSJim Jagielski jmethodID execute = env->GetMethodID(c, "execute", "()Ljava/lang/Object;");
134*b1cdbd2cSJim Jagielski if (execute == 0) {
135*b1cdbd2cSJim Jagielski return 0;
136*b1cdbd2cSJim Jagielski }
137*b1cdbd2cSJim Jagielski try {
138*b1cdbd2cSJim Jagielski return reinterpret_cast< jlong >(new Pool(
139*b1cdbd2cSJim Jagielski new jvmaccess::VirtualMachine(vm, env->GetVersion(), false, env),
140*b1cdbd2cSJim Jagielski execute, uno_threadpool_create()));
141*b1cdbd2cSJim Jagielski } catch (std::bad_alloc) {
142*b1cdbd2cSJim Jagielski throwOutOfMemory(env);
143*b1cdbd2cSJim Jagielski return 0;
144*b1cdbd2cSJim Jagielski }
145*b1cdbd2cSJim Jagielski }
146*b1cdbd2cSJim Jagielski
147*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT void JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_attach(JNIEnv *,jclass,jlong pool)148*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_attach(
149*b1cdbd2cSJim Jagielski JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
150*b1cdbd2cSJim Jagielski {
151*b1cdbd2cSJim Jagielski uno_threadpool_attach(reinterpret_cast< Pool * >(pool)->pool);
152*b1cdbd2cSJim Jagielski }
153*b1cdbd2cSJim Jagielski
154*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT jobject JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_enter(JNIEnv * env,jclass,jlong pool)155*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_enter(
156*b1cdbd2cSJim Jagielski JNIEnv * env, jclass, jlong pool) SAL_THROW_EXTERN_C()
157*b1cdbd2cSJim Jagielski {
158*b1cdbd2cSJim Jagielski jobject job;
159*b1cdbd2cSJim Jagielski uno_threadpool_enter(
160*b1cdbd2cSJim Jagielski reinterpret_cast< Pool * >(pool)->pool,
161*b1cdbd2cSJim Jagielski reinterpret_cast< void ** >(&job));
162*b1cdbd2cSJim Jagielski if (job == 0) {
163*b1cdbd2cSJim Jagielski return 0;
164*b1cdbd2cSJim Jagielski }
165*b1cdbd2cSJim Jagielski jobject ref = env->NewLocalRef(job);
166*b1cdbd2cSJim Jagielski env->DeleteGlobalRef(job);
167*b1cdbd2cSJim Jagielski return ref;
168*b1cdbd2cSJim Jagielski }
169*b1cdbd2cSJim Jagielski
170*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT void JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_detach(JNIEnv *,jclass,jlong pool)171*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_detach(
172*b1cdbd2cSJim Jagielski JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
173*b1cdbd2cSJim Jagielski {
174*b1cdbd2cSJim Jagielski uno_threadpool_detach(reinterpret_cast< Pool * >(pool)->pool);
175*b1cdbd2cSJim Jagielski }
176*b1cdbd2cSJim Jagielski
177*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT void JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_putJob(JNIEnv * env,jclass,jlong pool,jbyteArray threadId,jobject job,jboolean request,jboolean oneWay)178*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_putJob(
179*b1cdbd2cSJim Jagielski JNIEnv * env, jclass, jlong pool, jbyteArray threadId, jobject job,
180*b1cdbd2cSJim Jagielski jboolean request, jboolean oneWay) SAL_THROW_EXTERN_C()
181*b1cdbd2cSJim Jagielski {
182*b1cdbd2cSJim Jagielski void * s = env->GetPrimitiveArrayCritical(threadId, 0);
183*b1cdbd2cSJim Jagielski if (s == 0) {
184*b1cdbd2cSJim Jagielski return;
185*b1cdbd2cSJim Jagielski }
186*b1cdbd2cSJim Jagielski rtl::ByteSequence seq(
187*b1cdbd2cSJim Jagielski static_cast< sal_Int8 * >(s), env->GetArrayLength(threadId));
188*b1cdbd2cSJim Jagielski // sal_Int8 and jbyte ought to be compatible; sal_Int32 and jsize are
189*b1cdbd2cSJim Jagielski // compatible here
190*b1cdbd2cSJim Jagielski //TODO: out of memory
191*b1cdbd2cSJim Jagielski env->ReleasePrimitiveArrayCritical(threadId, s, JNI_ABORT);
192*b1cdbd2cSJim Jagielski Pool * p = reinterpret_cast< Pool * >(pool);
193*b1cdbd2cSJim Jagielski jobject ref = env->NewGlobalRef(job);
194*b1cdbd2cSJim Jagielski if (ref == 0) {
195*b1cdbd2cSJim Jagielski return;
196*b1cdbd2cSJim Jagielski }
197*b1cdbd2cSJim Jagielski Job * j = 0;
198*b1cdbd2cSJim Jagielski if (request) {
199*b1cdbd2cSJim Jagielski j = new(std::nothrow) Job(p, ref);
200*b1cdbd2cSJim Jagielski if (j == 0) {
201*b1cdbd2cSJim Jagielski env->DeleteGlobalRef(ref);
202*b1cdbd2cSJim Jagielski throwOutOfMemory(env);
203*b1cdbd2cSJim Jagielski return;
204*b1cdbd2cSJim Jagielski }
205*b1cdbd2cSJim Jagielski }
206*b1cdbd2cSJim Jagielski uno_threadpool_putJob(
207*b1cdbd2cSJim Jagielski p->pool, seq.getHandle(),
208*b1cdbd2cSJim Jagielski request ? static_cast< void * >(j) : static_cast< void * >(ref),
209*b1cdbd2cSJim Jagielski request ? executeRequest : 0, oneWay);
210*b1cdbd2cSJim Jagielski }
211*b1cdbd2cSJim Jagielski
212*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT void JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_dispose(JNIEnv *,jclass,jlong pool)213*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_dispose(
214*b1cdbd2cSJim Jagielski JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
215*b1cdbd2cSJim Jagielski {
216*b1cdbd2cSJim Jagielski uno_threadpool_dispose(reinterpret_cast< Pool * >(pool)->pool);
217*b1cdbd2cSJim Jagielski }
218*b1cdbd2cSJim Jagielski
219*b1cdbd2cSJim Jagielski extern "C" JNIEXPORT void JNICALL
Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_destroy(JNIEnv *,jclass,jlong pool)220*b1cdbd2cSJim Jagielski Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_destroy(
221*b1cdbd2cSJim Jagielski JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
222*b1cdbd2cSJim Jagielski {
223*b1cdbd2cSJim Jagielski Pool * p = reinterpret_cast< Pool * >(pool);
224*b1cdbd2cSJim Jagielski uno_threadpool_destroy(p->pool);
225*b1cdbd2cSJim Jagielski delete p;
226*b1cdbd2cSJim Jagielski }
227