Mercurial > hg > Database > jungle-sharp
comparison src/main/csharp/jp.ac.u-ryukyu.ie.cr/jungle/transaction/AtomicReference.cs @ 7:02b2ab7bffe6
fix
author | Kazuma |
---|---|
date | Tue, 27 Sep 2016 18:36:05 +0900 |
parents | dec15de2c6ff |
children |
comparison
equal
deleted
inserted
replaced
5:0428c8888abf | 7:02b2ab7bffe6 |
---|---|
1 using System.Threading; | 1 using System.Threading; |
2 | 2 |
3 public class AtomicReference <T> where T : class { | 3 public class AtomicReference <T> where T : class { |
4 private T value; | 4 private T value; |
5 private bool isSet = false; | |
6 | |
7 public AtomicReference() { } | |
8 | 5 |
9 public AtomicReference(T value) { | 6 public AtomicReference(T value) { |
10 this.value = value; | 7 this.value = value; |
11 } | 8 } |
12 | 9 |
13 public T CompareAndSet(T newValue) { | 10 public bool CompareAndSet(T newValue, T prevValue) { |
14 // change to compere exchange. | 11 // change to compere exchange. |
15 isSet = true; | 12 T oldValue = value; |
16 return Interlocked.CompareExchange(ref value, value, newValue); | 13 return (oldValue != Interlocked.CompareExchange (ref value, newValue, prevValue)); |
17 } | 14 } |
18 | 15 |
19 public bool OptimicSet(T oldvalue) { | |
20 T old; | |
21 do { | |
22 old = value; | |
23 } while (old != CompareAndSet (value)); | |
24 return isSet; | |
25 } | |
26 | 16 |
27 public T Get() { | 17 public T Get() { |
28 return value; | 18 return value; |
29 } | 19 } |
30 } | 20 } |