File: Locks.java

package info (click to toggle)
openjdk-11 11.0.4%2B11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 757,028 kB
  • sloc: java: 5,016,041; xml: 1,191,974; cpp: 934,731; ansic: 555,697; sh: 24,299; objc: 12,703; python: 3,602; asm: 3,415; makefile: 2,772; awk: 351; sed: 172; perl: 114; jsp: 24; csh: 3
file content (472 lines) | stat: -rw-r--r-- 17,135 bytes parent folder | download | duplicates (6)
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
 * Copyright (c) 2003, 2016, 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.
 */

/*
 * @test
 * @bug     4530538
 * @summary Basic unit test of ThreadInfo.getLockName()
 *          and ThreadInfo.getLockOwnerName()
 * @author  Mandy Chung
 * @author  Jaroslav Bachorik
 *
 * @library /test/lib
 *
 * @run main/othervm Locks
 */
import java.lang.management.*;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Phaser;
import java.util.function.Predicate;
import jdk.test.lib.LockFreeLogger;

public class Locks {

    private static final Object OBJA = new Object();
    private static final Object OBJB = new Object();
    private static final EnhancedWaiter OBJC = new EnhancedWaiter();
    private static final ThreadMXBean TM = ManagementFactory.getThreadMXBean();
    private static final LockFreeLogger LOGGER = new LockFreeLogger();

    private static String getLockName(Object lock) {
        if (lock == null) return null;

        return lock.getClass().getName() + '@' +
                Integer.toHexString(System.identityHashCode(lock));
    }

    private static void assertNoLock(Thread t) {
        if (t == null) {
            return;
        }
        String name = t.getName();
        Optional<ThreadInfo> result = Arrays.stream(
                TM.getThreadInfo(TM.getAllThreadIds(), true, true))
                                            .filter(Objects::nonNull)
                                            .filter(i -> name.equals(i.getLockOwnerName()))
                                            .findAny();
        if (result.isPresent()) {
            throw new RuntimeException("Thread " + t.getName() + " is not "
                    + "supposed to be hold any lock. Currently owning lock : "
                    + result.get().getLockName());
        }
    }

   /*
    * Handy debug function to check if error condition is because of test code or not.
    */
    private static void printStackTrace(Thread thread) {
        if (thread == null) {
            return;
        }
        StackTraceElement[] stackTrace = thread.getStackTrace();
        log("Stack dump : Thread -> " + thread.getName());
        for (StackTraceElement stackTraceEl : stackTrace) {
            log("\t" + stackTraceEl.toString());
        }
    }

    private static void assertThreadState(Thread t, Thread.State expectedState) {
        long tid = t.getId();
        if (expectedState == Thread.State.BLOCKED
                && TM.getThreadInfo(tid).getThreadState() != Thread.State.BLOCKED) {
            int retryCount = 0;
            printStackTrace(t);
            while (TM.getThreadInfo(tid).getThreadState() != Thread.State.BLOCKED) {
                if (retryCount++ > 500) {
                    printStackTrace(t);
                    throw new RuntimeException("Thread " + t.getName() + " is at "
                            + TM.getThreadInfo(tid).getThreadState() + " state but is expected to "
                            + "be in Thread.State = " + expectedState);
                }
                goSleep(100);
            }
        }
        if (!TM.getThreadInfo(tid).getThreadState().equals(expectedState)) {
            printStackTrace(t);
            throw new RuntimeException("Thread " + t.getName() + " is at "
                    + TM.getThreadInfo(tid).getThreadState() + " state but is expected to "
                    + "be in Thread.State = " + expectedState);
        }
    }

   /*
    * Do slow check if thread is blocked on a lock. It is possible that last thread
    * to come out of Phaser might still be in Phaser call stack (Unsafe.park) and
    * hence might eventually acquire expected lock.
    */
    private static void checkBlockedObject(Thread t, Object lock, Thread owner) {
        long tid = t.getId();
        String result = TM.getThreadInfo(tid).getLockName();
        final String expectedLock = (lock != null ? getLockName(lock) : null);
        Predicate<String> p = (res) -> ((res != null && !res.equals(expectedLock))
                || (res == null && expectedLock != null));

        if (p.test(result)) {
            printStackTrace(t);
            int retryCount = 0;
            while (p.test(result)) {
                if (retryCount++ > 500) {
                    printStackTrace(t);
                    throw new RuntimeException("Thread " + t.getName() + " is blocked on "
                            + expectedLock + " but got " + result);
                }
                goSleep(100);
                result = TM.getThreadInfo(tid).getLockName();
            }
        }

        result = TM.getThreadInfo(tid).getLockOwnerName();
        final String expectedOwner = (owner != null ? owner.getName() : null);

        p = (res) -> ((res != null && !res.equals(expectedOwner))
                || (res == null && expectedOwner != null));
        if (p.test(result)) {
            printStackTrace(t);
            throw new RuntimeException("Owner of " + lock + " should be "
                    + expectedOwner + " but got " + result);
        }
    }

