Skip to content

Commit

Permalink
rewrite with option
Browse files Browse the repository at this point in the history
  • Loading branch information
DedSec256 committed Apr 25, 2024
1 parent 292b246 commit 6360481
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions src/Compiler/Utilities/illib.fs
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,32 @@ type InterruptibleLazy<'T> private (value, valueFactory: unit -> 'T) =
let syncObj = obj ()

[<VolatileField>]
let mutable valueFactory = valueFactory
let mutable lazyValue = value

let mutable value = value
new(valueFactory: unit -> 'T) = InterruptibleLazy(ValueNone, valueFactory)

new(valueFactory: unit -> 'T) = InterruptibleLazy(Unchecked.defaultof<_>, valueFactory)

member this.IsValueCreated =
match box valueFactory with
| null -> true
| _ -> false
member this.IsValueCreated = lazyValue.IsSome

member this.Value =
match box valueFactory with
| null -> value
match lazyValue with
| ValueSome value -> value
| _ ->
Monitor.Enter(syncObj)

try
match box valueFactory with
| null -> ()
match lazyValue with
| ValueSome value -> value
| _ ->

value <- valueFactory ()
valueFactory <- Unchecked.defaultof<_>
let value = valueFactory ()
lazyValue <- ValueSome(value)
value
finally
Monitor.Exit(syncObj)

value

member this.Force() = this.Value

static member FromValue(value) =
InterruptibleLazy(value, Unchecked.defaultof<_>)
InterruptibleLazy(ValueSome(value), Unchecked.defaultof<_>)

module InterruptibleLazy =
let force (x: InterruptibleLazy<'T>) = x.Value
Expand Down

0 comments on commit 6360481

Please sign in to comment.