KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > indexing > ReadWriteMonitor


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.jdt.internal.core.search.indexing;
12
13 /**
14  * Monitor ensuring no more than one writer working concurrently.
15  * Multiple readers are allowed to perform simultaneously.
16  */

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

24 private int status = 0;
25 /**
26  * Concurrent reading is allowed
27  * Blocking only when already writing.
28  */

29 public synchronized void enterRead() {
30     while (status < 0) {
31         try {
32             wait();
33         } catch(InterruptedException JavaDoc e) {
34             // ignore
35
}
36     }
37     status++;
38 }
39 /**
40  * Only one writer at a time is allowed to perform
41  * Blocking only when already writing or reading.
42  */

43 public synchronized void enterWrite() {
44     while (status != 0) {
45         try {
46             wait();
47         } catch(InterruptedException JavaDoc e) {
48             // ignore
49
}
50     }
51     status--;
52 }
53 /**
54  * Only notify waiting writer(s) if last reader
55  */

56 public synchronized void exitRead() {
57
58     if (--status == 0) notifyAll();
59 }
60 /**
61  * When writing is over, all readers and possible
62  * writers are granted permission to restart concurrently
63  */

64 public synchronized void exitWrite() {
65
66     if (++status == 0) notifyAll();
67 }
68 /**
69  * Atomic exitRead/enterWrite: Allows to keep monitor in between
70  * exit read and next enter write.
71  * Use when writing changes is optional, otherwise call the individual methods.
72  * Returns false if multiple readers are accessing the index.
73  */

74 public synchronized boolean exitReadEnterWrite() {
75     if (status != 1) return false; // only continue if this is the only reader
76

77     status = -1;
78     return true;
79 }
80 /**
81  * Atomic exitWrite/enterRead: Allows to keep monitor in between
82  * exit write and next enter read.
83  * When writing is over, all readers are granted permissing to restart
84  * concurrently.
85  * This is the same as:
86  * <pre>
87  * synchronized(monitor) {
88  * monitor.exitWrite();
89  * monitor.enterRead();
90  * }
91  * </pre>
92  */

93 public synchronized void exitWriteEnterRead() {
94     this.exitWrite();
95     this.enterRead();
96 }
97 public String JavaDoc toString() {
98     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
99     if (status == 0) {
100         buffer.append("Monitor idle "); //$NON-NLS-1$
101
} else if (status < 0) {
102         buffer.append("Monitor writing "); //$NON-NLS-1$
103
} else if (status > 0) {
104         buffer.append("Monitor reading "); //$NON-NLS-1$
105
}
106     buffer.append("(status = "); //$NON-NLS-1$
107
buffer.append(this.status);
108     buffer.append(")"); //$NON-NLS-1$
109
return buffer.toString();
110 }
111 }
112
Popular Tags