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"
42 /* The native implementation part of
43 * jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java.
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
;
59 Job(Pool
* thePool
, jobject theJob
): pool(thePool
), job(theJob
) {}
65 void throwOutOfMemory(JNIEnv
* env
) {
66 jclass c
= env
->FindClass("java/lang/OutOfMemoryError");
76 static void SAL_CALL
executeRequest(void * data
) {
77 Job
* job
= static_cast< Job
* >(data
);
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
);
87 } catch (const jvmaccess::VirtualMachine::AttachGuard::CreationException
&) {
88 //TODO: DeleteGlobalRef(job->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()
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
110 void * p
= env
->GetPrimitiveArrayCritical(a
, 0);
114 rtl_copyMemory(p
, seq
.getConstArray(), n
);
115 // sal_Int8 and jbyte ought to be compatible
116 env
->ReleasePrimitiveArrayCritical(a
, p
, 0);
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()
125 if (env
->GetJavaVM(&vm
) != JNI_OK
) { //TODO: no Java exception raised?
126 jclass c
= env
->FindClass("java/lang/RuntimeException");
128 env
->ThrowNew(c
, "JNI GetJavaVM failed");
132 jclass c
= env
->FindClass("com/sun/star/lib/uno/environments/remote/Job");
136 jmethodID execute
= env
->GetMethodID(c
, "execute", "()Ljava/lang/Object;");
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
);
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
)
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()
163 uno_threadpool_enter(
164 reinterpret_cast< Pool
* >(pool
)->pool
,
165 reinterpret_cast< void ** >(&job
));
169 jobject ref
= env
->NewLocalRef(job
);
170 env
->DeleteGlobalRef(job
);
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
)
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);
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
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
);
204 j
= new(std::nothrow
) Job(p
, ref
);
206 env
->DeleteGlobalRef(ref
);
207 throwOutOfMemory(env
);
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
)
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
)
230 Pool
* p
= reinterpret_cast< Pool
* >(pool
);
231 uno_threadpool_destroy(p
->pool
);
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */