KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > registry > ReadWriteMonitor


1 /*******************************************************************************
2  * Copyright (c) 2004, 2005 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.core.internal.registry;
12
13 /**
14  * Monitor ensuring no more than one writer working concurrently.
15  * Multiple readers are allowed to perform simultaneously.
16  *
17  * This class was borrowed from org.eclipse.jdt.internal.core.search.indexing.
18  */

19 public class ReadWriteMonitor {
20
21     /**
22      * <0 : writing (cannot go beyond -1, i.e one concurrent writer)
23      * =0 : idle
24      * >0 : reading (number of concurrent readers)
25      */

26     private int status = 0;
27
28     private Thread JavaDoc writeLockowner;
29
30     /**
31      * Concurrent reading is allowed
32      * Blocking only when already writing.
33      */

34     public synchronized void enterRead() {
35         if (writeLockowner == Thread.currentThread())
36             return;
37         while (status < 0) {
38             try {
39                 wait();
40             } catch (InterruptedException JavaDoc e) {
41                 // ignore
42
}
43         }
44         status++;
45     }
46
47     /**
48      * Only one writer at a time is allowed to perform
49      * Blocking only when already writing or reading.
50      */

51     public synchronized void enterWrite() {
52         if (writeLockowner != Thread.currentThread()) {
53             while (status != 0) {
54                 try {
55                     wait();
56                 } catch (InterruptedException JavaDoc e) {
57                     // ignore
58
}
59             }
60 // System.out.println(this + "lockowner:" + Thread.currentThread());
61
writeLockowner = Thread.currentThread();
62         }
63         status--;
64     }
65
66     /**
67      * Only notify waiting writer(s) if last reader
68      */

69     public synchronized void exitRead() {
70         if (writeLockowner == Thread.currentThread())
71             return;
72         if (--status == 0)
73             notifyAll();
74     }
75
76     /**
77      * When writing is over, all readers and possible
78      * writers are granted permission to restart concurrently
79      */

80     public synchronized void exitWrite() {
81         if (writeLockowner != Thread.currentThread())
82             throw new IllegalStateException JavaDoc("Current owner is " + writeLockowner); //$NON-NLS-1$
83
if (++status == 0) {
84             // System.out.println(this + "exitWrite:" + Thread.currentThread());
85
writeLockowner = null;
86             notifyAll();
87         }
88     }
89
90     public String JavaDoc toString() {
91         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
92         buffer.append(this.hashCode());
93         if (status == 0) {
94             buffer.append("Monitor idle "); //$NON-NLS-1$
95
} else if (status < 0) {
96             buffer.append("Monitor writing "); //$NON-NLS-1$
97
} else if (status > 0) {
98             buffer.append("Monitor reading "); //$NON-NLS-1$
99
}
100         buffer.append("(status = "); //$NON-NLS-1$
101
buffer.append(this.status);
102         buffer.append(")"); //$NON-NLS-1$
103
return buffer.toString();
104     }
105 }
106
Popular Tags