Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/gc/lock/CriticalSectionLocker.java
41162 views
1
/*
2
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
package nsk.share.gc.lock;
25
26
import nsk.share.TestBug;
27
import nsk.share.gc.lock.Locker;
28
29
/**
30
* CriticalSectionLocker represents a way to lock a resource
31
* by entering some critical section.
32
*/
33
public abstract class CriticalSectionLocker<T> implements Locker<T> {
34
private transient boolean enabled = false;
35
private transient boolean locked = false;
36
private Object sync = new Object();
37
private Thread thread;
38
private Throwable exception;
39
40
private final Runnable runnable = new Runnable() {
41
public void run() {
42
//System.out.println("Running");
43
try {
44
do {
45
synchronized (sync) {
46
while (enabled && !locked) {
47
try {
48
sync.wait();
49
} catch (InterruptedException e) {
50
}
51
}
52
if (!enabled)
53
break;
54
}
55
do {
56
criticalSection();
57
} while (locked);
58
} while (enabled);
59
// System.out.println("Exiting");
60
} catch (RuntimeException t) {
61
t.printStackTrace();
62
exception = t;
63
throw t;
64
}
65
}
66
};
67
68
public CriticalSectionLocker() {
69
}
70
71
/**
72
* Enter critical section.
73
*
74
*/
75
protected abstract void criticalSection();
76
77
public void enable() {
78
synchronized (sync) {
79
if (enabled)
80
throw new TestBug("Locker already enabled.");
81
// System.out.println("Enabling " + this);
82
enabled = true;
83
thread = new Thread(runnable, "Locker: " + runnable);
84
thread.setDaemon(true);
85
thread.start();
86
}
87
}
88
89
public void lock() {
90
synchronized (sync) {
91
if (locked)
92
throw new TestBug("Locker already locked.");
93
// System.out.println("Locking " + this);
94
locked = true;
95
sync.notifyAll();
96
}
97
}
98
99
public void unlock() {
100
synchronized (sync) {
101
if (!locked)
102
throw new TestBug("Locker not locked.");
103
// System.out.println("Unocking " + this);
104
locked = false;
105
sync.notifyAll();
106
}
107
}
108
109
public void disable() {
110
synchronized (sync) {
111
if (!enabled)
112
throw new TestBug("Locker not enabled.");
113
// System.out.println("Disabling " + this);
114
enabled = false;
115
sync.notifyAll();
116
}
117
try {
118
thread.join();
119
} catch (InterruptedException e) {
120
throw new TestBug(e);
121
}
122
}
123
124
public Throwable getException() {
125
return exception;
126
}
127
}
128
129