File: Deque.hx

package info (click to toggle)
haxe 1%3A3.2.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 23,464 kB
  • ctags: 9,612
  • sloc: ml: 83,200; ansic: 1,724; makefile: 473; java: 349; cs: 314; python: 250; sh: 43; cpp: 39; xml: 25
file content (73 lines) | stat: -rw-r--r-- 1,205 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package java.vm;
import java.Lib;

/**
	A Lock-free Queue implementation
**/
@:native('haxe.java.vm.Deque')
@:nativeGen class Deque<T>
{
	@:private var head:Node<T>;
	@:private var tail:Node<T>;

	public function new()
	{
		this.head = this.tail = new Node(null);
	}

	public function add(i : T)
	{
		var n = new Node(i);
		untyped __lock__(this,
		{
			tail.next = n;
			tail = n;
			try { untyped this.notify(); } catch(e:Dynamic) { throw e; }
		});
	}

	public function push(i : T)
	{
		var n = new Node(i);
		untyped __lock__(this,
		{
			n.next = head.next;
			head.next = n;
			try { untyped this.notify(); } catch(e:Dynamic) { throw e; }
		});
	}

	public function pop(block : Bool) : Null<T>
	{
		var ret = null;
		untyped __lock__(this, {
			var n = null;
			do {
				n = head.next;
				if (n != null)
				{
					ret = n.value;
					n.value = null;
					head = n;
				} else if (block) {
					//block
					try { untyped this.wait(); } catch(e:Dynamic) { throw e; }
				}
			} while( block && n == null );
		});
		return ret;
	}
}

@:native('haxe.java.vm.DequeNode')
@:nativeGen
class Node<T>
{
	public var value:T;
	public var next:Node<T>;

	public function new(val)
	{
		this.value = val;
	}
}