Skip to content

Commit 8821b95

Browse files
committed
Remove async-lock
1 parent fc83170 commit 8821b95

File tree

4 files changed

+37
-53
lines changed

4 files changed

+37
-53
lines changed

Cargo.lock

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

java_runtime/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ hashbrown = { workspace = true }
1212
parking_lot = { workspace = true }
1313
tracing = { workspace = true }
1414

15-
async-lock = { version = "^3.4", default-features = false }
1615
chrono = { version = "^0.4", default-features = false }
1716
encoding_rs = { version = "^0.8", features = ["alloc"], default-features = false }
1817
event-listener = { version = "^5.3", default-features = false }

java_runtime/src/classes/java/util/hashtable.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use alloc::{boxed::Box, sync::Arc, vec, vec::Vec};
22
use core::mem;
33

4-
use async_lock::Mutex;
54
use hashbrown::HashMap;
5+
use parking_lot::Mutex;
66

77
use java_class_proto::{JavaFieldProto, JavaMethodProto};
88
use jvm::{ClassInstance, ClassInstanceRef, Jvm, Result};
@@ -56,11 +56,10 @@ impl Hashtable {
5656
let rust_hash_map = Self::get_rust_hashmap(jvm, &this).await?;
5757
let key_hash: i32 = jvm.invoke_virtual(&key, "hashCode", "()I", ()).await?;
5858

59-
let rust_hash_map = rust_hash_map.lock().await;
60-
let vec = rust_hash_map.get(&key_hash);
59+
let vec = rust_hash_map.lock().get(&key_hash).cloned();
6160

6261
if vec.is_some() {
63-
for (key, _) in vec.unwrap() {
62+
for (key, _) in &vec.unwrap() {
6463
let equals = jvm.invoke_virtual(key, "equals", "(Ljava/lang/Object;)Z", ((*key).clone(),)).await?;
6564
if equals {
6665
return Ok(true);
@@ -77,11 +76,10 @@ impl Hashtable {
7776
let rust_hash_map = Self::get_rust_hashmap(jvm, &this).await?;
7877
let key_hash: i32 = jvm.invoke_virtual(&key, "hashCode", "()I", ()).await?;
7978

80-
let rust_hash_map = rust_hash_map.lock().await;
81-
let vec = rust_hash_map.get(&key_hash);
79+
let vec = rust_hash_map.lock().get(&key_hash).cloned();
8280

8381
if vec.is_some() {
84-
for (key, value) in vec.unwrap() {
82+
for (key, value) in &vec.unwrap() {
8583
let equals = jvm.invoke_virtual(key, "equals", "(Ljava/lang/Object;)Z", ((*key).clone(),)).await?;
8684
if equals {
8785
return Ok(value.clone().into());
@@ -104,14 +102,13 @@ impl Hashtable {
104102
let rust_hash_map = Self::get_rust_hashmap(jvm, &this).await?;
105103
let key_hash: i32 = jvm.invoke_virtual(&key, "hashCode", "()I", ()).await?;
106104

107-
let mut rust_hash_map = rust_hash_map.lock().await;
108-
let vec = rust_hash_map.get_mut(&key_hash);
105+
let vec = rust_hash_map.lock().get(&key_hash).cloned();
109106

110107
if vec.is_some() {
111-
for (i, (bucket_key, _)) in vec.as_ref().unwrap().iter().enumerate() {
108+
for (i, (bucket_key, _)) in vec.unwrap().iter().enumerate() {
112109
let equals = jvm.invoke_virtual(bucket_key, "equals", "(Ljava/lang/Object;)Z", (key.clone(),)).await?;
113110
if equals {
114-
let (_, old_value) = vec.unwrap().remove(i);
111+
let (_, old_value) = rust_hash_map.lock().get_mut(&key_hash).unwrap().remove(i);
115112

116113
return Ok(old_value.into());
117114
}
@@ -134,19 +131,28 @@ impl Hashtable {
134131
let rust_hash_map = Self::get_rust_hashmap(jvm, &this).await?;
135132
let key_hash: i32 = jvm.invoke_virtual(&key, "hashCode", "()I", ()).await?;
136133

137-
let mut rust_hash_map = rust_hash_map.lock().await;
138-
let vec = rust_hash_map.entry(key_hash).or_insert_with(Vec::new);
134+
let vec = {
135+
let mut rust_hash_map = rust_hash_map.lock();
136+
if !rust_hash_map.contains_key(&key_hash) {
137+
rust_hash_map.insert(key_hash, Vec::new());
138+
}
139+
140+
rust_hash_map.get(&key_hash).cloned().unwrap()
141+
};
139142

140143
for (i, (bucket_key, _)) in vec.iter().enumerate() {
141144
let equals = jvm.invoke_virtual(bucket_key, "equals", "(Ljava/lang/Object;)Z", (key.clone(),)).await?;
142145
if equals {
146+
let mut rust_hash_map = rust_hash_map.lock();
147+
let vec = rust_hash_map.get_mut(&key_hash).unwrap();
148+
143149
let (_, old_value) = mem::replace(&mut vec[i], (key.into(), value.into()));
144150

145151
return Ok(old_value.into());
146152
}
147153
}
148154

149-
vec.push((key.into(), value.into()));
155+
rust_hash_map.lock().get_mut(&key_hash).unwrap().push((key.into(), value.into()));
150156

151157
Ok(None.into())
152158
}

java_runtime/src/classes/java/util/vector.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::mem;
33
use alloc::vec;
44
use alloc::{boxed::Box, format, sync::Arc, vec::Vec};
55

6-
use async_lock::Mutex;
6+
use parking_lot::Mutex;
77

88
use java_class_proto::{JavaFieldProto, JavaMethodProto};
99
use jvm::{ClassInstance, ClassInstanceRef, Jvm, Result};
@@ -82,7 +82,7 @@ impl Vector {
8282
tracing::debug!("java.util.Vector::add({:?}, {:?})", &this, &element);
8383

8484
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
85-
rust_vector.lock().await.push(element.into());
85+
rust_vector.lock().push(element.into());
8686

8787
Ok(true)
8888
}
@@ -92,7 +92,7 @@ impl Vector {
9292

9393
// do we need to call add() instead?
9494
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
95-
rust_vector.lock().await.push(element.into());
95+
rust_vector.lock().push(element.into());
9696

9797
Ok(())
9898
}
@@ -107,7 +107,7 @@ impl Vector {
107107
tracing::debug!("java.util.Vector::insertElementAt({:?}, {:?}, {:?})", &this, &element, index);
108108

109109
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
110-
rust_vector.lock().await.insert(index as usize, element.into());
110+
rust_vector.lock().insert(index as usize, element.into());
111111

112112
Ok(())
113113
}
@@ -116,7 +116,7 @@ impl Vector {
116116
tracing::debug!("java.util.Vector::elementAt({:?}, {:?})", &this, index);
117117

118118
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
119-
let element = rust_vector.lock().await.get(index as usize).unwrap().clone();
119+
let element = rust_vector.lock().get(index as usize).unwrap().clone();
120120

121121
Ok(element.into())
122122
}
@@ -131,7 +131,7 @@ impl Vector {
131131
tracing::debug!("java.util.Vector::set({:?}, {:?}, {:?})", &this, index, &element);
132132

133133
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
134-
let old_element = mem::replace(&mut rust_vector.lock().await[index as usize], element.into());
134+
let old_element = mem::replace(&mut rust_vector.lock()[index as usize], element.into());
135135

136136
Ok(old_element.into())
137137
}
@@ -140,7 +140,7 @@ impl Vector {
140140
tracing::debug!("java.util.Vector::size({:?})", &this);
141141

142142
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
143-
let size = rust_vector.lock().await.len();
143+
let size = rust_vector.lock().len();
144144

145145
Ok(size as _)
146146
}
@@ -149,7 +149,7 @@ impl Vector {
149149
tracing::debug!("java.util.Vector::isEmpty({:?})", &this);
150150

151151
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
152-
let is_empty = rust_vector.lock().await.is_empty();
152+
let is_empty = rust_vector.lock().is_empty();
153153

154154
Ok(is_empty)
155155
}
@@ -158,7 +158,7 @@ impl Vector {
158158
tracing::debug!("java.util.Vector::remove({:?}, {:?})", &this, index);
159159

160160
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
161-
let removed = rust_vector.lock().await.remove(index as usize);
161+
let removed = rust_vector.lock().remove(index as usize);
162162

163163
Ok(removed.into())
164164
}
@@ -167,7 +167,7 @@ impl Vector {
167167
tracing::debug!("java.util.Vector::removeAllElements({:?})", &this);
168168

169169
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
170-
rust_vector.lock().await.clear();
170+
rust_vector.lock().clear();
171171

172172
Ok(())
173173
}
@@ -176,7 +176,7 @@ impl Vector {
176176
tracing::debug!("java.util.Vector::removeElementAt({:?}, {:?})", &this, index);
177177

178178
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
179-
rust_vector.lock().await.remove(index as usize);
179+
rust_vector.lock().remove(index as usize);
180180

181181
Ok(())
182182
}
@@ -185,7 +185,7 @@ impl Vector {
185185
tracing::debug!("java.util.Vector::lastIndexOf({:?}, {:?})", &this, &element);
186186

187187
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
188-
let index = rust_vector.lock().await.len() - 1;
188+
let index = rust_vector.lock().len() - 1;
189189

190190
let index: i32 = jvm
191191
.invoke_virtual(&this, "lastIndexOf", "(Ljava/lang/Object;I)I", (element, index as i32))
@@ -204,15 +204,16 @@ impl Vector {
204204
tracing::debug!("java.util.Vector::lastIndexOf({:?}, {:?}, {:?})", &this, &element, index);
205205

206206
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
207-
let vector = rust_vector.lock().await;
208-
let size = vector.len();
207+
let size = rust_vector.lock().len();
209208

210209
if index as usize >= size {
211210
return Err(jvm
212211
.exception("java/lang/IndexOutOfBoundsException", &format!("{} >= {}", index, size))
213212
.await);
214213
}
215214

215+
let vector = rust_vector.lock();
216+
216217
for (i, item) in vector[..=index as usize].iter().enumerate().rev() {
217218
if item.is_none() {
218219
if element.is_null() {
@@ -235,11 +236,11 @@ impl Vector {
235236

236237
let rust_vector = Self::get_rust_vector(jvm, &this).await?;
237238

238-
if rust_vector.lock().await.len() == 0 {
239+
if rust_vector.lock().len() == 0 {
239240
return Ok(None.into());
240241
}
241242

242-
let element = rust_vector.lock().await.first().cloned().unwrap();
243+
let element = rust_vector.lock().first().cloned().unwrap();
243244

244245
Ok(element.into())
245246
}

0 commit comments

Comments
 (0)