    private static void goSleep(long ms){
        try {
            Thread.sleep(ms);
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    }

    private static volatile int dummyCounter = 0;

    static class LockAThread extends Thread {
        private final Phaser p;
        public LockAThread(Phaser p) {
            super("LockAThread");
            this.p = p;
        }
        @Override
        public void run() {
            synchronized(OBJA) {
                // block here while LockBThread holds OBJB
                log("LockAThread about to block on OBJB");
                p.arriveAndAwaitAdvance(); // Phase 1 (blocking)
                synchronized(OBJB) {
                    dummyCounter++;
                }
            }
            p.arriveAndAwaitAdvance(); // Phase 2 (blocking)
            log("LockAThread about to exit");
            // Make sure the current thread is not holding any lock
            assertNoLock(this);
        }
    }

    static class LockBThread extends Thread {
        private final Phaser p;
        public LockBThread(Phaser p) {
            super("LockBThread");
            this.p = p;
        }
        @Override
        public void run() {
            synchronized(OBJB) {
                log("LockBThread about to block on OBJC");
                p.arriveAndAwaitAdvance(); // Phase 1 (blocking)
                // Signal main thread about to block on OBJC
                synchronized(OBJC) {
                    dummyCounter++;
                }
            }
            p.arriveAndAwaitAdvance(); // Phase 2 (blocking)
            log("LockBThread about to exit");
            // Make sure the current thread is not holding any lock
            assertNoLock(this);
        }
    }

   /*
    * Must be invoked from within a synchronized context
    */
    private static class EnhancedWaiter {

        boolean isNotified = false;

        public void doWait() throws InterruptedException {
            while (!isNotified) {
                wait();
            }
            isNotified = false;
        }

        public void doNotify() {
            isNotified = true;
            notify();
        }
    }

    private static WaitingThread waiter;
    private static final Object ready = new Object();
    private static CheckerThread checker;

    static class WaitingThread extends Thread {
        private final Phaser p;

        volatile boolean waiting = false;

        public WaitingThread(Phaser p) {
            super("WaitingThread");
            this.p = p;
        }
        @Override
        public void run() {
            synchronized(OBJC) {
                log("WaitingThread about to wait on OBJC");
                try {
                    // Signal checker thread, about to wait on OBJC.
                    waiting = false;
                    p.arriveAndAwaitAdvance(); // Phase 1 (waiting)
                    waiting = true;
                    OBJC.doWait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e); // Do not continue test
                }

                // block until CheckerThread finishes checking
                log("WaitingThread about to block on ready");
                // signal checker thread that it is about acquire
                // object ready.
                p.arriveAndAwaitAdvance(); // Phase 2 (waiting)
                synchronized(ready) {
                    dummyCounter++;
                }
            }
            synchronized(OBJC) {
                try {
                    // signal checker thread, about to wait on OBJC
                    waiting = false;
                    p.arriveAndAwaitAdvance(); // Phase 3 (waiting)
                    waiting = true;
                    OBJC.doWait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            log("WaitingThread about to exit waiting on OBJC 2");
        }

        public void waitForWaiting() {
            p.arriveAndAwaitAdvance();
            while (!waiting) {
                goSleep(10);
            }
            waitForState(State.WAITING);
        }

        public void waitForBlocked() {
            p.arriveAndAwaitAdvance();
            waitForState(State.BLOCKED);
        }

        private void waitForState(Thread.State state) {
            while (!waiter.isInterrupted() && waiter.getState() != state) {
                Thread.yield();
            }
        }
    }
    static class CheckerThread extends Thread {
        public CheckerThread() {
            super("CheckerThread");
        }

        @Override
        public void run() {
            synchronized(ready) {
                // wait until WaitingThread about to wait for OBJC
                waiter.waitForWaiting(); // Phase 1 (waiting)
                assertThreadState(waiter, Thread.State.WAITING);
                checkBlockedObject(waiter, OBJC, null);

                synchronized(OBJC) {
                    OBJC.doNotify();
                }
                // wait for waiter thread to about to enter
                // synchronized object ready.
                waiter.waitForBlocked(); // Phase 2 (waiting)
                assertThreadState(waiter, Thread.State.BLOCKED);
                checkBlockedObject(waiter, ready, this);
            }

            // wait for signal from waiting thread that it is about
            // wait for OBJC.
            waiter.waitForWaiting(); // Phase 3 (waiting)
            synchronized(OBJC) {
                assertThreadState(waiter, Thread.State.WAITING);
                checkBlockedObject(waiter, OBJC, Thread.currentThread());
                OBJC.doNotify();
            }
        }
    }

    public static void main(String args[]) throws Exception {
        try {
            Thread mainThread = Thread.currentThread();

            // Test uncontested case
            LockAThread t1;
            LockBThread t2;

            Phaser p = new Phaser(3);
            synchronized(OBJC) {
                // Make sure the main thread is not holding any lock
                assertNoLock(mainThread);

                // Test deadlock case
                // t1 holds lockA and attempts to lock B
                // t2 holds lockB and attempts to lock C
                t1 = new LockAThread(p);
                t1.start();

                t2 = new LockBThread(p);
                t2.start();

                p.arriveAndAwaitAdvance(); // Phase 1 (blocking)
                assertThreadState(t2, Thread.State.BLOCKED);
                checkBlockedObject(t2, OBJC, mainThread);
                assertThreadState(t1, Thread.State.BLOCKED);
                checkBlockedObject(t1, OBJB, t2);

                long[] expectedThreads = new long[3];
                expectedThreads[0] = t1.getId(); // blocked on lockB
                expectedThreads[1] = t2.getId(); // owner of lockB blocking on lockC
                expectedThreads[2] = mainThread.getId(); // owner of lockC
                findThreadsBlockedOn(OBJB, expectedThreads);
            }
            p.arriveAndAwaitAdvance(); // Phase 2 (blocking)

            p = new Phaser(2);
            // Test Object.wait() case
            waiter = new WaitingThread(p);
            waiter.start();

            checker = new CheckerThread();
            checker.start();

            try {
                waiter.join();
                checker.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        } finally { // log all the messages to STDOUT
            System.out.println(LOGGER.toString());
        }
        System.out.println("Test passed.");
    }

    private static ThreadInfo findOwnerInfo(ThreadInfo[] infos, String lock)
            throws Exception {
        ThreadInfo ownerInfo = null;
        for (ThreadInfo info : infos) {
            String blockedLock = info.getLockName();
            if (lock.equals(blockedLock)) {
                long threadId = info.getLockOwnerId();
                if (threadId == -1) {
                    throw new RuntimeException("TEST FAILED: " +
                            lock + " expected to have owner");
                }
                for (ThreadInfo info1 : infos) {
                    if (info1.getThreadId() == threadId) {
                        ownerInfo = info1;
                        break;
                    }
                }
            }
        }
        return ownerInfo;
    }
    private static void findThreadsBlockedOn(Object o, long[] expectedThreads)
            throws Exception {
        String lock = getLockName(o);
        // Check with ThreadInfo with no stack trace (i.e. no safepoint)
        ThreadInfo[] infos = TM.getThreadInfo(TM.getAllThreadIds());
        doCheck(infos, lock, expectedThreads);

        // Check with ThreadInfo with stack trace
        infos = TM.getThreadInfo(TM.getAllThreadIds(), 1);
        doCheck(infos, lock, expectedThreads);
    }

    private static void doCheck(ThreadInfo[] infos, String lock, long[] expectedThreads)
            throws Exception {
        ThreadInfo ownerInfo = null;
        // Find the thread who is blocking on lock
        for (ThreadInfo info : infos) {
            String blockedLock = info.getLockName();
            if (lock.equals(blockedLock)) {
                log("%s blocked on %s", info.getThreadName(), blockedLock);
                ownerInfo = info;
            }
        }
        if (ownerInfo == null) {
            throw new RuntimeException("TEST FAILED: " +
                    "Can't retrieve ThreadInfo for the blocked thread");
        }

        long[] threads = new long[10];
        int count = 0;
        threads[count++] = ownerInfo.getThreadId();
        while (ownerInfo.getThreadState() == Thread.State.BLOCKED) {
            ownerInfo = findOwnerInfo(infos, lock);
            threads[count++] = ownerInfo.getThreadId();
            log(" Owner = %s  id = %d",
                    ownerInfo.getThreadName(),
                    ownerInfo.getThreadId()
            );
            lock = ownerInfo.getLockName();
            log("%s Id = %d  blocked on %s",
                    ownerInfo.getThreadName(),
                    ownerInfo.getThreadId(),
                    lock
            );
        }
        log("");

        if (count != expectedThreads.length) {
            throw new RuntimeException("TEST FAILED: " +
                    "Expected chain of threads not matched; current count =" + count);
        }
        for (int i = 0; i < count; i++) {
            if (threads[i] != expectedThreads[i]) {
                log("TEST FAILED: Unexpected thread in the chain %s expected to be %s",
                    threads[i],
                    expectedThreads[i]
                );
            }
        }
    }

    private static void log(String format, Object ... args) {
        LOGGER.log(format + "%n", args);
    }
}