packages feed

zeolite-lang-0.16.0.0: lib/thread/mutex.0rt

/* -----------------------------------------------------------------------------
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]

testcase "MutexLock crashes if not freed" {
  crash
  require "not freed"
}

unittest test {
  Mutex mutex <- SimpleMutex.new()
  \ MutexLock.lock(mutex)
}


testcase "MutexLock crashes if freed twice" {
  crash
  require "freed multiple times"
}

unittest test {
  Mutex mutex <- SimpleMutex.new()
  MutexLock lock <- MutexLock.lock(mutex)
  \ lock.freeResource()
  \ lock.freeResource()
}


testcase "mutex integration test" {
  success
}

unittest test {
  Mutex mutex <- SimpleMutex.new()
  Value value <- Value.create()
  Thread evenThread <- ProcessThread.from(EvenOrOdd.create(mutex,true, 10,value)).start()
  Thread oddThread  <- ProcessThread.from(EvenOrOdd.create(mutex,false,10,value)).start()
  \ evenThread.join()
  \ oddThread.join()
  \ Testing.checkEquals<?>(value.get(),20)
}

concrete EvenOrOdd {
  refines Routine

  // Args:
  // - Mutex: Locks the Value.
  // - Bool:  Increment only on even if true, or on odd if false.
  // - Int:   Number of increment operations.
  // - Value: Value to increment.
  @type create (Mutex,Bool,Int,Value) -> (Routine)
}

define EvenOrOdd {
  @value Mutex mutex
  @value Bool even
  @value Int count
  @value Value value

  create (m,e,c,v) {
    return EvenOrOdd{ m, e, c, v }
  }

  run () {
    scoped {
      Int i <- 0
    } in while (i < count) {
      $Hidden[count]$
      scoped {
        MutexLock lock <- MutexLock.lock(mutex)
        $Hidden[mutex]$
        if (value.getInUse()) {
          fail("value in use")
        } else {
          \ value.setInUse(true)
        }
      } cleanup {
        \ value.setInUse(false)
        \ lock.freeResource()
      } in {
        $Hidden[lock]$
        if ((value.get()%2 == 0) ^ !even) {
          i <- i+1
          \ value.increment()
        }
      }
    }
  }
}