xref: /AOO41X/main/jurt/com/sun/star/lib/uno/environments/remote/JavaThreadPool.java (revision 2be432768a66cc90838f6a32e76ec156f587e741)
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 /**
27  * This class implements a java thread pool.
28  * <p>
29  * @version     $Revision: 1.13 $ $ $Date: 2008-04-11 11:20:22 $
30  * @author      Kay Ramme
31  * @see         com.sun.star.uno.UnoRuntime
32  * @see         com.sun.star.lib.uno.environments.remote.ThreadPool
33  * @see         com.sun.star.lib.uno.environments.remote.IThreadPool
34  * @see         com.sun.star.lib.uno.environments.remote.Job
35  * @see         com.sun.star.lib.uno.environments.remote.JobQueue
36  * @since       UDK1.0
37  */
38 public class JavaThreadPool implements IThreadPool {
39     /**
40      * When set to true, enables various debugging output.
41      */
42     private static final boolean DEBUG = false;
43 
44     JavaThreadPoolFactory _javaThreadPoolFactory;
45 
JavaThreadPool(JavaThreadPoolFactory javaThreadPoolFactory)46     JavaThreadPool(JavaThreadPoolFactory javaThreadPoolFactory) {
47         _javaThreadPoolFactory = javaThreadPoolFactory;
48     }
49 
getThreadId()50     public ThreadId getThreadId() {
51         return JavaThreadPoolFactory.getThreadId();
52     }
53 
attach( ThreadId threadId )54     public Object attach( ThreadId threadId )
55     {
56         if(DEBUG) System.err.println("##### " + getClass().getName() + ".attach - id:" + threadId);
57         JobQueue jobQueue = _javaThreadPoolFactory.getJobQueue(threadId);
58         if(jobQueue == null)
59             jobQueue = new JobQueue(_javaThreadPoolFactory, threadId, false);
60 
61         // acquiring the jobQueue registers it at the ThreadPoolFactory
62         jobQueue.acquire();
63         return jobQueue;
64     }
65 
attach()66     public void attach() {
67         attach( getThreadId() );
68     }
69 
detach( Object handle, ThreadId id )70     public void detach( Object handle, ThreadId id )
71     {
72         ((JobQueue)handle).release();
73     }
74 
detach()75     public void detach() {
76         ThreadId threadId =  getThreadId();
77         detach(_javaThreadPoolFactory.getJobQueue(threadId), threadId );
78     }
79 
80 
enter( )81     public Object enter( ) throws Throwable {
82         ThreadId threadId = getThreadId();
83         return enter( _javaThreadPoolFactory.getJobQueue( threadId ), threadId  );
84     }
85 
enter( Object handle, ThreadId threadId )86     public Object enter( Object handle, ThreadId threadId ) throws Throwable {
87         return ((JobQueue)handle).enter(this);
88     }
89 
putJob(Job job)90     public void putJob(Job job) {
91         if (!job.isRequest() || job.isSynchronous()) {
92             JobQueue jobQueue = _javaThreadPoolFactory.getJobQueue(job.getThreadId());
93 
94             // this has not be synchronized, cause
95             // sync jobs can only come over one bridge
96             // (cause the thread blocks on other side)
97             if(jobQueue == null)
98                 jobQueue = new JobQueue(_javaThreadPoolFactory, job.getThreadId(), true);
99 
100             // put job acquires the queue and registers it at the ThreadPoolFactory
101             jobQueue.putJob(job, this);
102         }
103         else {
104             // this has to be synchronized, cause
105             // async jobs of the same thread can come
106             // over different bridges
107             synchronized(_javaThreadPoolFactory) {
108                 JobQueue async_jobQueue = _javaThreadPoolFactory.getAsyncJobQueue(job.getThreadId());
109 
110                 // ensure there is jobQueue
111                 if(async_jobQueue == null) // so, there is really no async queue
112                     async_jobQueue = new JobQueue(_javaThreadPoolFactory, job.getThreadId());
113 
114                 // put job acquires the queue and registers it at the ThreadPoolFactory
115                 async_jobQueue.putJob(job, this);
116             }
117         }
118     }
119 
dispose(Throwable throwable)120     public void dispose(Throwable throwable) {
121         if(DEBUG) System.err.println("##### " + getClass().getName() + ".dispose:" + throwable);
122 
123         _javaThreadPoolFactory.dispose(this, throwable);
124     }
125 
destroy()126     public void destroy() {
127     }
128 }
129