Skip to content

Commit

Permalink
Tidy up source files and export to soundness
Browse files Browse the repository at this point in the history
  • Loading branch information
propensive committed Jun 11, 2024
1 parent f6e5dc0 commit 1d33aa2
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 51 deletions.
37 changes: 37 additions & 0 deletions src/core/feudalism.Immutable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Feudalism, version [unreleased]. Copyright 2024 Jon Pretty, Propensive OÜ.
The primary distribution site is: https://propensive.com/
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.
*/

package feudalism

import scala.reflect.*

import language.experimental.captureChecking

trait Immutable[MutableType, ImmutableType]:
def snapshot(ref: ImmutableType): ImmutableType = ref
def make(ref: MutableType): ImmutableType

object Immutable:
given [ElementType: ClassTag] => Immutable[Array[ElementType], IArray[ElementType]] as array:
def make(value: Array[ElementType]): IArray[ElementType] = value.asInstanceOf[IArray[ElementType]]

override def snapshot(value: IArray[ElementType]): IArray[ElementType] =
val array = new Array[ElementType](value.length)
System.arraycopy(value, 0, array, 0, value.length)
array.asInstanceOf[IArray[ElementType]]

given Immutable[StringBuilder, String] as stringBuilder = _.toString
given [ValueType](using DummyImplicit) => Immutable[ValueType, ValueType] as any = identity(_)
56 changes: 5 additions & 51 deletions src/core/feudalism.scala → src/core/feudalism.Mutex.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,8 @@

package feudalism

import scala.reflect.*

import language.experimental.captureChecking

class MutexRef[ValueType](value: ValueType, makeSnapshot: ValueType => ValueType):
def apply(): ValueType = value
def snapshot(): ValueType = makeSnapshot(value)

class Mutex[ValueType](initial: ValueType):
private var count: Int = 0
private var value: ValueType = initial
Expand All @@ -45,11 +39,11 @@ class Mutex[ValueType](initial: ValueType):
synchronized:
while count == -1 do wait()
count += 1

try lambda(MutexRef(immutable.make(value), immutable.snapshot(_))) finally synchronized:
count -= 1
notify()

def isolate[ResultType](lambda: ValueType => ResultType): ResultType =
synchronized:
while count != 0 do wait()
Expand All @@ -58,12 +52,12 @@ class Mutex[ValueType](initial: ValueType):
try lambda(value) finally synchronized:
count = 0
notify()

def replace(lambda: ValueType => ValueType): ValueType =
synchronized:
while count != 0 do wait()
count = -1

try
value = lambda(value)
value
Expand All @@ -76,47 +70,7 @@ class Mutex[ValueType](initial: ValueType):
synchronized:
while count != 0 do wait()
count = -1

try value = value2 finally synchronized:
count = 0
notify()

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

def access[ResultType](evaluate: => ResultType): ResultType =
synchronized:
while count == -1 do wait()
count += 1

try evaluate finally synchronized:
count -= 1
notify()

def isolate[ResultType](evaluate: => ResultType): ResultType =
synchronized:
while count != 0 do wait()
count = -1

try evaluate finally synchronized:
try value = value2 finally synchronized:
count = 0
notify()

trait Immutable[MutableType, ImmutableType]:
def snapshot(ref: ImmutableType): ImmutableType = ref
def make(ref: MutableType): ImmutableType

object Immutable:
given array[ElementType: ClassTag]: Immutable[Array[ElementType], IArray[ElementType]] with
def make(value: Array[ElementType]): IArray[ElementType] = value.asInstanceOf[IArray[ElementType]]

override def snapshot(value: IArray[ElementType]): IArray[ElementType] =
val array = new Array[ElementType](value.length)
System.arraycopy(value, 0, array, 0, value.length)
array.asInstanceOf[IArray[ElementType]]

given stringBuilder: Immutable[StringBuilder, String] with
def make(value: StringBuilder): String = value.toString

given any[ValueType](using DummyImplicit): Immutable[ValueType, ValueType] with
def make(value: ValueType): ValueType = value
23 changes: 23 additions & 0 deletions src/core/feudalism.MutexRef.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
Feudalism, version [unreleased]. Copyright 2024 Jon Pretty, Propensive OÜ.
The primary distribution site is: https://propensive.com/
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.
*/

package feudalism

import language.experimental.captureChecking

class MutexRef[ValueType](value: ValueType, makeSnapshot: ValueType => ValueType):
def apply(): ValueType = value
def snapshot(): ValueType = makeSnapshot(value)
40 changes: 40 additions & 0 deletions src/core/feudalism.Semaphore.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Feudalism, version [unreleased]. Copyright 2024 Jon Pretty, Propensive OÜ.
The primary distribution site is: https://propensive.com/
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.
*/

package feudalism

import language.experimental.captureChecking

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

def access[ResultType](evaluate: => ResultType): ResultType =
synchronized:
while count == -1 do wait()
count += 1

try evaluate finally synchronized:
count -= 1
notify()

def isolate[ResultType](evaluate: => ResultType): ResultType =
synchronized:
while count != 0 do wait()
count = -1

try evaluate finally synchronized:
count = 0
notify()
3 changes: 3 additions & 0 deletions src/core/soundness.feudalism-core.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package soundness

export feudalism.{Immutable, Mutex, MutexRef, Semaphore}

0 comments on commit 1d33aa2

Please sign in to comment.