Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

16
Task/Mutex/E/mutex-1.e Normal file
View file

@ -0,0 +1,16 @@
def makeMutex() {
# The mutex is available (released) if available is resolved, otherwise it
# has been seized/locked. The specific value of available is irrelevant.
var available := null
# The interface to the mutex is a function, taking a function (action)
# to be executed.
def mutex(action) {
# By assigning available to our promise here, the mutex remains
# unavailable to the /next/ caller until /this/ action has gotten
# its turn /and/ resolved its returned value.
available := Ref.whenResolved(available, fn _ { action <- () })
}
return mutex
}

39
Task/Mutex/E/mutex-2.e Normal file
View file

@ -0,0 +1,39 @@
Creating the mutex:
? def mutex := makeMutex()
# value: <mutex>
Creating the shared resource:
? var value := 0
# value: 0
Manipulating the shared resource non-atomically so as to show a problem:
? for _ in 0..1 {
> when (def v := (&value) <- get()) -> {
> (&value) <- put(v + 1)
> }
> }
? value
# value: 1
The value has been incremented twice, but non-atomically, and so is 1 rather
than the intended 2.
? value := 0
# value: 0
This time, we use the mutex to protect the action.
? for _ in 0..1 {
> mutex(fn {
> when (def v := (&value) <- get()) -> {
> (&value) <- put(v + 1)
> }
> })
> }
? value
# value: 2