packages feed

zeolite-lang-0.19.0.0: tests/simulate-refs/shared.0rx

/* -----------------------------------------------------------------------------
Copyright 2021 Kevin P. Barry

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
----------------------------------------------------------------------------- */

// Author: Kevin P. Barry [ta0kira@gmail.com]

define SharedRoutine {
  new (name,state) {
    if (state.addStrong() == 1) {
      \ state.addWeak()
    }
    return DropSharedEnter.new(name,state)
  }

  alreadyStrong (name,state) {
    return DropSharedEnter.new(name,state)
  }
}


concrete DropSharedEnter {
  @type new (String,ReferenceState) -> (StateMachine)
}

define DropSharedEnter {
  $ReadOnly[name,state]$

  refines StateMachine

  @value String name
  @value ReferenceState state

  new (name,state) {
    return DropSharedEnter{ name, state }
  }

  transition () {
    \ state.addOperation<#self>(name)
    if (state.remStrong() == 0) {
      return DropSharedFreeObject.new(name,state)
    } else {
      return empty
    }
  }
}


concrete DropSharedFreeObject {
  @type new (String,ReferenceState) -> (StateMachine)
}

define DropSharedFreeObject {
  $ReadOnly[name,state]$

  refines StateMachine

  @value String name
  @value ReferenceState state

  new (name,state) {
    return DropSharedFreeObject{ name, state }
  }

  transition () {
    \ state.addOperation<#self>(name)
    \ state.kill()
    return DropSharedCheckWeak.new(name,state)
  }
}


concrete DropSharedCheckWeak {
  @type new (String,ReferenceState) -> (StateMachine)
}

define DropSharedCheckWeak {
  $ReadOnly[name,state]$

  refines StateMachine

  @value String name
  @value ReferenceState state

  new (name,state) {
    return DropSharedCheckWeak{ name, state }
  }

  transition () {
    \ state.addOperation<#self>(name)
    if (state.remWeak() == 0) {
      return DropSharedFreeWeak.new(name,state)
    } else {
      return empty
    }
  }
}


concrete DropSharedFreeWeak {
  @type new (String,ReferenceState) -> (StateMachine)
}

define DropSharedFreeWeak {
  $ReadOnly[name,state]$

  refines StateMachine

  @value String name
  @value ReferenceState state

  new (name,state) {
    return DropSharedFreeWeak{ name, state }
  }

  transition () {
    \ state.addOperation<#self>(name)
    \ state.cleanupWeak()
    return empty
  }
}