/* ----------------------------------------------------------------------------- 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 "simple Thread test" { success } unittest test { CountLoop counter <- CountLoop.create(10000) \ Testing.checkEquals(counter.get(),0) \ counter.start() \ Testing.checkEquals(counter.get(),10000) \ counter.start() \ Testing.checkEquals(counter.get(),20000) } concrete CountLoop { refines Process @type create (Int) -> (CountLoop) @value get () -> (Int) } define CountLoop { refines Routine @value Int current @value Int count @value weak Thread thread create (c) { return CountLoop{ 0, c, empty } } start () { // Starting it right away causes the thread function to take a reference to // thread. This means that once it exits, thread will become empty. thread <- ProcessThread.from(self).start() return self } run () { scoped { Int i <- 0 } in while (i < count) { current <- current+1 } update { i <- i+1 } } get () { optional Thread thread2 <- strong(thread) if (present(thread2)) { if (!require(thread2).isRunning()) { fail("thread has not been started yet") } else { \ require(thread2).join() } } return current } } testcase "ProcessThread 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() } } } } } testcase "Argv available in Thread" { success args "arg1" } unittest test { \ ProcessThread.from(CheckArgv.create()).start().join() } concrete CheckArgv { refines Routine @type create () -> (CheckArgv) } define CheckArgv { create () { return CheckArgv{ } } run () { \ Testing.checkEquals(Argv.global().readAt(1),"arg1") } } testcase "join() crashes if Thread not started yet" { crash require "thread.*started" } unittest test { \ ProcessThread.from(NoOpRoutine.create()).join() } testcase "join() once" { success } unittest test { \ ProcessThread.from(NoOpRoutine.create()).start().join() } testcase "join() twice crashes" { crash require "thread.*started" } unittest test { \ ProcessThread.from(NoOpRoutine.create()).start().join().join() } testcase "detach() crashes if Thread not started yet" { crash require "thread.*started" } unittest test { \ ProcessThread.from(InfiniteRoutine.create()).detach() } testcase "detach() once" { success } unittest test { \ ProcessThread.from(InfiniteRoutine.create()).start().detach() } testcase "detach() twice crashes" { crash require "thread.*started" } unittest test { \ ProcessThread.from(InfiniteRoutine.create()).start().detach().detach() } testcase "weak Thread frees on exit" { success } unittest startAndJoin { weak Thread thread <- ProcessThread.from(NoOpRoutine.create()).start().join() if (present(strong(thread))) { fail("thread is still present") } } unittest notStarted { weak Thread thread <- ProcessThread.from(NoOpRoutine.create()) if (present(strong(thread))) { fail("thread is still present") } }