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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.fo.properties;
import java.lang.ref.WeakReference;
/**
* Dedicated cache, meant for storing canonical instances
* of property-related classes.
* The public access points are overloaded <code>fetch()</code> methods
* that each correspond to a cached type.
* It is designed especially to be used concurrently by multiple threads,
* drawing heavily upon the principles behind Java 1.5's
* <code>ConcurrentHashMap</code>.
*/
public final class PropertyCache {
/** bitmask to apply to the hash to get to the
* corresponding cache segment */
private static final int SEGMENT_MASK = 0x1F;
/** the segments array (length = 32) */
private CacheSegment[] segments = new CacheSegment[SEGMENT_MASK + 1];
/** the table of hash-buckets */
private CacheEntry[] table = new CacheEntry[8];
/* same hash function as used by java.util.HashMap */
private static int hash(Object x) {
int h = x.hashCode();
h += ~(h << 9);
h ^= (h >>> 14);
h += (h << 4);
h ^= (h >>> 10);
return h;
}
/* shortcut function */
private static boolean eq(Object p, Object q) {
return (p == q || (p != null && p.equals(q)));
}
/* Class modeling a cached entry */
private final class CacheEntry {
final CacheEntry next;
volatile WeakReference ref;
final int hash;
/* main constructor */
CacheEntry(Object p, CacheEntry next) {
this.next = next;
this.ref = new WeakReference(p);
this.hash = p.hashCode();
}
/* clone constructor */
CacheEntry(CacheEntry old, CacheEntry next) {
this.next = next;
this.ref = old.ref;
this.hash = old.hash;
}
public boolean isCleared() {
return (ref == null || ref.get() == null);
}
}
/* Wrapper objects to synchronize on */
private final class CacheSegment {
private int count = 0;
}
/*
* Class modeling a cleanup thread.
*
* Once run() is called, the segment is locked and the hash-bucket
* will be traversed, removing any obsolete entries.
* If the cleanup has no effect, rehash() is called.
*/
private final class CacheCleaner implements Runnable {
private int hash;
CacheCleaner(int hash) {
this.hash = hash;
}
public void run() {
//System.out.println("Cleaning segment " + this.segment);
CacheSegment segment = segments[this.hash & SEGMENT_MASK];
int oldCount;
int newCount;
synchronized (segment) {
oldCount = segment.count;
/* check first to see if another cleaner thread already
* pushed the number of entries back below the threshold
* if so, return immediately
*/
if (segment.count < (2 * table.length)) {
return;
}
int index = this.hash & (table.length - 1);
CacheEntry first = table[index];
for (CacheEntry e = first; e != null; e = e.next) {
if (e.isCleared()) {
/* remove obsolete entry
/* 1. clear value, cause interference for non-blocking get() */
e.ref = null;
/* 2. clone the segment, without the obsolete entry */
CacheEntry head = e.next;
for (CacheEntry c = first; c != e; c = c.next) {
if (!c.isCleared()) {
head = new CacheEntry(c, head);
}
}
table[index] = head;
segment.count--;
}
}
newCount = segment.count;
}
if (oldCount == newCount) {
/* cleanup had no effect, try rehashing */
rehash(SEGMENT_MASK);
}
}
}
/*
* Puts a new instance in the cache.
* If the total number of entries for the corresponding
* segment exceeds twice the amount of hash-buckets, a
* cleanup thread will be launched to remove obsolete
* entries.
*/
private final void put(Object o) {
int hash = hash(o);
CacheSegment segment = segments[hash & SEGMENT_MASK];
synchronized (segment) {
int index = hash & (table.length - 1);
CacheEntry entry = table[index];
if (entry == null) {
entry = new CacheEntry(o, null);
table[index] = entry;
segment.count++;
} else {
WeakReference ref = entry.ref;
if (ref != null && eq(ref.get(), o)) {
return;
} else {
CacheEntry newEntry = new CacheEntry(o, entry);
table[index] = newEntry;
segment.count++;
}
}
if (segment.count > (2 * table.length)) {
/* launch cleanup in a separate thread,
* so it acquires its own lock, and put()
* can return immediately */
Thread cleaner = new Thread(new CacheCleaner(hash), "FOP PropertyCache Cleaner");
cleaner.start();
}
}
}
/* Gets a cached instance. Returns null if not found */
private final Object get(Object o) {
int hash = hash(o);
int index = hash & (table.length - 1);
CacheEntry entry = table[index];
WeakReference r;
Object q;
/* try non-synched first */
for (CacheEntry e = entry; e != null; e = e.next) {
if (e.hash == o.hashCode()
&& (r = e.ref) != null
&& (q = r.get()) != null
&& eq(q, o)) {
return q;
}
}
/* retry synched, only if the above attempt did not succeed,
* as another thread may, in the meantime, have added a
* corresponding entry */
CacheSegment segment = segments[hash & SEGMENT_MASK];
synchronized (segment) {
entry = table[index];
for (CacheEntry e = entry; e != null; e = e.next) {
if (e.hash == o.hashCode()
&& (r = e.ref) != null
&& (q = r.get()) != null
&& eq(q, o)) {
return q;
}
}
}
return null;
}
/*
* Recursively acquires locks on all 32 segments,
* then performs a check on the segments first to see `
* how many precisely exceed the threshold ( 2 x table.length ).
* If this number exceeds half the amount of buckets,
* extends the cache and redistributes the entries.
*
* Example:
* For a cache with default size of 8 buckets, each bucket is
* a segment, and as such, rehash() will only have effect
* if more than 4 buckets exceed the size of 16 entries.
*
*/
private final void rehash(int index) {
CacheSegment seg = segments[index];
synchronized (seg) {
if (index > 0) {
/* need to recursively acquire locks on all segments */
rehash(index - 1);
} else {
/* double the amount of buckets */
int newLength = table.length << 1;
if (newLength > 0) { //no overflow?
/* Check segmentcounts first */
int countSegments = 0;
int threshold = table.length * 2;
for (int i = segments.length; --i >= 0;) {
if (segments[i].count > threshold) {
countSegments++;
}
}
if (countSegments <= (table.length / 2)) {
return;
} else {
/* reset segmentcounts */
for (int i = segments.length; --i >= 0;) {
segments[i].count = 0;
}
}
CacheEntry[] newTable = new CacheEntry[newLength];
int hash, idx;
WeakReference ref;
Object o;
newLength--;
for (int i = table.length; --i >= 0;) {
for (CacheEntry c = table[i]; c != null; c = c.next) {
ref = c.ref;
if (ref != null) {
if ((o = ref.get()) != null) {
hash = hash(o);
idx = hash & newLength;
newTable[idx] = new CacheEntry(c, newTable[idx]);
segments[hash & SEGMENT_MASK].count++;
}
}
}
}
table = newTable;
}
}
}
}
/**
* Default constructor.
*/
public PropertyCache() {
for (int i = SEGMENT_MASK + 1; --i >= 0;) {
segments[i] = new CacheSegment();
}
}
/**
* Generic fetch() method.
* Checks if the given <code>Object</code> is present in the cache -
* if so, returns a reference to the cached instance.
* Otherwise the given object is added to the cache and returned.
*
* @param obj the Object to check for
* @return the cached instance
*/
private final Object fetch(Object obj) {
if (obj == null) {
return null;
}
Object cacheEntry = get(obj);
if (cacheEntry != null) {
return cacheEntry;
}
put(obj);
return obj;
}
/**
* Checks if the given <code>Property</code> is present in the cache -
* if so, returns a reference to the cached instance.
* Otherwise the given object is added to the cache and returned.
*
* @param prop the Property instance to check for
* @return the cached instance
*/
public final Property fetch(Property prop) {
return (Property) fetch((Object) prop);
}
/**
* Checks if the given <code>CommonHyphenation</code> is present in the cache -
* if so, returns a reference to the cached instance.
* Otherwise the given object is added to the cache and returned.
*
* @param chy the CommonHyphenation instance to check for
* @return the cached instance
*/
public final CommonHyphenation fetch(CommonHyphenation chy) {
return (CommonHyphenation) fetch((Object) chy);
}
/**
* Checks if the given <code>CachedCommonFont</code> is present in the cache -
* if so, returns a reference to the cached instance.
* Otherwise the given object is added to the cache and returned.
*
* @param ccf the CachedCommonFont instance to check for
* @return the cached instance
*/
public final CommonFont.CachedCommonFont fetch(CommonFont.CachedCommonFont ccf) {
return (CommonFont.CachedCommonFont) fetch((Object) ccf);
}
/**
* Checks if the given <code>CommonFont</code> is present in the cache -
* if so, returns a reference to the cached instance.
* Otherwise the given object is added to the cache and returned.
*
* @param cf the CommonFont instance to check for
* @return the cached instance
*/
public final CommonFont fetch(CommonFont cf) {
return (CommonFont) fetch((Object) cf);
}
}
|