11use core:: { hash:: BuildHasher , time:: Duration } ;
22
3- use alloc:: { boxed:: Box , format, sync :: Arc , vec} ;
3+ use alloc:: { boxed:: Box , format, vec} ;
44
55use dyn_clone:: clone_box;
6- use event_listener:: Event ;
76use hashbrown:: DefaultHashBuilder ;
8- use java_class_proto:: { JavaFieldProto , JavaMethodProto } ;
7+ use java_class_proto:: JavaMethodProto ;
98use java_constants:: MethodAccessFlags ;
10- use jvm:: { Array , ClassInstance , ClassInstanceRef , Jvm , Result , runtime:: JavaLangString } ;
9+ use jvm:: { ClassInstance , ClassInstanceRef , Jvm , Result , runtime:: JavaLangString } ;
1110
1211use crate :: { Runtime , RuntimeClassProto , RuntimeContext , SpawnCallback , classes:: java:: lang:: String } ;
1312
@@ -34,7 +33,7 @@ impl Object {
3433 JavaMethodProto :: new( "wait" , "()V" , Self :: wait, Default :: default ( ) ) ,
3534 JavaMethodProto :: new( "finalize" , "()V" , Self :: finalize, Default :: default ( ) ) ,
3635 ] ,
37- fields : vec ! [ JavaFieldProto :: new ( "waitEvent" , "[B" , Default :: default ( ) ) ] ,
36+ fields : vec ! [ ] ,
3837 access_flags : Default :: default ( ) ,
3938 }
4039 }
@@ -107,27 +106,15 @@ impl Object {
107106 async fn notify ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > ) -> Result < ( ) > {
108107 tracing:: debug!( "java.lang.Object::notify({:?})" , & this) ;
109108
110- let wait_event: ClassInstanceRef < Array < i8 > > = jvm. get_field ( & this, "waitEvent" , "[B" ) . await ?;
111- if wait_event. is_null ( ) {
112- return Ok ( ( ) ) ;
113- }
114-
115- let wait_event: Arc < Event > = jvm. get_rust_object_field ( & this, "waitEvent" ) . await ?;
116- wait_event. notify ( 1 ) ;
109+ jvm. object_notify ( & this, 1 ) ;
117110
118111 Ok ( ( ) )
119112 }
120113
121114 async fn notify_all ( jvm : & Jvm , _: & mut RuntimeContext , this : ClassInstanceRef < Self > ) -> Result < ( ) > {
122115 tracing:: debug!( "java.lang.Object::notifyAll({:?})" , & this) ;
123116
124- let wait_event: ClassInstanceRef < Array < i8 > > = jvm. get_field ( & this, "waitEvent" , "[B" ) . await ?;
125- if wait_event. is_null ( ) {
126- return Ok ( ( ) ) ;
127- }
128-
129- let wait_event: Arc < Event > = jvm. get_rust_object_field ( & this, "waitEvent" ) . await ?;
130- wait_event. notify ( usize:: MAX ) ;
117+ jvm. object_notify ( & this, usize:: MAX ) ;
131118
132119 Ok ( ( ) )
133120 }
@@ -140,24 +127,21 @@ impl Object {
140127 Ok ( ( ) )
141128 }
142129
143- async fn wait_long_int ( jvm : & Jvm , context : & mut RuntimeContext , mut this : ClassInstanceRef < Self > , millis : i64 , nanos : i32 ) -> Result < ( ) > {
130+ async fn wait_long_int ( jvm : & Jvm , context : & mut RuntimeContext , this : ClassInstanceRef < Self > , millis : i64 , nanos : i32 ) -> Result < ( ) > {
144131 tracing:: debug!( "java.lang.Object::wait({:?}, {:?}, {:?})" , & this, millis, nanos) ;
145132
146- let wait_event = Arc :: new ( Event :: new ( ) ) ;
147- jvm. put_rust_object_field ( & mut this, "waitEvent" , wait_event. clone ( ) ) . await ?;
148-
149- struct Waiter {
133+ struct TimeoutNotifier {
150134 timeout : i64 ,
151- wait_event : Arc < Event > ,
135+ jvm : Jvm ,
136+ this : Box < dyn ClassInstance > ,
152137 context : Box < dyn Runtime > ,
153138 }
154139
155140 #[ async_trait:: async_trait]
156- impl SpawnCallback for Waiter {
141+ impl SpawnCallback for TimeoutNotifier {
157142 async fn call ( & self ) -> Result < ( ) > {
158143 self . context . sleep ( Duration :: from_millis ( self . timeout as _ ) ) . await ;
159-
160- self . wait_event . notify ( 1 ) ; // TODO this would notify other waiter
144+ self . jvm . object_notify ( & self . this , 1 ) ;
161145 Ok ( ( ) )
162146 }
163147 }
@@ -166,15 +150,16 @@ impl Object {
166150 if timeout != 0 {
167151 context. spawn (
168152 jvm,
169- Box :: new ( Waiter {
153+ Box :: new ( TimeoutNotifier {
170154 timeout,
171- wait_event : wait_event. clone ( ) ,
155+ jvm : jvm. clone ( ) ,
156+ this : this. clone ( ) . into ( ) ,
172157 context : clone_box ( context) ,
173158 } ) ,
174159 ) ;
175160 }
176161
177- wait_event . listen ( ) . await ;
162+ jvm . object_wait ( & this ) . await ? ;
178163
179164 Ok ( ( ) )
180165 }
0 commit comments