update dev300-m58
[ooovba.git] / bridges / source / jni_uno / nativethreadpool.cxx
blob173966a87a0e6b5cb17542d9db47759aab05336f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: nativethreadpool.cxx,v $
10 * $Revision: 1.6 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
34 #include "jvmaccess/virtualmachine.hxx"
35 #include "rtl/byteseq.h"
36 #include "rtl/byteseq.hxx"
37 #include "rtl/memory.h"
38 #include "rtl/ref.hxx"
39 #include "sal/types.h"
40 #include "uno/threadpool.h"
42 #include "jni.h"
44 #include <new>
46 /* The native implementation part of
47 * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
50 namespace {
52 struct Pool {
53 Pool(rtl::Reference< jvmaccess::VirtualMachine > const & theVirtualMachine,
54 jmethodID theExecute, uno_ThreadPool thePool):
55 virtualMachine(theVirtualMachine), execute(theExecute), pool(thePool) {}
57 rtl::Reference< jvmaccess::VirtualMachine > virtualMachine;
58 jmethodID execute;
59 uno_ThreadPool pool;
62 struct Job {
63 Job(Pool * thePool, jobject theJob): pool(thePool), job(theJob) {}
65 Pool * pool;
66 jobject job;
69 void throwOutOfMemory(JNIEnv * env) {
70 jclass c = env->FindClass("java/lang/OutOfMemoryError");
71 if (c != 0) {
72 env->ThrowNew(c, "");
78 extern "C" {
80 static void SAL_CALL executeRequest(void * data) {
81 Job * job = static_cast< Job * >(data);
82 try {
83 jvmaccess::VirtualMachine::AttachGuard guard(job->pool->virtualMachine);
84 JNIEnv * env = guard.getEnvironment();
85 // Failure of the following Job.execute Java call is ignored; if that
86 // call fails, it should be due to a java.lang.Error, which is not
87 // handled well, anyway:
88 env->CallObjectMethod(job->job, job->pool->execute);
89 env->DeleteGlobalRef(job->job);
90 delete job;
91 } catch (jvmaccess::VirtualMachine::AttachGuard::CreationException &) {
92 //TODO: DeleteGlobalRef(job->job)
93 delete job;
99 extern "C" JNIEXPORT jbyteArray JNICALL
100 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_threadId(
101 JNIEnv * env, jclass) SAL_THROW_EXTERN_C()
103 sal_Sequence * s = 0;
104 uno_getIdOfCurrentThread(&s); //TODO: out of memory
105 uno_releaseIdFromCurrentThread();
106 rtl::ByteSequence seq(s);
107 rtl_byte_sequence_release(s);
108 sal_Int32 n = seq.getLength();
109 jbyteArray a = env->NewByteArray(n);
110 // sal_Int32 and jsize are compatible here
111 if (a == 0) {
112 return 0;
114 void * p = env->GetPrimitiveArrayCritical(a, 0);
115 if (p == 0) {
116 return 0;
118 rtl_copyMemory(p, seq.getConstArray(), n);
119 // sal_Int8 and jbyte ought to be compatible
120 env->ReleasePrimitiveArrayCritical(a, p, 0);
121 return a;
124 extern "C" JNIEXPORT jlong JNICALL
125 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_create(
126 JNIEnv * env, jclass) SAL_THROW_EXTERN_C()
128 JavaVM * vm;
129 if (env->GetJavaVM(&vm) != JNI_OK) { //TODO: no Java exception raised?
130 jclass c = env->FindClass("java/lang/RuntimeException");
131 if (c != 0) {
132 env->ThrowNew(c, "JNI GetJavaVM failed");
134 return 0;
136 jclass c = env->FindClass("com/sun/star/lib/uno/environments/remote/Job");
137 if (c == 0) {
138 return 0;
140 jmethodID execute = env->GetMethodID(c, "execute", "()Ljava/lang/Object;");
141 if (execute == 0) {
142 return 0;
144 try {
145 return reinterpret_cast< jlong >(new Pool(
146 new jvmaccess::VirtualMachine(vm, env->GetVersion(), false, env),
147 execute, uno_threadpool_create()));
148 } catch (std::bad_alloc) {
149 throwOutOfMemory(env);
150 return 0;
154 extern "C" JNIEXPORT void JNICALL
155 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_attach(
156 JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
158 uno_threadpool_attach(reinterpret_cast< Pool * >(pool)->pool);
161 extern "C" JNIEXPORT jobject JNICALL
162 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_enter(
163 JNIEnv * env, jclass, jlong pool) SAL_THROW_EXTERN_C()
165 jobject job;
166 uno_threadpool_enter(
167 reinterpret_cast< Pool * >(pool)->pool,
168 reinterpret_cast< void ** >(&job));
169 if (job == 0) {
170 return 0;
172 jobject ref = env->NewLocalRef(job);
173 env->DeleteGlobalRef(job);
174 return ref;
177 extern "C" JNIEXPORT void JNICALL
178 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_detach(
179 JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
181 uno_threadpool_detach(reinterpret_cast< Pool * >(pool)->pool);
184 extern "C" JNIEXPORT void JNICALL
185 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_putJob(
186 JNIEnv * env, jclass, jlong pool, jbyteArray threadId, jobject job,
187 jboolean request, jboolean oneWay) SAL_THROW_EXTERN_C()
189 void * s = env->GetPrimitiveArrayCritical(threadId, 0);
190 if (s == 0) {
191 return;
193 rtl::ByteSequence seq(
194 static_cast< sal_Int8 * >(s), env->GetArrayLength(threadId));
195 // sal_Int8 and jbyte ought to be compatible; sal_Int32 and jsize are
196 // compatible here
197 //TODO: out of memory
198 env->ReleasePrimitiveArrayCritical(threadId, s, JNI_ABORT);
199 Pool * p = reinterpret_cast< Pool * >(pool);
200 jobject ref = env->NewGlobalRef(job);
201 if (ref == 0) {
202 return;
204 Job * j = 0;
205 if (request) {
206 j = new(std::nothrow) Job(p, ref);
207 if (j == 0) {
208 env->DeleteGlobalRef(ref);
209 throwOutOfMemory(env);
210 return;
213 uno_threadpool_putJob(
214 p->pool, seq.getHandle(),
215 request ? static_cast< void * >(j) : static_cast< void * >(ref),
216 request ? executeRequest : 0, oneWay);
219 extern "C" JNIEXPORT void JNICALL
220 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_dispose(
221 JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
223 uno_threadpool_dispose(reinterpret_cast< Pool * >(pool)->pool);
226 extern "C" JNIEXPORT void JNICALL
227 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_destroy(
228 JNIEnv *, jclass, jlong pool) SAL_THROW_EXTERN_C()
230 Pool * p = reinterpret_cast< Pool * >(pool);
231 uno_threadpool_destroy(p->pool);
232 delete p;