1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: nativethreadpool.cxx,v $
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"
46 /* The native implementation part of
47 * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
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
;
63 Job(Pool
* thePool
, jobject theJob
): pool(thePool
), job(theJob
) {}
69 void throwOutOfMemory(JNIEnv
* env
) {
70 jclass c
= env
->FindClass("java/lang/OutOfMemoryError");
80 static void SAL_CALL
executeRequest(void * data
) {
81 Job
* job
= static_cast< Job
* >(data
);
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
);
91 } catch (jvmaccess::VirtualMachine::AttachGuard::CreationException
&) {
92 //TODO: DeleteGlobalRef(job->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
114 void * p
= env
->GetPrimitiveArrayCritical(a
, 0);
118 rtl_copyMemory(p
, seq
.getConstArray(), n
);
119 // sal_Int8 and jbyte ought to be compatible
120 env
->ReleasePrimitiveArrayCritical(a
, p
, 0);
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()
129 if (env
->GetJavaVM(&vm
) != JNI_OK
) { //TODO: no Java exception raised?
130 jclass c
= env
->FindClass("java/lang/RuntimeException");
132 env
->ThrowNew(c
, "JNI GetJavaVM failed");
136 jclass c
= env
->FindClass("com/sun/star/lib/uno/environments/remote/Job");
140 jmethodID execute
= env
->GetMethodID(c
, "execute", "()Ljava/lang/Object;");
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
);
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()
166 uno_threadpool_enter(
167 reinterpret_cast< Pool
* >(pool
)->pool
,
168 reinterpret_cast< void ** >(&job
));
172 jobject ref
= env
->NewLocalRef(job
);
173 env
->DeleteGlobalRef(job
);
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);
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
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
);
206 j
= new(std::nothrow
) Job(p
, ref
);
208 env
->DeleteGlobalRef(ref
);
209 throwOutOfMemory(env
);
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
);