author | jlahoda |
Tue, 12 Nov 2019 06:32:13 +0000 | |
changeset 59021 | cfc7bb9a5a92 |
parent 49934 | 44839fbb20db |
permissions | -rw-r--r-- |
49934
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
1 |
/* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
2 |
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
4 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
8 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
13 |
* accompanied this code). |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
14 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
18 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
21 |
* questions. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
22 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
23 |
package nsk.share.locks; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
24 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
25 |
/* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
26 |
* Thread intended to hold given lock until method releaseLock() not called |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
27 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
28 |
* Example of usage: |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
29 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
30 |
* Object lockToHold = new Object(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
31 |
* MonitorLockingThread lockingThread = new MonitorLockingThread(lockToHold); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
32 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
33 |
* // after calling this method lock 'lockToHold' is acquired by lockingThread |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
34 |
* lockingThread.acquireLock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
35 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
36 |
* // after calling this method lockingThread releases 'lockToHold' and finishes execution |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
37 |
* lockingThread.releaseLock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
38 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
39 |
public class MonitorLockingThread extends Thread { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
40 |
/* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
41 |
* Class MonitorLockingThread is written for usage in tests provoking monitor contention. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
42 |
* Typically in these tests exists thread holding lock (MonitorLockingThread) and another |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
43 |
* thread trying to acquire the same lock. But this scenario also requires one more thread |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
44 |
* which will force MonitorLockingThread to release lock when contention occurred, for this purpose |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
45 |
* auxiliary thread class LockFreeThread is written. |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
46 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
47 |
* Example of usage of MonitorLockingThread and LockFreeThread: |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
48 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
49 |
* Object lock = new Object(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
50 |
* MonitorLockingThread monitorLockingThread = new MonitorLockingThread(lock); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
51 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
52 |
* MonitorLockingThread.LockFreeThread lockFreeThread = |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
53 |
* new MonitorLockingThread.LockFreeThread(Thread.currentThread(), monitorLockingThread); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
54 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
55 |
* monitorLockingThread.acquireLock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
56 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
57 |
* lockFreeThread.start(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
58 |
* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
59 |
* // try to acquire lock which is already held by MonitorLockingThread (here monitor contention should occur), |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
60 |
* // when LockFreeThread finds that contention occurred it forces MonitorLockingThread to release lock |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
61 |
* // and current thread is able to continue execution |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
62 |
* synchronized (lock) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
63 |
* } |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
64 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
65 |
public static class LockFreeThread extends Thread { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
66 |
private Thread blockedThread; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
67 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
68 |
private MonitorLockingThread lockingThread; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
69 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
70 |
public LockFreeThread(Thread blockedThread, MonitorLockingThread lockingThread) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
71 |
this.blockedThread = blockedThread; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
72 |
this.lockingThread = lockingThread; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
73 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
74 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
75 |
public void run() { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
76 |
/* |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
77 |
* Wait when blockedThread's state will switch to 'BLOCKED' (at that moment monitor contention |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
78 |
* should already occur) and then force MonitorLockingThread to release lock |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
79 |
*/ |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
80 |
while (blockedThread.getState() != Thread.State.BLOCKED) |
59021 | 81 |
Thread.yield(); |
49934
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
82 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
83 |
lockingThread.releaseLock(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
84 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
85 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
86 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
87 |
private volatile boolean isRunning = true; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
88 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
89 |
private volatile boolean holdsLock; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
90 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
91 |
private Object lockToHold; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
92 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
93 |
public MonitorLockingThread(Object lockToHold) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
94 |
this.lockToHold = lockToHold; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
95 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
96 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
97 |
public void run() { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
98 |
synchronized (lockToHold) { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
99 |
holdsLock = true; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
100 |
while (isRunning) |
59021 | 101 |
Thread.yield(); |
49934
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
102 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
103 |
holdsLock = false; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
104 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
105 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
106 |
public void releaseLock() { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
107 |
isRunning = false; |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
108 |
while (holdsLock) |
59021 | 109 |
Thread.yield(); |
49934
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
110 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
111 |
|
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
112 |
public void acquireLock() { |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
113 |
start(); |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
114 |
while (!holdsLock) |
59021 | 115 |
Thread.yield(); |
49934
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
116 |
} |
44839fbb20db
8199643: [TESTBUG] Open source common VM testbase code
iignatyev
parents:
diff
changeset
|
117 |
} |