KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > core > syncinfo > ReentrantLock


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.core.syncinfo;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.team.core.TeamException;
20 import org.eclipse.team.internal.core.subscribers.BatchingLock;
21
22 /**
23  * Provides a per-thread nested locking mechanism. A thread can acquire a
24  * lock on a specific resource by calling acquire(). Subsequently, acquire() can be called
25  * multiple times on the resource or any of its children from within the same thread
26  * without blocking. Other threads that try
27  * and acquire the lock on those same resources will be blocked until the first
28  * thread releases all it's nested locks.
29  * <p>
30  * The locking is managed by the platform via scheduling rules. This class simply
31  * provides the nesting mechnism in order to allow the client to determine when
32  * the lock for the thread has been released. Therefore, this lock will block if
33  * another thread already locks the same resource.</p>
34  */

35 public class ReentrantLock extends BatchingLock {
36     
37     public class CVSThreadInfo extends ThreadInfo{
38         private Set JavaDoc changedFolders = new HashSet JavaDoc();
39         public CVSThreadInfo(IFlushOperation operation) {
40             super(operation);
41         }
42         public void addChangedFolder(IContainer container) {
43             changedFolders.add(container);
44         }
45         public boolean isEmpty() {
46             return changedFolders.isEmpty() && super.isEmpty();
47         }
48         public IContainer[] getChangedFolders() {
49             return (IContainer[]) changedFolders.toArray(new IContainer[changedFolders.size()]);
50         }
51         public void flush(IProgressMonitor monitor) throws TeamException {
52             try {
53                 super.flush(monitor);
54             } finally {
55                 // We have to clear the resources no matter what since the next attempt
56
// to flush may not have an appropriate scheduling rule
57
changedFolders.clear();
58             }
59         }
60     }
61     
62     /* (non-Javadoc)
63      * @see org.eclipse.team.internal.core.subscribers.BatchingLock#createThreadInfo(org.eclipse.team.internal.core.subscribers.BatchingLock.IFlushOperation)
64      */

65     protected ThreadInfo createThreadInfo(IFlushOperation operation) {
66         return new CVSThreadInfo(operation);
67     }
68     
69     public void folderChanged(IContainer folder) {
70         CVSThreadInfo info = (CVSThreadInfo)getThreadInfo();
71         Assert.isNotNull(info, "Folder changed outside of resource lock"); //$NON-NLS-1$
72
info.addChangedFolder(folder);
73     }
74
75 }
76
Popular Tags