?? ?
2010-04-22 6523c1f6534042d89ff8a07d4e7c06050276521e
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
package com.ximple.io.dgn7;
 
//~--- JDK imports ------------------------------------------------------------
 
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
 
/**
 * Lock
 *
 * @author Ulysses
 * @version 0.1
 * @since 2006/5/18 上午 10:27:24
 */
public class Lock {
    Logger logger = LogManager.getLogger("com.ximple.io.dgn7");
 
    /**
     * indicates a write is occurring
     */
    int writeLocks = 0;
 
    /**
     * if not null a writer is waiting for the lock or is writing.
     */
    Thread writer;
 
    /**
     * Thread->Owner map. If empty no read locks exist.
     */
    Map owners = new HashMap();
 
    /**
     * If the lock can be read locked the lock will be read and default
     * visibility for tests
     *
     * @return
     * @throws java.io.IOException
     */
    synchronized boolean canRead() throws IOException {
        if ((writer != null) && (writer != Thread.currentThread())) {
            return false;
        }
 
        if (writer == null) {
            return true;
        }
 
        if (owners.size() > 1) {
            return false;
        }
 
        return true;
    }
 
    /**
     * If the lock can be read locked the lock will be read and default
     * visibility for tests
     *
     * @return
     * @throws IOException
     */
    synchronized boolean canWrite() throws IOException {
        if (owners.size() > 1) {
            return false;
        }
 
        if ((canRead()) && ((writer == Thread.currentThread()) || (writer == null))) {
            if (owners.isEmpty()) {
                return true;
            }
 
            if (owners.containsKey(Thread.currentThread())) {
                return true;
            }
        }
 
        return false;
    }
 
    /**
     * Called by shapefileReader before a read is started and before an IOStream
     * is openned.
     *
     * @throws IOException
     */
    public synchronized void lockRead() throws IOException {
        if (!canRead()) {
            while ((writeLocks > 0) || (writer != null)) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw (IOException) new IOException().initCause(e);
                }
            }
        }
 
        assertTrue("A write lock exists that is owned by another thread", canRead());
 
        Thread current = Thread.currentThread();
        Owner owner = (Owner) owners.get(current);
 
        if (owner != null) {
            owner.timesLocked++;
        } else {
            owner = new Owner(current);
            owners.put(current, owner);
        }
 
        logger.debug("Start Read Lock:" + owner);
    }
 
    private void assertTrue(String message, boolean b) {
        if (!b) {
            throw new AssertionError(message);
        }
    }
 
    /**
     * Called by ShapefileReader after a read is complete and after the IOStream
     * is closed.
     */
    public synchronized void unlockRead() {
        assertTrue("Current thread does not have a readLock", owners.containsKey(Thread.currentThread()));
 
        Owner owner = (Owner) owners.get(Thread.currentThread());
 
        assertTrue("Current thread has " + owner.timesLocked + "negative number of locks", owner.timesLocked > 0);
        owner.timesLocked--;
 
        if (owner.timesLocked == 0) {
            owners.remove(Thread.currentThread());
        }
 
        notifyAll();
        logger.debug("unlock Read:" + owner);
    }
 
    /**
     * Called by ShapefileDataStore before a write is started and before an
     * IOStream is openned.
     *
     * @throws IOException
     */
    public synchronized void lockWrite() throws IOException {
        Thread currentThread = Thread.currentThread();
 
        if (writer == null) {
            writer = currentThread;
        }
 
        while (!canWrite()) {
            try {
                wait();
            } catch (InterruptedException e) {
                throw (IOException) new IOException().initCause(e);
            }
 
            if (writer == null) {
                writer = currentThread;
            }
        }
 
        if (writer == null) {
            writer = currentThread;
        }
 
        assertTrue("The current thread is not the writer", writer == currentThread);
        assertTrue("There are read locks not belonging to the current thread.", canRead());
        writeLocks++;
        logger.debug(currentThread.getName() + " is getting write lock:" + writeLocks);
    }
 
    /**
     * default visibility for tests
     */
    synchronized int getReadLocks(Thread thread) {
        Owner owner = (Owner) owners.get(thread);
 
        if (owner == null) {
            return -1;
        }
 
        return owner.timesLocked;
    }
 
    public synchronized void unlockWrite() {
        if (writeLocks > 0) {
            assertTrue("current thread does not own the write lock", writer == Thread.currentThread());
            assertTrue("writeLock has already been unlocked", writeLocks > 0);
            writeLocks--;
 
            if (writeLocks == 0) {
                writer = null;
            }
        }
 
        logger.debug("unlock write:" + Thread.currentThread().getName());
        notifyAll();
    }
 
    /**
     * default visibility for tests
     */
    synchronized boolean ownWriteLock(Thread thread) {
        return (writer == thread) && (writeLocks > 0);
    }
 
    private class Owner {
        final Thread owner;
        int timesLocked;
 
        Owner(Thread owner) {
            this.owner = owner;
            timesLocked = 1;
        }
 
        public String toString() {
            return owner.getName() + " has " + timesLocked + " locks";
        }
    }
}