summaryrefslogtreecommitdiff
path: root/gee/lazy.vala
blob: 5e7bc3689c3e4d3ee599118b6453d6011c1f7470 (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* lazy.vala
 *
 * Copyright (C) 2011  Maciej Piechotka
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 * Author:
 * 	Maciej Piechotka <uzytkownik2@gmail.com>
 */

namespace Gee {
	public delegate G LazyFunc<G> ();
}

/**
 * Represents a lazy value. I.e. value that is computed on demand.
 *
 * This class is not thread-safe.
 */
public class Gee.Lazy<G> {
	public Lazy (owned LazyFunc<G> func) {
		_func = (owned)func;
	}

	public Lazy.from_value (G item) {
		_value = item;
	}

	public void eval () {
		if (_func != null) {
			_value = _func ();
			_func = null;
		}
	}

	public new G get () {
		eval ();
		return _value;
	}

	public new G value {
		get {
			eval ();
			return _value;
		}
	}

	/**
	 * Provides a future for a lazy value.
	 *
	 * Note: The future can be requested only once and all access must be
	 *   done through it.
	 */
	public Gee.Future<G>? future {
		owned get {
			return new Future<G> (this);
		}
	}

	private LazyFunc<G>? _func;
	private G? _value;

	private class Future<G> : Object, Gee.Future<G> {
		public Future (Lazy<G> lazy) {
			_lazy = lazy;
			_when_done = new Gee.Future.SourceFuncArrayElement<G>[0];
		}

		public bool ready {
			get {
				_mutex.lock ();
				bool result = _lazy._func == null;
				_mutex.unlock ();
				return result;
			}
		}

		public GLib.Error? exception {get {return null;}}

		public unowned G wait () throws Gee.FutureError {
			_mutex.lock ();
			if (_lazy._func != null) {
				if (_state == State.EVAL) {
					_eval.wait (_mutex);
					_mutex.unlock ();
				} else {
					do_eval ();
				}
			} else {
				_mutex.unlock ();
			}
			return _lazy._value;
		}

		public unowned bool wait_until (int64 end_time, out unowned G? value = null) throws Gee.FutureError {
			_mutex.lock ();
			if (_lazy._func != null) {
				if (_state == State.EVAL) {
					bool res = _eval.wait_until (_mutex, end_time);
					_mutex.unlock ();
					if (!res) {
						value = null;
						return false;
					}
				} else {
					do_eval ();
				}
			} else {
				_mutex.unlock ();
			}
			value = _lazy._value;
			return true;
		}

		public async unowned G wait_async () throws Gee.FutureError {
			_mutex.lock ();
			if (_lazy._func != null) {
				if (_state == State.EVAL) {
					_when_done += SourceFuncArrayElement(wait_async.callback);
					yield Gee.Utils.Async.yield_and_unlock (_mutex);
				} else {
					do_eval ();
				}
			} else {
				_mutex.unlock ();
			}
			return _lazy.value;
		}

		private void do_eval () {
			_state = State.EVAL;
			_mutex.unlock ();

			_lazy._value = _lazy._func ();

			_mutex.lock ();
			_lazy._func = null;
			_state = State.UNLOCK;
			_eval.broadcast ();
			_mutex.unlock ();

			Gee.Future.SourceFuncArrayElement<G>[] when_done = (owned)_when_done;
			for (int i = 0; i < when_done.length; i++) {
				when_done[i].func ();
			}
		}

		private Mutex _mutex = Mutex ();
		private Cond _eval = Cond ();
		private Lazy<G> _lazy;
		private State _state = State.UNLOCK;
		private Gee.Future.SourceFuncArrayElement<G>[]? _when_done;
		private enum State {
			UNLOCK,
			EVAL
		}
	}
}