Skip to content

Commit 62da8dc

Browse files
committed
Merge remote-tracking branch 'origin/GT-3262_emteere_LockProfiler'
2 parents 5057d65 + 9fd8880 commit 62da8dc

File tree

1 file changed

+24
-14
lines changed
  • Ghidra/Framework/Project/src/main/java/ghidra/util

1 file changed

+24
-14
lines changed

Ghidra/Framework/Project/src/main/java/ghidra/util/Lock.java

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* ###
22
* IP: GHIDRA
3-
* REVIEWED: YES
43
*
54
* Licensed under the Apache License, Version 2.0 (the "License");
65
* you may not use this file except in compliance with the License.
@@ -17,45 +16,51 @@
1716
package ghidra.util;
1817

1918
/**
20-
* Ghidra synchronization lock. This class allows creation of named locks for
21-
* modifying tables in the Ghidra data base. This class also creates an instance
22-
* of a global lock that must first be obtained when synchronizing using multiple
23-
* of the named locks.
19+
* Ghidra synchronization lock. This class allows creation of named locks for
20+
* synchroniing modification of multiple tables in the Ghidra database.
2421
*/
2522
public class Lock {
2623
private Thread owner;
27-
private int cnt = 0;
24+
private int lockAquireCount = 0;
25+
private int waiterCount = 0;
2826
private String name;
2927

3028
/**
3129
* Creates an instance of a lock for synchronization within Ghidra.
30+
*
3231
* @param name the name of this lock
3332
*/
3433
public Lock(String name) {
3534
this.name = name;
3635
}
3736

3837
/**
39-
* Acquire this synchronization lock.
40-
* (i.e. begin synchronizing on this named lock.)
38+
* Acquire this synchronization lock. (i.e. begin synchronizing on this named
39+
* lock.)
4140
*/
4241
public synchronized void acquire() {
4342
Thread currThread = Thread.currentThread();
4443

4544
while (true) {
4645
if (owner == null) {
47-
cnt = 1;
46+
lockAquireCount = 1;
4847
owner = currThread;
4948
return;
5049
}
5150
else if (owner == currThread) {
52-
cnt++;
51+
lockAquireCount++;
5352
return;
5453
}
5554
try {
55+
waiterCount++;
5656
wait();
5757
}
5858
catch (InterruptedException e) {
59+
// exception from another threads notify(), ignore
60+
// and try to get lock again
61+
}
62+
finally {
63+
waiterCount--;
5964
}
6065
}
6166
}
@@ -67,10 +72,15 @@ else if (owner == currThread) {
6772
public synchronized void release() {
6873
Thread currThread = Thread.currentThread();
6974

70-
if (cnt > 0 && (owner == currThread)) {
71-
if (--cnt == 0) {
75+
if (lockAquireCount > 0 && (owner == currThread)) {
76+
if (--lockAquireCount == 0) {
7277
owner = null;
73-
notify();
78+
// This is purely to help sample profiling. If notify() is called the
79+
// sampler can attribute time to the methods calling this erroneously. For some reason
80+
// the visualvm sampler gets a sample more often when notify() is called.
81+
if (waiterCount != 0) {
82+
notify();
83+
}
7484
}
7585
}
7686
else {
@@ -80,10 +90,10 @@ public synchronized void release() {
8090

8191
/**
8292
* Gets the thread that currently owns the lock.
93+
*
8394
* @return the thread that owns the lock or null.
8495
*/
8596
public Thread getOwner() {
8697
return owner;
8798
}
88-
8999
}

0 commit comments

Comments
 (0)