2
0
Эх сурвалжийг харах

[java] minor fix and Lock implementation

Caue Waneck 12 жил өмнө
parent
commit
4ee573f42c

+ 4 - 1
gencommon.ml

@@ -5784,7 +5784,10 @@ struct
         in
         in
         let error = error || (match follow actual_t with | TFun _ -> false | _ -> true) in
         let error = error || (match follow actual_t with | TFun _ -> false | _ -> true) in
         if error then (* if error, ignore arguments *)
         if error then (* if error, ignore arguments *)
-          mk_cast ecall.etype { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist ) }
+          if is_void ecall.etype then
+            { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist ) }
+          else
+            mk_cast ecall.etype { ecall with eexpr = TCall({ e1 with eexpr = TField(!ef, f) }, elist ) }
         else begin
         else begin
           (* infer arguments *)
           (* infer arguments *)
           (* let called_t = TFun(List.map (fun e -> "arg",false,e.etype) elist, ecall.etype) in *)
           (* let called_t = TFun(List.map (fun e -> "arg",false,e.etype) elist, ecall.etype) in *)

+ 1 - 10
genjava.ml

@@ -2187,21 +2187,12 @@ let generate con =
 
 
   let basic = con.basic in
   let basic = con.basic in
   (* make the basic functions in java *)
   (* make the basic functions in java *)
-  let wait = mk_class_field "wait" (TFun([], basic.tvoid)) true Ast.null_pos (Method MethNormal) [] in
-  wait.cf_meta <- (Meta.Overload,[],wait.cf_pos) :: wait.cf_meta;
-  (try
-    let i64 = mt_to_t_dyn (get_type gen (["haxe"],"Int64")) in
-    wait.cf_overloads <- [
-      mk_class_field "wait" (TFun(["timeout",false,i64], basic.tvoid)) true Ast.null_pos (Method MethNormal) [];
-      mk_class_field "wait" (TFun(["timeout",false,i64; "nanos",false,basic.tint], basic.tvoid)) true Ast.null_pos (Method MethNormal) [];
-    ]
-  with | TypeNotFound _ -> ());
   let basic_fns =
   let basic_fns =
   [
   [
     mk_class_field "equals" (TFun(["obj",false,t_dynamic], basic.tbool)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "equals" (TFun(["obj",false,t_dynamic], basic.tbool)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "toString" (TFun([], basic.tstring)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "toString" (TFun([], basic.tstring)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "hashCode" (TFun([], basic.tint)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "hashCode" (TFun([], basic.tint)) true Ast.null_pos (Method MethNormal) [];
-    wait;
+    mk_class_field "wait" (TFun([], basic.tvoid)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "notify" (TFun([], basic.tvoid)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "notify" (TFun([], basic.tvoid)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "notifyAll" (TFun([], basic.tvoid)) true Ast.null_pos (Method MethNormal) [];
     mk_class_field "notifyAll" (TFun([], basic.tvoid)) true Ast.null_pos (Method MethNormal) [];
   ] in
   ] in

+ 1 - 1
std/java/vm/AtomicList.hx

@@ -3,7 +3,7 @@ package java.vm;
 import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.atomic.AtomicReference;
 
 
 /**
 /**
-	A lock-free stack implementation
+	A lock-free queue implementation
 **/
 **/
 @:native('haxe.java.vm.AtomicList')
 @:native('haxe.java.vm.AtomicList')
 @:nativeGen class AtomicList<T>
 @:nativeGen class AtomicList<T>

+ 70 - 0
std/java/vm/Lock.hx

@@ -0,0 +1,70 @@
+package java.vm;
+import java.Lib;
+
+@:native('haxe.java.vm.Lock') class Lock
+{
+	@:private @:volatile var releasedCount = 0;
+
+	/**
+		Creates a new lock, which is initially locked
+	**/
+	public function new()
+	{
+
+	}
+
+	/**
+		Waits for a lock to be released and acquire it.
+		If `timeout` (in seconds) is not null and expires then the returned value is false
+	**/
+	public function wait(?timeout : Float) : Bool
+	{
+		var ret = false;
+		untyped __lock__(this,
+		{
+			if (--releasedCount >= 0)
+				return true;
+			if (timeout == null)
+			{
+				try
+				{
+					untyped __java__("this.wait()");
+				}
+				catch(e:Dynamic) {
+					throw e;
+				}
+			} else {
+				var t:Dynamic = this;
+				try
+				{
+					var t:haxe.Int64 = cast timeout * 1000;
+					untyped __java__("this.wait(t)");
+				}
+				catch(e:Dynamic) {
+					throw e;
+				}
+			}
+			ret = this.releasedCount >= 0;
+			if (!ret && ++this.releasedCount == 0) //even if timeout failed, we should release the lock; Other locks may be released then
+			{
+				untyped this.notify();
+			}
+		});
+		return ret;
+	}
+
+	/**
+		Release a lock. The thread does not need to own the lock to be able to release it.
+		If a lock is released several times, it can be acquired as many times
+	**/
+	public function release()
+	{
+		untyped __lock__(this,
+		{
+			if (++releasedCount >= 0)
+			{
+				untyped this.notify();
+			}
+		});
+	}
+}