Skip to content

Commit

Permalink
fix grant
Browse files Browse the repository at this point in the history
  • Loading branch information
rmgk committed Jan 25, 2024
1 parent 84521bb commit 0d2dea3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ case class TokenAgreement(
)
object TokenAgreement {

val unchanged: Dotted[TokenAgreement] = Dotted(TokenAgreement(Token.unchanged, ReplicatedSet.empty))
val unchanged: TokenAgreement = TokenAgreement(Token.unchanged, ReplicatedSet.empty)

extension [C, E](container: C)
def tokens: syntax[C] = syntax(container)
Expand All @@ -42,16 +42,17 @@ object TokenAgreement {

def release(using ReplicaId): CausalMutate = updateWant(_.remove(replicaId))

def grant(using ReplicaId): CausalMutate = mutate:
// We finde the “largest” ID that wants the token.
// This is incredibly “unfair” but does prevent deadlocks in case someone needs multiple tokens.
current.wants.elements.maxOption match
case Some(head) if head != replicaId =>
val removeWant: Dotted[ReplicatedSet[Uid]] = current.wants.inheritContext.remove(head)
removeWant.map: rw =>
TokenAgreement(Token(current.token.epoche + 1, head), rw)
case _ => unchanged

def grant(using ReplicaId): Mutate = mutate:
if !isOwner then unchanged
else
// We find the “largest” ID that wants the token.
// This is incredibly “unfair” but does prevent deadlocks in case someone needs multiple tokens.
current.wants.elements.maxOption match
case Some(head) if head != replicaId =>
TokenAgreement(Token(current.token.epoche + 1, head), ReplicatedSet.empty)
case _ => unchanged

def isOwner(using ReplicaId, PermQuery): Boolean = replicaId == current.token.owner
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ class OpsSyntaxHelper[C, L](container: C) extends OpsTypes[C, L] {
extension [A](a: A) def inheritContext(using PermCausalMutate): Dotted[A] = Dotted(a, context)

def mutate(l: Dotted[L])(using perm: PermCausalMutate): C = l.mutator
def mutate(l: L)(using perm: PermMutate): C = l.mutator

}

0 comments on commit 0d2dea3

Please sign in to comment.