1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 package com.sun.star.lib.uno.environments.remote;
25 
26 final class NativeThreadPool implements IThreadPool {
NativeThreadPool()27     public NativeThreadPool() {
28         pool = create();
29     }
30 
getThreadId()31     public ThreadId getThreadId() {
32         return new ThreadId(threadId());
33     }
34 
attach()35     public void attach() {
36         attach(pool);
37     }
38 
attach(ThreadId id)39     public Object attach(ThreadId id) {
40         attach();
41         return null;
42     }
43 
detach()44     public void detach() {
45         detach(pool);
46     }
47 
detach(Object handle, ThreadId id)48     public void detach(Object handle, ThreadId id) {
49         detach();
50     }
51 
enter()52     public Object enter() throws Throwable {
53         Job job = enter(pool);
54         if (job == null) {
55             throw dispose;
56         }
57         return job.execute();
58     }
59 
enter(Object handle, ThreadId id)60     public Object enter(Object handle, ThreadId id) throws Throwable {
61         return enter();
62     }
63 
putJob(Job job)64     public void putJob(Job job) {
65         putJob(
66             pool, job.getThreadId().getBytes(), job, job.isRequest(),
67             job.isRequest() && !job.isSynchronous());
68     }
69 
dispose(Throwable throwable)70     public void dispose(Throwable throwable) {
71         dispose = throwable;
72         dispose(pool);
73     }
74 
destroy()75     public void destroy() {
76         destroy(pool);
77     }
78 
79     // The native implementation is in
80     // bridges/source/jni_uno/nativethreadpool.cxx:
81     static {
82         System.loadLibrary("java_uno");
83     }
threadId()84     private static native byte[] threadId();
create()85     private static native long create();
attach(long pool)86     private static native void attach(long pool);
enter(long pool)87     private static native Job enter(long pool);
detach(long pool)88     private static native void detach(long pool);
putJob( long pool, byte[] threadId, Job job, boolean request, boolean oneWay)89     private static native void putJob(
90         long pool, byte[] threadId, Job job, boolean request, boolean oneWay);
dispose(long pool)91     private static native void dispose(long pool);
destroy(long pool)92     private static native void destroy(long pool);
93 
94     private final long pool;
95     private volatile Throwable dispose;
96 }
97