File: SynchronizerLockingThreads.java

package info (click to toggle)
openjdk-11 11.0.22%2B7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,236 kB
  • sloc: java: 5,174,754; xml: 1,192,262; cpp: 1,133,036; ansic: 461,316; javascript: 162,554; sh: 16,859; objc: 13,683; python: 4,753; asm: 3,570; makefile: 2,894; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (282 lines) | stat: -rw-r--r-- 11,891 bytes parent folder | download | duplicates (10)
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
/*
 * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package nsk.monitoring.share.thread;

import java.lang.management.*;
import nsk.share.log.*;
import java.util.Map;
import java.util.HashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;

/**
 * Scenario that starts two threads that use locks * to synchronize.
 * The code is based on tests/java/lang/management/ThreadMXBean/LockingThread.java
 */
public class SynchronizerLockingThreads implements ThreadMonitoringScenario, LogAware {
        private static final String[] expectedMethodsThread1 = {
                "nsk.monitoring.share.thread.SynchronizerLockingThreads$Thread1.runInside",
                "nsk.monitoring.share.thread.SynchronizerLockingThreads$Thread1.A",
                "nsk.monitoring.share.thread.SynchronizerLockingThreads$Thread1.B",
                "nsk.monitoring.share.thread.SynchronizerLockingThreads$Thread1.C",
                "nsk.monitoring.share.thread.SynchronizerLockingThreads$Thread1.D",
                "java.lang.Object.wait"
        };
        private static final String[] expectedMethodsThread2 = {
                "nsk.monitoring.share.thread.SynchronizerLockingThreads$Thread2.runInside"
        };
        private ReentrantLock lock1 = new ReentrantLock();
        private ReentrantLock lock2 = new ReentrantLock();
        private ReentrantLock lock3 = new ReentrantLock();
        private ReentrantLock lock4 = new ReentrantLock();
        private CustomLock lock5 = new CustomLock("lock5");
        private CustomLock lock6 = new CustomLock("lock6");
        private CustomLock lock7 = new CustomLock("lock7");
        private ReentrantLock lock8 = new ReentrantLock();
        private MonitoringThread thread1;
        private MonitoringThread thread2;
        private Log log;
        private RunType recursionType;
        private int maxDepth;

        public SynchronizerLockingThreads(Log log, RunType recursionType, int maxDepth) {
                setLog(log);
                this.recursionType = recursionType;
                this.maxDepth = maxDepth;
                thread1 = new Thread1(log, recursionType, maxDepth);
                thread2 = new Thread2(log, recursionType, maxDepth);
        }

        static class CustomLock {
                private String name;

                public CustomLock(String name) {
                        this.name = name;
                }

                public String toString() {
                        return name;
                }
        }

        private class Thread1 extends RecursiveMonitoringThread {
                private volatile boolean ready = false;
                private Object readyLock = new Object();
                private Map<String, Object[]> lockedMonitors = new HashMap<String, Object[]>();
                private Map<String, Lock[]> lockedSynchronizers = new HashMap<String, Lock[]>();

                public Thread1(Log log, RunType recursionType, int maxDepth) {
                        super(log, recursionType, maxDepth);
                        lockedMonitors.put("D", new Object[] {});
                        lockedMonitors.put("C", new Object[] { lock6 });
                        lockedMonitors.put("B", new Object[] { lock5 });
                        lockedMonitors.put("A", new Object[] { });
                        lockedSynchronizers.put("D", new ReentrantLock[0]); // no sync locked
                        lockedSynchronizers.put("C", new ReentrantLock[0]); // no sync locked
                        lockedSynchronizers.put("B", new Lock[] {lock4});
                        lockedSynchronizers.put("A", new Lock[] {lock3, lock2, lock1});
                }

                public void checkThreadInfo(ThreadInfo info) {
                        super.checkThreadInfo(info);
                        checkLockInfo(info.getLockInfo(), lock7);
                        checkMonitorInfo(info.getLockedMonitors(), lockedMonitors);
                        checkSynchronizers(info.getLockedSynchronizers(), lockedSynchronizers);
                }

