Skip to content

Commit

Permalink
Change read to apply() and add update methods
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Apr 2, 2024
1 parent f85a8e0 commit fed35de
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/core/feudalism.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,20 @@ class Mutex[ValueType](initial: ValueType):
private var count: Int = 0
private var value: ValueType = initial

def read[ResultType, ImmutableType]
def apply(): ValueType =
synchronized:
while count == -1 do wait()
count += 1

val result = value

synchronized:
count -= 1
notify()

value

def use[ResultType, ImmutableType]
(using immutable: Immutable[ValueType, ImmutableType])
(lambda: (ref: MutexRef[ImmutableType]) => ResultType)
: ResultType =
Expand Down Expand Up @@ -70,6 +83,17 @@ class Mutex[ValueType](initial: ValueType):

result

def update(value2: => ValueType): Unit =
synchronized:
while count != 0 do wait()
count = -1

value = value2

synchronized:
count = 0
notify()

class Semaphore():
private var count: Int = 0

Expand Down

0 comments on commit fed35de

Please sign in to comment.