anchor.diff:      /// <param name="source">Backend source for distributed cancellation tokens.</param>
       /// <param name="parent">Parent distributed cancellation token source. Defaults to no parent.</param>
       /// <param name="elevate">Directly elevate cancellation token to distributed. Defaults to false.</param>
  -    static member internal Create(source : ICloudDictionary<DistributedCancellationTokenInfo>, ?parent : DistributedCancellationToken, ?elevate : bool) = async {
  -        let elevate = defaultArg elevate false
  -        match parent with
  -        | None when elevate ->
  -            let! id = tryCreateToken source None
  -            return new DistributedCancellationToken(source, Distributed <| Option.get id)
  -        | _ ->
  -            let dcts = new DistributedCancellationToken(source, Localized(parent))
  -            if elevate then dcts.TryElevateToDistributed() |> ignore
  -            return dcts
  -    }
  -
  -and [<Sealed;DataContract>] DistributedCancellationTokenFactory private (source : ICloudDictionary<DistributedCancellationTokenInfo>) =
  -    
  -    [<DataMember(Name = "Source")>]
  -    let source = source
  -
  +    member __.Create(?parent : DistributedCancellationToken<'Token>, ?elevate : bool) =
  +        DistributedCancellationToken.Create(factory, ?parent = parent, ?elevate = elevate)
  +
  +
  +type DistributedCancellationToken =
       /// <summary>
  -    ///     Creates a distributed cancellation token source with lazy elevation to distributed cancellation semantics.
  +    ///     Creates a serializable distributed cancellation token factory
       /// </summary>
  -    /// <param name="source">Backend source for distributed cancellation tokens.</param>
  -    /// <param name="parent">Parent distributed cancellation token source. Defaults to no parent.</param>
  -    /// <param name="elevate">Directly elevate cancellation token to distributed. Defaults to false.</param>
  -    member __.Create(?parent : DistributedCancellationToken, ?elevate : bool) =
  -        DistributedCancellationToken.Create(source, ?parent = parent, ?elevate = elevate)
  -
  -    static member Create(dictionaryProvider : ICloudDictionaryProvider) = async {
  -        let! dict = dictionaryProvider.Create<DistributedCancellationTokenInfo>()
  -        return new DistributedCancellationTokenFactory(dict)
  -    }
  -
  -    interface ICancellationTokenSourceFactory with
  -        member x.CreateCancellationTokenSource() = async {
  -            let! dcts = DistributedCancellationToken.Create(source)
  -            return dcts :> ICloudCancellationTokenSource
  -        }
  -        
  -        member x.CreateLinkedCancellationTokenSource(parent: ICloudCancellationToken) = async {
  -            match parent with
  -            | :? DistributedCancellationToken as pdcts ->
  -                let! dcts = DistributedCancellationToken.Create(pdcts.Source, parent = pdcts)
  -                return dcts :> ICloudCancellationTokenSource        
  -            | _ -> return invalidArg "parent" "invalid cancellation token type."
  -        }+    /// <param name="globalTokenFactory"></param>
  +    static member CreateFactory(globalTokenFactory : IGlobalCancellationTokenFactory<'Token>) = 
  +        new DistributedCancellationTokenFactory<'Token>(globalTokenFactory)
positive.code: open MBrace.Store.Internals
  open MBrace.Runtime.Utils
  
  type ICancellationTokenSourceFactory =
      abstract CreateCancellationTokenSource : unit -> Async<ICloudCancellationTokenSource>
      abstract CreateLinkedCancellationTokenSource : parent:ICloudCancellationToken -> Async<ICloudCancellationTokenSource>
  
  type internal DistributedCancellationTokenInfo =
      {
          IsCancellationRequested : bool
          Children : string list
      }
  
  [<AutoOpen>]
  module private DistributedCancellationTokenSourceImpl =
  
      open MBraceAsyncExtensions
  
      /// check cancellation 
      let checkCancellation (source : ICloudDictionary<DistributedCancellationTokenInfo>) (tokenId : string) = async {
          let! contents = source.TryFind tokenId
          return
              match contents with
              | None -> true
              | Some c -> c.IsCancellationRequested
      }
  
      /// cancels a distributed token entity and all its children
      let cancelTokenEntity (source : ICloudDictionary<DistributedCancellationTokenInfo>) (id : string) = async {
          let visited = new ConcurrentDictionary<string, unit> ()
          let rec cancel id = async {
              if visited.TryAdd(id, ()) then
                  let! isCancelled = checkCancellation source id
                  if isCancelled then
                      let! info = source.Update(id, fun info -> { info with IsCancellationRequested = true })
                      do! info.Children |> Seq.map cancel |> Async.Parallel |> Async.Ignore
          }
  
          do! cancel id
      }
  
      /// creates a new cancellation token id with provided parent id
      let tryCreateToken (source : ICloudDictionary<DistributedCancellationTokenInfo>) (parentId : string option) = async {
          let createEntry() = async {
              let id = mkUUID()
              do! source.Add(id, { IsCancellationRequested = false ; Children = [] })
              return id
          }
  
          match parentId with
          | None ->
              let! id = createEntry()
              return Some id
  
          | Some parentId ->
              let! parent = source.TryFind parentId
              match parent with
              | Some p when not p.IsCancellationRequested ->
                  let! id = createEntry()
  
                  let! parentInfo = source.Update(parentId, fun info -> { info with Children = id :: info.Children })
  
                  // parent has been cancelled in the meantime, do a cancel of current token
                  if parentInfo.IsCancellationRequested then do! cancelTokenEntity source id
  
                  return Some id
              | _ -> return None
      }
  
      let localTokens = new System.Collections.Concurrent.ConcurrentDictionary<string, CancellationToken> ()
      /// creates a cancellation token that is updated by polling the token entities
      let createLocalCancellationToken id checkCanceled =
          let ok, t = localTokens.TryGetValue id
          if ok then t
          elif Async.RunSync checkCanceled then
              new CancellationToken(canceled = true)
          else
              let createToken _ =
                  let cts = new System.Threading.CancellationTokenSource()
  
                  let rec checkCancellation () = async {
                      let! isCancelled = Async.Catch checkCanceled
                      match isCancelled with
                      | Choice1Of2 true -> 
                          cts.Cancel()
                          localTokens.TryRemove id |> ignore
                      | Choice1Of2 false ->
                          do! Async.Sleep 200
                          return! checkCancellation ()
  
negative.code:     member __.IsCancellationRequested = let t = getLocalToken() in t.IsCancellationRequested
      /// Cancel cancellation token
      member __.Cancel() =
          if __.IsCancellationRequested then ()
          else
              lock state (fun () ->
                  match state with
                  | Canceled _ -> ()
                  | Localized _ -> localCancellationTokenSource.Cancel()
                  | Distributed token -> token.Cancel() |> Async.RunSync)
  
      member internal __.Source = source
  
  