File: SyncMap.java

package info (click to toggle)
concurrent-dfsg 1.3.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 976 kB
  • ctags: 2,018
  • sloc: java: 10,704; xml: 49; makefile: 12
file content (307 lines) | stat: -rw-r--r-- 6,405 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
  File: SyncMap.java

  Originally written by Doug Lea and released into the public domain.
  This may be used for any purposes whatsoever without acknowledgment.
  Thanks for the assistance and support of Sun Microsystems Labs,
  and everyone contributing, testing, and using this code.

  History:
  Date       Who                What
   1Aug1998  dl               Create public version
*/

package EDU.oswego.cs.dl.util.concurrent;
import java.util.*;

/**
 * SyncMaps wrap Sync-based control around java.util.Maps.
 * They operate in the same way as SyncCollection.
 * <p>
 * Reader operations are
 * <ul>
 *  <li> size
 *  <li> isEmpty
 *  <li> get
 *  <li> containsKey
 *  <li> containsValue
 *  <li> keySet
 *  <li> entrySet
 *  <li> values
 * </ul>
 * Writer operations are:
 * <ul>
 *  <li> put
 *  <li> putAll
 *  <li> remove
 *  <li> clear
 * </ul>
 *  
 * <p>[<a href="http://gee.cs.oswego.edu/dl/classes/EDU/oswego/cs/dl/util/concurrent/intro.html"> Introduction to this package. </a>]
 * @see SyncCollection
**/


public class SyncMap implements Map {
  protected final Map c_;	   // Backing Map
  protected final Sync rd_;  //  sync for read-only methods
  protected final Sync wr_;  //  sync for mutative methods

  protected final SynchronizedLong syncFailures_ = new SynchronizedLong(0);

  /**
   * Create a new SyncMap protecting the given map,
   * and using the given sync to control both reader and writer methods.
   * Common, reasonable choices for the sync argument include
   * Mutex, ReentrantLock, and Semaphores initialized to 1.
   **/
  public SyncMap(Map map, Sync sync) {
    this (map, sync, sync);
  }


  /**
   * Create a new SyncMap protecting the given map,
   * and using the given ReadWriteLock to control reader and writer methods.
   **/
  public SyncMap(Map map, ReadWriteLock rwl) {
    this (map, rwl.readLock(), rwl.writeLock());
  }

  /**
   * Create a new SyncMap protecting the given map,
   * and using the given pair of locks to control reader and writer methods.
   **/
  public SyncMap(Map map, Sync readLock, Sync writeLock) {
    c_ = map; 
    rd_ = readLock;
    wr_ = writeLock;
  }

  /** 
   * Return the Sync object managing read-only operations
   **/
      
  public Sync readerSync() {
    return rd_;
  }

  /** 
   * Return the Sync object managing mutative operations
   **/

  public Sync writerSync() {
    return wr_;
  }

  /**
   * Return the number of synchronization failures for read-only operations
   **/
  public long syncFailures() {
    return syncFailures_.get();
  }


  /** Try to acquire sync before a reader operation; record failure **/
  protected boolean beforeRead() {
    try {
      rd_.acquire();
      return false;
    }
    catch (InterruptedException ex) { 
      syncFailures_.increment();
      return true; 
    }
  }

  /** Clean up after a reader operation **/
  protected void afterRead(boolean wasInterrupted) {
    if (wasInterrupted) {
      Thread.currentThread().interrupt();
    }
    else
      rd_.release();
  }



  public int hashCode() {
    boolean wasInterrupted = beforeRead();
    try {
      return c_.hashCode();
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

  public boolean equals(Object o) {
    boolean wasInterrupted = beforeRead();
    try {
      return c_.equals(o);
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

  public int size() {
    boolean wasInterrupted = beforeRead();
    try {
      return c_.size();
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

  public boolean isEmpty() {
    boolean wasInterrupted = beforeRead();
    try {
      return c_.isEmpty();
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

  public boolean containsKey(Object o) {
    boolean wasInterrupted = beforeRead();
    try {
      return c_.containsKey(o);
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

  public boolean containsValue(Object o) {
    boolean wasInterrupted = beforeRead();
    try {
      return c_.containsValue(o);
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

  public Object get(Object key) {
    boolean wasInterrupted = beforeRead();
    try {
      return c_.get(key);
    }
    finally {
      afterRead(wasInterrupted);
    }
  }


  public Object put(Object key, Object value) {
    try {
      wr_.acquire();
      try {
        return c_.put(key, value);
      }
      finally {
        wr_.release();
      }
    }
    catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
      throw new UnsupportedOperationException();
    }
  }

  public Object remove(Object key) {
    try {
      wr_.acquire();
      try {
        return c_.remove(key);
      }
      finally {
        wr_.release();
      }
    }
    catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
      throw new UnsupportedOperationException();
    }
  }

  public void putAll(Map coll) {
    try {
      wr_.acquire();
      try {
        c_.putAll(coll);
      }
      finally {
        wr_.release();
      }
    }
    catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
      throw new UnsupportedOperationException();
    }
  }

	
  public void clear() {
    try {
      wr_.acquire();
      try {
        c_.clear();
      }
      finally {
        wr_.release();
      }
    }
    catch (InterruptedException ex) {
      Thread.currentThread().interrupt();
      throw new UnsupportedOperationException();
    }
  }

  private transient Set keySet_ = null;
  private transient Set entrySet_ = null;
  private transient Collection values_ = null;
  
  public Set keySet() {
    boolean wasInterrupted = beforeRead();
    try {
      if (keySet_ == null)
        keySet_ = new SyncSet(c_.keySet(), rd_, wr_);
      return keySet_;
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

  public Set entrySet() {
    boolean wasInterrupted = beforeRead();
    try {
      if (entrySet_ == null)
        entrySet_ = new SyncSet(c_.entrySet(), rd_, wr_);
      return entrySet_;
    }
    finally {
      afterRead(wasInterrupted);
    }
  }


  public Collection values() {
    boolean wasInterrupted = beforeRead();
    try {
      if (values_ == null)
        values_ = new SyncCollection(c_.values(), rd_, wr_);
      return values_;
    }
    finally {
      afterRead(wasInterrupted);
    }
  }

}