66// Copyright © 2015 TypeLift. All rights reserved.
77//
88
9- /// A transactional variable
9+ /// A TVar (read: Transactional Variabe) is a shared memory location that
10+ /// supports atomic memory transactions.
1011public struct TVar < T> : Comparable , Hashable {
1112 internal var value : TVarType < T >
1213 let _id : Int
@@ -15,6 +16,13 @@ public struct TVar<T> : Comparable, Hashable {
1516 return _id
1617 }
1718
19+ /// Uses an STM transaction to return the current value stored in the receiver.
20+ public func read( ) -> STM < T > {
21+ return STM { trans in
22+ return trans. readTVar ( self )
23+ }
24+ }
25+
1826 private init ( _ value : TVarType < T > , _ id : Int ) {
1927 self . value = value
2028 self . _id = id
@@ -23,12 +31,6 @@ public struct TVar<T> : Comparable, Hashable {
2331 internal var upCast : TVar < Any > {
2432 return TVar < Any > ( self . value. upCast, self . _id)
2533 }
26-
27- public func read( ) -> STM < T > {
28- return STM { trans in
29- return trans. readTVar ( self )
30- }
31- }
3234}
3335
3436extension TVar where T : Equatable {
@@ -38,6 +40,7 @@ extension TVar where T : Equatable {
3840 self . _id = nextId
3941 }
4042
43+ /// Uses an STM transaction to write the supplied value into the receiver.
4144 public func write( value : T ) -> STM < ( ) > {
4245 return STM< T> { ( trans : TLog ) in
4346 trans. writeTVar ( self , value: PreEquatable ( t: { value } ) )
@@ -53,6 +56,7 @@ extension TVar where T : AnyObject {
5356 self . _id = nextId
5457 }
5558
59+ /// Uses an STM transaction to write the supplied value into the receiver.
5660 public func write( value : T ) -> STM < ( ) > {
5761 return STM< T> { ( trans : TLog ) in
5862 trans. writeTVar ( self , value: UnderlyingRef ( t: { value } ) )
@@ -68,6 +72,7 @@ extension TVar where T : Any {
6872 self . _id = nextId
6973 }
7074
75+ /// Uses an STM transaction to write the supplied value into the receiver.
7176 public func write( value : T ) -> STM < ( ) > {
7277 return STM< T> { ( trans : TLog ) in
7378 trans. writeTVar ( self , value: Ref ( t: { value } ) )
0 commit comments