summaryrefslogtreecommitdiff
path: root/src/cli/runtime/Transaction.cs
blob: e3490548f46f39d9e5a9c62cf001f9fb1bb39d22 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/**
 *   Copyright (c) Rich Hickey. All rights reserved.
 *   The use and distribution terms for this software are covered by the
 *   Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
 *   which can be found in the file CPL.TXT at the root of this distribution.
 *   By using this software in any fashion, you are agreeing to be bound by
 * 	 the terms of this license.
 *   You must not remove this notice, or any other, from this software.
 **/

/* rich May 30, 2006 */

using System;
using System.Threading;
using System.Collections.Generic;

namespace org.clojure.runtime
{
public class Transaction{

public const int COMMITTED = 0;
public const int WORKING = 1;
static readonly Object lockObj = new Object();

volatile static int nextSeq = 1;

static int getNextSeq(){
lock (lockObj)
{
		return nextSeq++;
	}
}

public class Info{
internal int seq;
internal int status;


internal Info(int seq,int status){
	this.seq = seq;
	this.status = status;
}
}


Info info;
int startSeq;

Dictionary<TRef,Object> sets;
Dictionary<TRef,ISeq> commutates;


static public Object runInTransaction(ThreadLocalData tld,IFn fn) {
	if(tld.transaction != null)
		return fn.invoke(tld);
	tld.transaction = new Transaction();
	try{
		return tld.transaction.run(tld, fn);
		}
	finally{
		tld.transaction = null;
		}
}

static public TRef tref(ThreadLocalData tld, Object val) {
	Transaction trans = tld.getTransaction();
	TRef tref = new TRef();
	trans.set(tref, val);
	return tref;
}

static public Object get(ThreadLocalData tld, TRef tref) {
	 return tld.getTransaction().get(tref);
}

static public Object set(ThreadLocalData tld, TRef tref, Object val) {
	 return tld.getTransaction().set(tref,val);
}

static public void touch(ThreadLocalData tld, TRef tref) {
	tld.getTransaction().touch(tref);
}

static public void commutate(ThreadLocalData tld, TRef tref, IFn fn) {
	tld.getTransaction().commutate(tref, fn);
}


Object run(ThreadLocalData tld, IFn fn) {
	bool done = false;
	Object ret = null;
	List<TRef> locks = null;
	List<TRef> locked = null;

	while(!done){
		try
			{
			ret = fn.invoke(tld);
			if(locks == null && (sets != null || commutates != null))
				locks = new List<TRef>();
			if(sets != null)
				locks.AddRange(sets.Keys);
			if(commutates != null)
				locks.AddRange(commutates.Keys);
			if(locks != null)
				{
				if(locked == null)
					locked = new List<TRef>(locks.Count);
				//lock in order, to avoid deadlocks
				locks.Sort();
				foreach(TRef tref in locks)
					{
					//will block here
					Monitor.Enter(tref);
					locked.Add(tref);
					if(sets.ContainsKey(tref))
						{
						//try again if the thing we are trying to set has changed since we started
						TVal curr = getCurrent(tref);
						if(curr != null && curr.tinfo.seq > startSeq)
							goto loop;
						}
					}
				}

			//at this point all write targets are locked
			//turn commutates into sets
			foreach(KeyValuePair<TRef, ISeq> e in commutates)
				{
				TRef tref = e.Key;
				//note this will npe if tref has never been set, as designed
				Object val = getCurrent(tref).val;
				for(ISeq c = e.Value;c!=null;c = c.rest())
					{
					IFn f = (IFn) c.first();
					val = f.invoke(tld, val);
					}
				sets[tref] =  val;
				}

			//set the new vals
			foreach(KeyValuePair<TRef, Object> entry in sets)
				{
				TRef tref = entry.Key;
				tref.push(entry.Value, info);
				}

			//atomic commit
			lock(lockObj){
				info.seq = getNextSeq();
				info.status = COMMITTED;
			}

			done = true;
			loop:
			    ;
			}
		finally{
			if(locked != null)
				{
				foreach(TRef tref in locked)
					{
					Monitor.Exit(tref);
					}
				locked.Clear();
				}
			reset();
			if(locks != null)
				locks.Clear();
			}
		}
	return ret;
}

private void reset(){
	if(sets != null)
		sets.Clear();
	if(commutates != null)
		commutates.Clear();

}


Transaction(){
	lock(lockObj){
		int seq = getNextSeq();
		this.info = new Info(seq, WORKING);
		this.startSeq = seq;
	}
}

Object get(TRef tref) {
	if(sets != null && sets.ContainsKey(tref))
		return sets[tref];

    for(TVal ver = tref;ver != null;ver = ver.prior)
	    {
	    //note this will npe if tref has never been set, as designed
	    if(ver.tinfo.status == COMMITTED && ver.tinfo.seq <= startSeq)
		    return ver.val;
	    }

	throw new Exception("Version not found");
}

static TVal getCurrent(TRef tref) {
    for(TVal ver = tref;ver != null;ver = ver.prior)
	    {
	    if(ver.tinfo != null && ver.tinfo.status == COMMITTED)
		    return ver;
	    }
	//this return only if no value was ever successfully set
	return null;
}

Object set(TRef tref, Object val) {
	if(sets == null)
		sets = new Dictionary<TRef,Object>();
	if(commutates != null && commutates.ContainsKey(tref))
		throw new Exception("Can't commutate and set a TRef in the same transaction");

	sets[tref] =val;
	return val;
	}

void touch(TRef tref) {
	set(tref, get(tref));
	}

void commutate(TRef tref, IFn fn) {
	if(commutates == null)
		commutates = new Dictionary<TRef,ISeq>();
	if(sets != null && sets.ContainsKey(tref))
		throw new Exception("Can't commutate and set a TRef in the same transaction");
	commutates[tref] = RT.cons(fn, commutates[tref]);
	}

}
}