2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.lib
.uno
.environments
.remote
;
22 * This class implements a java thread pool.
24 * @see com.sun.star.uno.UnoRuntime
25 * @see com.sun.star.lib.uno.environments.remote.ThreadPool
26 * @see com.sun.star.lib.uno.environments.remote.IThreadPool
27 * @see com.sun.star.lib.uno.environments.remote.Job
28 * @see com.sun.star.lib.uno.environments.remote.JobQueue
31 public class JavaThreadPool
implements IThreadPool
{
33 * When set to true, enables various debugging output.
35 private static final boolean DEBUG
= false;
37 JavaThreadPoolFactory _javaThreadPoolFactory
;
39 JavaThreadPool(JavaThreadPoolFactory javaThreadPoolFactory
) {
40 _javaThreadPoolFactory
= javaThreadPoolFactory
;
43 public ThreadId
getThreadId() {
44 return JavaThreadPoolFactory
.getThreadId();
47 public Object
attach( ThreadId threadId
)
49 if(DEBUG
) System
.err
.println("##### " + getClass().getName() + ".attach - id:" + threadId
);
50 JobQueue jobQueue
= _javaThreadPoolFactory
.getJobQueue(threadId
);
52 jobQueue
= new JobQueue(_javaThreadPoolFactory
, threadId
, false);
54 // acquiring the jobQueue registers it at the ThreadPoolFactory
59 public void attach() {
60 attach( getThreadId() );
63 public void detach( Object handle
, ThreadId id
)
65 ((JobQueue
)handle
).release();
68 public void detach() {
69 ThreadId threadId
= getThreadId();
70 detach(_javaThreadPoolFactory
.getJobQueue(threadId
), threadId
);
74 public Object
enter( ) throws Throwable
{
75 ThreadId threadId
= getThreadId();
76 return enter( _javaThreadPoolFactory
.getJobQueue( threadId
), threadId
);
79 public Object
enter( Object handle
, ThreadId threadId
) throws Throwable
{
80 return ((JobQueue
)handle
).enter(this);
83 public void putJob(Job job
) {
84 if (!job
.isRequest() || job
.isSynchronous()) {
85 JobQueue jobQueue
= _javaThreadPoolFactory
.getJobQueue(job
.getThreadId());
87 // this has not be synchronized, cause
88 // sync jobs can only come over one bridge
89 // (cause the thread blocks on other side)
91 jobQueue
= new JobQueue(_javaThreadPoolFactory
, job
.getThreadId(), true);
93 // put job acquires the queue and registers it at the ThreadPoolFactory
94 jobQueue
.putJob(job
, this);
97 // this has to be synchronized, cause
98 // async jobs of the same thread can come
99 // over different bridges
100 synchronized(_javaThreadPoolFactory
) {
101 JobQueue async_jobQueue
= _javaThreadPoolFactory
.getAsyncJobQueue(job
.getThreadId());
103 // ensure there is jobQueue
104 if(async_jobQueue
== null) // so, there is really no async queue
105 async_jobQueue
= new JobQueue(_javaThreadPoolFactory
, job
.getThreadId());
107 // put job acquires the queue and registers it at the ThreadPoolFactory
108 async_jobQueue
.putJob(job
, this);
113 public void dispose(Throwable throwable
) {
114 if(DEBUG
) System
.err
.println("##### " + getClass().getName() + ".dispose:" + throwable
);
116 _javaThreadPoolFactory
.dispose(this, throwable
);
119 public void destroy() {