@@ -25,13 +25,11 @@ type IAsyncEnumerable<'T> =
2525type AsyncSeq < 'T > = IAsyncEnumerable< 'T>
2626// abstract GetEnumerator : unit -> IAsyncEnumerator<'T>
2727
28- type AsyncSeqSrc < 'a > = private { mutable tail : AsyncSeqSrcNode < 'a > }
28+ type AsyncSeqSrc < 'a > = private { tail : AsyncSeqSrcNode < 'a > ref }
2929
3030and private AsyncSeqSrcNode < 'a > =
31- struct
32- val tcs : TaskCompletionSource <( 'a * AsyncSeqSrcNode < 'a >) option >
33- new ( tcs ) = { tcs = tcs }
34- end
31+ val tcs : TaskCompletionSource <( 'a * AsyncSeqSrcNode < 'a >) option >
32+ new ( tcs ) = { tcs = tcs }
3533
3634[<AutoOpen>]
3735module internal Utils =
@@ -1400,19 +1398,18 @@ module AsyncSeq =
14001398 new AsyncSeqSrcNode<_>( new TaskCompletionSource<_>())
14011399
14021400 let create () : AsyncSeqSrc < 'a > =
1403- { tail = createNode () }
1401+ { tail = ref ( createNode () ) }
14041402
14051403 let put ( a : 'a ) ( s : AsyncSeqSrc < 'a >) =
14061404 let newTail = createNode ()
1407- let tail = s.tail
1408- s.tail <- newTail
1405+ let tail = Interlocked.Exchange( s.tail, newTail)
14091406 tail.tcs.SetResult( Some( a, newTail))
14101407
14111408 let close ( s : AsyncSeqSrc < 'a >) : unit =
1412- s.tail.tcs.SetResult( None)
1409+ s.tail.Value. tcs.SetResult( None)
14131410
14141411 let error ( ex : exn ) ( s : AsyncSeqSrc < 'a >) : unit =
1415- s.tail.tcs.SetException( ex)
1412+ s.tail.Value. tcs.SetException( ex)
14161413
14171414 let rec private toAsyncSeqImpl ( s : AsyncSeqSrcNode < 'a >) : AsyncSeq < 'a > =
14181415 asyncSeq {
@@ -1424,7 +1421,7 @@ module AsyncSeq =
14241421 yield ! toAsyncSeqImpl tl }
14251422
14261423 let toAsyncSeq ( s : AsyncSeqSrc < 'a >) : AsyncSeq < 'a > =
1427- toAsyncSeqImpl s.tail
1424+ toAsyncSeqImpl s.tail.Value
14281425
14291426
14301427
0 commit comments