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