                protected void runInside() {
                        A();
                }

                void A() {
                        lock1.lock();
                        try {
                                lock2.lock();
                                try {
                                        lock3.lock();
                                        try {
                                                B();
                                        } finally {
                                                lock3.unlock();
                                        }
                                } finally {
                                        lock2.unlock();
                                }
                        } finally {
                                lock1.unlock();
                        }
                }

                void B() {
                        lock4.lock();
                        try {
                                synchronized(lock5) {
                                        C();
                                }
                        } finally {
                                lock4.unlock();
                        }
                }

                void C() {
                        synchronized(lock6) {
                                D();
                        }
                }

                void D() {
                        synchronized(lock7) {
                                try {
                                        synchronized (readyLock) {
                                                ready = true;
                                                readyLock.notifyAll();
                                        }
                                        lock7.wait();
                                } catch (InterruptedException e) {
                                        throw new RuntimeException(e);
                                }
                        }
                }


                public void waitState() {
                        synchronized (readyLock) {
                                while (!ready) {
                                        try {
                                                readyLock.wait();
                                        } catch (InterruptedException e) {
                                                log.warn(e);
                                        }
                                }
                        }
                        waitThreadState(Thread.State.WAITING);
                }

                public void finish() {
                        synchronized (lock7) {
                                lock7.notifyAll();
                        }
                }

                protected boolean isStackTraceElementExpected(StackTraceElement element) {
                        return super.isStackTraceElementExpected(element) || checkStackTraceElement(element, expectedMethodsThread1);
                }
        }

        private class Thread2 extends RecursiveMonitoringThread {
                private boolean ready = false;
                private Object readyLock = new Object();
                private Map<String, Object[]> lockedMonitors = new HashMap<String, Object[]>();
                private Map<String, Lock[]> lockedSynchronizers = new HashMap<String, Lock[]>();
                private Condition c = lock8.newCondition();

                public Thread2(Log log, RunType recursionType, int maxDepth) {
                        super(log, recursionType, maxDepth);
                }

                public void checkThreadInfo(ThreadInfo info) {
                        super.checkThreadInfo(info);
                        checkLockInfo(info.getLockInfo(), c);
                        checkMonitorInfo(info.getLockedMonitors(), lockedMonitors);
                        checkSynchronizers(info.getLockedSynchronizers(), lockedSynchronizers);
                }

                protected void runInside() {
                        lock8.lock();
                        try {
                                synchronized (readyLock) {
                                        ready = true;
                                        readyLock.notifyAll();
                                }
                                c.await();
                        } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                        } finally {
                                lock8.unlock();
                        }
                }

                public void waitState() {
                        synchronized (readyLock) {
                                while (!ready) {
                                        try {
                                                readyLock.wait();
                                        } catch (InterruptedException e) {
                                                log.warn(e);
                                        }
                                }
                        }
                        waitThreadState(Thread.State.WAITING);
                }

                public void finish() {
                        lock8.lock();
                        try {
                                c.signalAll();
                        } finally {
                                lock8.unlock();
                        }
                }

                protected boolean isStackTraceElementExpected(StackTraceElement element) {
                        return super.isStackTraceElementExpected(element) ||
                                checkStackTraceElement(element, expectedMethodsThread2) ||
                                element.getClassName().startsWith("java.util.concurrent.locks.") ||
                                element.getClassName().startsWith("jdk.internal.misc.");
                }
        }


        public void begin() {
                thread1.begin();
                thread2.begin();
        }

        public void waitState() {
                thread1.waitState();
                thread2.waitState();
        }

        public void check(ThreadMXBean threadMXBean) {
                long[] ids = new long[] { thread1.getId(), thread2.getId() };
                ThreadInfo[] info = threadMXBean.getThreadInfo(ids, true, true);
                thread1.checkThreadInfo(info[0]);
                thread2.checkThreadInfo(info[1]);
        }

        public void finish() {
                thread1.finish();
                thread2.finish();
        }

        public void end() {
                thread1.end();
                thread2.end();
        }

        public void setLog(Log log) {
                this.log = log;
        }
}