Bump for 3.6-28
[LibreOffice.git] / bridges / source / jni_uno / nativethreadpool.cxx
blob8ed0d9ee27a7d371fc5aa71614ba7f5337d16832
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 #include "jvmaccess/virtualmachine.hxx"
31 #include "rtl/byteseq.h"
32 #include "rtl/byteseq.hxx"
33 #include "rtl/memory.h"
34 #include "rtl/ref.hxx"
35 #include "sal/types.h"
36 #include "uno/threadpool.h"
38 #include "jni.h"
40 #include <new>
42 /* The native implementation part of
43 * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
46 namespace {
48 struct Pool {
49 Pool(rtl::Reference< jvmaccess::VirtualMachine > const & theVirtualMachine,
50 jmethodID theExecute, uno_ThreadPool thePool):
51 virtualMachine(theVirtualMachine), execute(theExecute), pool(thePool) {}
53 rtl::Reference< jvmaccess::VirtualMachine > virtualMachine;
54 jmethodID execute;
55 uno_ThreadPool pool;
58 struct Job {
59 Job(Pool * thePool, jobject theJob): pool(thePool), job(theJob) {}
61 Pool * pool;
62 jobject job;
65 void throwOutOfMemory(JNIEnv * env) {
66 jclass c = env->FindClass("java/lang/OutOfMemoryError");
67 if (c != 0) {
68 env->ThrowNew(c, "");
74 extern "C" {
76 static void SAL_CALL executeRequest(void * data) {
77 Job * job = static_cast< Job * >(data);
78 try {
79 jvmaccess::VirtualMachine::AttachGuard guard(job->pool->virtualMachine);
80 JNIEnv * env = guard.getEnvironment();
81 // Failure of the following Job.execute Java call is ignored; if that
82 // call fails, it should be due to a java.lang.Error, which is not
83 // handled well, anyway:
84 env->CallObjectMethod(job->job, job->pool->execute);
85 env->DeleteGlobalRef(job->job);
86 delete job;
87 } catch (const jvmaccess::VirtualMachine::AttachGuard::CreationException &) {
88 //TODO: DeleteGlobalRef(job->job)
89 delete job;
95 extern "C" JNIEXPORT jbyteArray JNICALL
96 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_threadId(
97 JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
99 sal_Sequence * s = 0;
100 uno_getIdOfCurrentThread(&s); //TODO: out of memory
101 uno_releaseIdFromCurrentThread();
102 rtl::ByteSequence seq(s);
103 rtl_byte_sequence_release(s);
104 sal_Int32 n = seq.getLength();
105 jbyteArray a = env->NewByteArray(n);
106 // sal_Int32 and jsize are compatible here
107 if (a == 0) {
108 return 0;
110 void * p = env->GetPrimitiveArrayCritical(a, 0);
111 if (p == 0) {
112 return 0;
114 rtl_copyMemory(p, seq.getConstArray(), n);
115 // sal_Int8 and jbyte ought to be compatible
116 env->ReleasePrimitiveArrayCritical(a, p, 0);
117 return a;
120 extern "C" JNIEXPORT jlong JNICALL
121 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_create(
122 JNIEnv * env, SAL_UNUSED_PARAMETER jclass) SAL_THROW_EXTERN_C()
124 JavaVM * vm;
125 if (env->GetJavaVM(&vm) != JNI_OK) { //TODO: no Java exception raised?
126 jclass c = env->FindClass("java/lang/RuntimeException");
127 if (c != 0) {
128 env->ThrowNew(c, "JNI GetJavaVM failed");
130 return 0;
132 jclass c = env->FindClass("com/sun/star/lib/uno/environments/remote/Job");
133 if (c == 0) {
134 return 0;
136 jmethodID execute = env->GetMethodID(c, "execute", "()Ljava/lang/Object;");
137 if (execute == 0) {
138 return 0;
140 try {
141 return reinterpret_cast< jlong >(new Pool(
142 new jvmaccess::VirtualMachine(vm, env->GetVersion(), false, env),
143 execute, uno_threadpool_create()));
144 } catch (const std::bad_alloc &) {
145 throwOutOfMemory(env);
146 return 0;
150 extern "C" JNIEXPORT void JNICALL
151 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_attach(
152 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
153 SAL_THROW_EXTERN_C()
155 uno_threadpool_attach(reinterpret_cast< Pool * >(pool)->pool);
158 extern "C" JNIEXPORT jobject JNICALL
159 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_enter(
160 JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool) SAL_THROW_EXTERN_C()
162 jobject job;
163 uno_threadpool_enter(
164 reinterpret_cast< Pool * >(pool)->pool,
165 reinterpret_cast< void ** >(&job));
166 if (job == 0) {
167 return 0;
169 jobject ref = env->NewLocalRef(job);
170 env->DeleteGlobalRef(job);
171 return ref;
174 extern "C" JNIEXPORT void JNICALL
175 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_detach(
176 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
177 SAL_THROW_EXTERN_C()
179 uno_threadpool_detach(reinterpret_cast< Pool * >(pool)->pool);
182 extern "C" JNIEXPORT void JNICALL
183 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_putJob(
184 JNIEnv * env, SAL_UNUSED_PARAMETER jclass, jlong pool, jbyteArray threadId,
185 jobject job, jboolean request, jboolean oneWay) SAL_THROW_EXTERN_C()
187 void * s = env->GetPrimitiveArrayCritical(threadId, 0);
188 if (s == 0) {
189 return;
191 rtl::ByteSequence seq(
192 static_cast< sal_Int8 * >(s), env->GetArrayLength(threadId));
193 // sal_Int8 and jbyte ought to be compatible; sal_Int32 and jsize are
194 // compatible here
195 //TODO: out of memory
196 env->ReleasePrimitiveArrayCritical(threadId, s, JNI_ABORT);
197 Pool * p = reinterpret_cast< Pool * >(pool);
198 jobject ref = env->NewGlobalRef(job);
199 if (ref == 0) {
200 return;
202 Job * j = 0;
203 if (request) {
204 j = new(std::nothrow) Job(p, ref);
205 if (j == 0) {
206 env->DeleteGlobalRef(ref);
207 throwOutOfMemory(env);
208 return;
211 uno_threadpool_putJob(
212 p->pool, seq.getHandle(),
213 request ? static_cast< void * >(j) : static_cast< void * >(ref),
214 request ? executeRequest : 0, oneWay);
217 extern "C" JNIEXPORT void JNICALL
218 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_dispose(
219 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
220 SAL_THROW_EXTERN_C()
222 uno_threadpool_dispose(reinterpret_cast< Pool * >(pool)->pool);
225 extern "C" JNIEXPORT void JNICALL
226 Java_com_sun_star_lib_uno_environments_remote_NativeThreadPool_destroy(
227 SAL_UNUSED_PARAMETER JNIEnv *, SAL_UNUSED_PARAMETER jclass, jlong pool)
228 SAL_THROW_EXTERN_C()
230 Pool * p = reinterpret_cast< Pool * >(pool);
231 uno_threadpool_destroy(p->pool);
232 delete p;
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */