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