xref: /AOO41X/main/jurt/com/sun/star/lib/uno/environments/remote/NativeThreadPool.java (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 package com.sun.star.lib.uno.environments.remote;
29 
30 final class NativeThreadPool implements IThreadPool {
31     public NativeThreadPool() {
32         pool = create();
33     }
34 
35     public ThreadId getThreadId() {
36         return new ThreadId(threadId());
37     }
38 
39     public void attach() {
40         attach(pool);
41     }
42 
43     public Object attach(ThreadId id) {
44         attach();
45         return null;
46     }
47 
48     public void detach() {
49         detach(pool);
50     }
51 
52     public void detach(Object handle, ThreadId id) {
53         detach();
54     }
55 
56     public Object enter() throws Throwable {
57         Job job = enter(pool);
58         if (job == null) {
59             throw dispose;
60         }
61         return job.execute();
62     }
63 
64     public Object enter(Object handle, ThreadId id) throws Throwable {
65         return enter();
66     }
67 
68     public void putJob(Job job) {
69         putJob(
70             pool, job.getThreadId().getBytes(), job, job.isRequest(),
71             job.isRequest() && !job.isSynchronous());
72     }
73 
74     public void dispose(Throwable throwable) {
75         dispose = throwable;
76         dispose(pool);
77     }
78 
79     public void destroy() {
80         destroy(pool);
81     }
82 
83     // The native implementation is in
84     // bridges/source/jni_uno/nativethreadpool.cxx:
85     static {
86         System.loadLibrary("java_uno");
87     }
88     private static native byte[] threadId();
89     private static native long create();
90     private static native void attach(long pool);
91     private static native Job enter(long pool);
92     private static native void detach(long pool);
93     private static native void putJob(
94         long pool, byte[] threadId, Job job, boolean request, boolean oneWay);
95     private static native void dispose(long pool);
96     private static native void destroy(long pool);
97 
98     private final long pool;
99     private volatile Throwable dispose;
100 }
101