Compare commits
No commits in common. "master" and "v1.2" have entirely different histories.
@ -15,7 +15,7 @@ buildscript {
|
||||
}
|
||||
|
||||
group = "ru.landgrafhomyak.utility"
|
||||
version = "1.4"
|
||||
version = "1.2"
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
@ -82,7 +82,6 @@ xomrk {
|
||||
|
||||
jvmTest {
|
||||
dependencies {
|
||||
implementation(kotlinStdlibDependency)
|
||||
implementation("org.testng:testng:7.5.1")
|
||||
}
|
||||
}
|
||||
|
||||
@ -25,15 +25,10 @@ public sealed class ChildCloseableState<T : CloseableState> : CloseableState {
|
||||
override fun startUsage(): Unit =
|
||||
this._state.startUsage()
|
||||
|
||||
@ManualStateManipulation
|
||||
override fun startUsageIfNotClosed(): Boolean =
|
||||
this._state.startUsageIfNotClosed()
|
||||
|
||||
@ManualStateManipulation
|
||||
override fun finishUsage(): Unit =
|
||||
this._state.finishUsage()
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
override fun close() {
|
||||
this._state.finishUsage()
|
||||
|
||||
@ -8,17 +8,9 @@ public interface CloseableState {
|
||||
@ManualStateManipulation
|
||||
public fun startUsage()
|
||||
|
||||
/**
|
||||
* Tries to start usage and returns `false` if entered or `true` if closed.
|
||||
* Doesn't prevent from throwing error if this state forbids concurrent access.
|
||||
*/
|
||||
@ManualStateManipulation
|
||||
public fun startUsageIfNotClosed(): Boolean
|
||||
|
||||
@ManualStateManipulation
|
||||
public fun finishUsage()
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
public fun close()
|
||||
|
||||
|
||||
@ -13,16 +13,12 @@ public expect class CloseableStateCloseableWrapper : CloseableState.ExternallySy
|
||||
@ManualStateManipulation
|
||||
override fun startUsage()
|
||||
|
||||
@ManualStateManipulation
|
||||
override fun startUsageIfNotClosed(): Boolean
|
||||
|
||||
@ManualStateManipulation
|
||||
override fun finishUsage()
|
||||
|
||||
@ManualStateManipulation
|
||||
override fun finishUsage(close: Boolean)
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
override fun close()
|
||||
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
package ru.landgrafhomyak.utility.closeable_state_1
|
||||
|
||||
/**
|
||||
* Utility annotation to simplify searching method that closes/destructs/deallocs object.
|
||||
*/
|
||||
@Target(AnnotationTarget.FUNCTION)
|
||||
@Retention(AnnotationRetention.SOURCE)
|
||||
public annotation class Destructor()
|
||||
@ -18,16 +18,12 @@ public expect open class ErrorOnConcurrentAccessState : CloseableState.Externall
|
||||
@ManualStateManipulation
|
||||
public final override fun startUsage()
|
||||
|
||||
@ManualStateManipulation
|
||||
public final override fun startUsageIfNotClosed(): Boolean
|
||||
|
||||
@ManualStateManipulation
|
||||
public final override fun finishUsage()
|
||||
|
||||
@ManualStateManipulation
|
||||
public final override fun finishUsage(close: Boolean)
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
public final override fun close()
|
||||
|
||||
|
||||
@ -16,7 +16,6 @@ public class HandleWrapper<H>(
|
||||
return this.state.withUse { method(this.handle) }
|
||||
}
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
public fun close() {
|
||||
this.state.close()
|
||||
|
||||
@ -19,13 +19,9 @@ public expect open class UsagesCounter : CloseableState.AllowsConcurrency {
|
||||
@ManualStateManipulation
|
||||
public final override fun startUsage()
|
||||
|
||||
@ManualStateManipulation
|
||||
public final override fun startUsageIfNotClosed(): Boolean
|
||||
|
||||
@ManualStateManipulation
|
||||
public final override fun finishUsage()
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
public final override fun close()
|
||||
|
||||
|
||||
@ -91,27 +91,3 @@ public inline fun CloseableState.ExternallySynchronized.tryFinishUsageThenClose(
|
||||
onSuccess = { this.finishUsage(needClose) }
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@OptIn(ManualStateManipulation::class)
|
||||
@JvmName("withUseIfNotClosed\$kt")
|
||||
public inline fun <R> CloseableState.withUseIfNotClosed(ifClosed: R, block: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
|
||||
}
|
||||
if (this.startUsageIfNotClosed())
|
||||
return ifClosed
|
||||
return safeAutoClose1(action = block, finally = { this.finishUsage() })
|
||||
}
|
||||
|
||||
@OptIn(ManualStateManipulation::class)
|
||||
@JvmName("withUseIfNotClosed\$kt")
|
||||
public inline fun <R> CloseableState.withUseIfNotClosed(ifClosed: () -> R, block: () -> R): R {
|
||||
contract {
|
||||
callsInPlace(ifClosed, InvocationKind.AT_MOST_ONCE)
|
||||
callsInPlace(block, InvocationKind.AT_MOST_ONCE)
|
||||
}
|
||||
if (this.startUsageIfNotClosed())
|
||||
return ifClosed()
|
||||
return safeAutoClose1(action = block, finally = { this.finishUsage() })
|
||||
}
|
||||
@ -1,110 +0,0 @@
|
||||
package ru.landgrafhomyak.utility.closeable_state_1;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
public /* open */ class SpinLockSynchronizedState
|
||||
extends SpinLockSynchronizedState$Errors
|
||||
implements CloseableState.ExternallySynchronized {
|
||||
|
||||
private enum State {
|
||||
OPEN, IN_USE, CLOSED
|
||||
}
|
||||
|
||||
private final AtomicReference<State> _currentState;
|
||||
|
||||
public SpinLockSynchronizedState() {
|
||||
this._currentState = new AtomicReference<>(State.OPEN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isInUse() {
|
||||
return this._currentState.get() == State.IN_USE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final boolean isClosed() {
|
||||
return this._currentState.get() == State.CLOSED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void assertNotClosed() {
|
||||
if (this.isClosed())
|
||||
this.throwClosed();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void startUsage() {
|
||||
if (this.startUsageIfNotClosed())
|
||||
this.throwClosed();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final boolean startUsageIfNotClosed() {
|
||||
while (true) {
|
||||
switch (this._currentState.compareAndExchange(State.OPEN, State.IN_USE)) {
|
||||
case IN_USE:
|
||||
Thread.onSpinWait();
|
||||
continue;
|
||||
case CLOSED:
|
||||
return true;
|
||||
case OPEN:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void _finishUsage(State nextState) {
|
||||
switch (this._currentState.compareAndExchange(State.IN_USE, nextState)) {
|
||||
case OPEN:
|
||||
throw new IllegalStateException("Can't finish usage because it isn't started");
|
||||
case CLOSED:
|
||||
this.throwClosed();
|
||||
case IN_USE:
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void finishUsage() {
|
||||
this._finishUsage(State.OPEN);
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public void finishUsage(boolean close) {
|
||||
this._finishUsage(close ? State.CLOSED : State.OPEN);
|
||||
}
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void close() {
|
||||
while (true) {
|
||||
switch (this._currentState.compareAndExchange(State.OPEN, State.CLOSED)) {
|
||||
case CLOSED:
|
||||
this.throwClosed();
|
||||
case IN_USE:
|
||||
Thread.onSpinWait();
|
||||
continue;
|
||||
case OPEN:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
switch (this._currentState.get()) {
|
||||
case CLOSED:
|
||||
return "<closeable spinlock-synchronized state [free]>";
|
||||
case IN_USE:
|
||||
return "<closeable spinlock-synchronized state [in use]>";
|
||||
case OPEN:
|
||||
return "<closeable spinlock-synchronized state [closed]>";
|
||||
}
|
||||
throw new RuntimeException("Unreachable");
|
||||
}
|
||||
}
|
||||
@ -31,7 +31,6 @@ public final class jCloseableStateCloseableWrapper
|
||||
this._self.assertNotClosed();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public void startUsage() {
|
||||
this._self.startUsage();
|
||||
@ -47,43 +46,18 @@ public final class jCloseableStateCloseableWrapper
|
||||
}
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public boolean startUsageIfNotClosed() {
|
||||
if (this._self.startUsageIfNotClosed())
|
||||
return true;
|
||||
try {
|
||||
if (this._parent.startUsageIfNotClosed()) {
|
||||
this._self.finishUsage();
|
||||
return true;
|
||||
}
|
||||
} catch (Throwable e1) {
|
||||
try {
|
||||
this._self.finishUsage();
|
||||
} catch (Throwable e2) {
|
||||
e1.addSuppressed(e2);
|
||||
}
|
||||
throw e1;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public void finishUsage() {
|
||||
this._parent.finishUsage();
|
||||
this._self.finishUsage();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public void finishUsage(boolean close) {
|
||||
this._parent.finishUsage();
|
||||
this._self.finishUsage(close);
|
||||
}
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public void close() {
|
||||
this._self.close();
|
||||
@ -93,5 +67,4 @@ public final class jCloseableStateCloseableWrapper
|
||||
public String toString() {
|
||||
return "<closeable state " + this._parent + " wrapped with " + this._self + ">";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -32,25 +32,16 @@ public /* open */ class jErrorOnConcurrentAccessState
|
||||
this.throwClosed();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void startUsage() {
|
||||
if (this.startUsageIfNotClosed())
|
||||
this.throwClosed();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final boolean startUsageIfNotClosed() {
|
||||
switch (this._currentState.compareAndExchange(State.OPEN, State.IN_USE)) {
|
||||
case IN_USE:
|
||||
this.throwConcurrent();
|
||||
case CLOSED:
|
||||
return true;
|
||||
this.throwClosed();
|
||||
case OPEN:
|
||||
return false;
|
||||
break;
|
||||
}
|
||||
throw new RuntimeException("Unreachable");
|
||||
}
|
||||
|
||||
private void _finishUsage(State nextState) {
|
||||
@ -64,20 +55,16 @@ public /* open */ class jErrorOnConcurrentAccessState
|
||||
}
|
||||
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void finishUsage() {
|
||||
this._finishUsage(State.OPEN);
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public void finishUsage(boolean close) {
|
||||
this._finishUsage(close ? State.CLOSED : State.OPEN);
|
||||
}
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void close() {
|
||||
switch (this._currentState.compareAndExchange(State.OPEN, State.CLOSED)) {
|
||||
|
||||
@ -22,28 +22,20 @@ public /* open */ class jUsagesCounter
|
||||
this.throwClosed();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void startUsage() {
|
||||
if (this.startUsageIfNotClosed())
|
||||
this.throwClosed();
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final boolean startUsageIfNotClosed() {
|
||||
while (true) {
|
||||
final long cur = this._currentUsagesCount.get();
|
||||
if (cur < 0) {
|
||||
return true;
|
||||
this.throwClosed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._currentUsagesCount.compareAndSet(cur, cur + 1))
|
||||
return false;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void finishUsage() {
|
||||
while (true) {
|
||||
@ -58,8 +50,6 @@ public /* open */ class jUsagesCounter
|
||||
}
|
||||
}
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
@Override
|
||||
public final void close() {
|
||||
long currentReferencesCount;
|
||||
|
||||
@ -1,33 +0,0 @@
|
||||
@file:OptIn(ExperimentalContracts::class)
|
||||
|
||||
package ru.landgrafhomyak.utility.closeable_state_1
|
||||
|
||||
import kotlin.contracts.ExperimentalContracts
|
||||
|
||||
public class OwnedSpinLockSynchronizedState : SpinLockSynchronizedState {
|
||||
private var _owner: Any?
|
||||
|
||||
public constructor() : super() {
|
||||
this._owner = null
|
||||
}
|
||||
|
||||
public constructor(owner: Any) : super() {
|
||||
this._owner = owner
|
||||
}
|
||||
|
||||
public var owner: Any
|
||||
get() = this._owner ?: throw IllegalStateException("Owner not set yet")
|
||||
set(value) {
|
||||
if (this._owner != null) throw IllegalStateException("Owner already initialized")
|
||||
this._owner = value
|
||||
}
|
||||
|
||||
override fun throwClosed(): Nothing {
|
||||
throw IllegalStateException("Object is closed: ${this._owner}")
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
val base = super.toString()
|
||||
return base.substring(0, base.length - 1) + " of ${this._owner}>"
|
||||
}
|
||||
}
|
||||
@ -1,7 +0,0 @@
|
||||
package ru.landgrafhomyak.utility.closeable_state_1
|
||||
|
||||
internal abstract class `SpinLockSynchronizedState$Errors` : CloseableState.ExternallySynchronized {
|
||||
protected open fun throwClosed(): Nothing {
|
||||
throw IllegalStateException("Object is closed")
|
||||
}
|
||||
}
|
||||
@ -36,26 +36,6 @@ public actual class CloseableStateCloseableWrapper : CloseableState.ExternallySy
|
||||
}
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
actual override fun startUsageIfNotClosed(): Boolean {
|
||||
if (this._self.startUsageIfNotClosed())
|
||||
return true
|
||||
try {
|
||||
if (this._parent.startUsageIfNotClosed()) {
|
||||
this._self.finishUsage()
|
||||
return true
|
||||
}
|
||||
} catch (e1: Throwable) {
|
||||
try {
|
||||
this._self.finishUsage()
|
||||
} catch (e2: Throwable) {
|
||||
e1.addSuppressed(e2)
|
||||
}
|
||||
throw e1
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
actual override fun finishUsage() {
|
||||
this._parent.finishUsage()
|
||||
@ -68,7 +48,6 @@ public actual class CloseableStateCloseableWrapper : CloseableState.ExternallySy
|
||||
this._self.finishUsage(close)
|
||||
}
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
actual override fun close() {
|
||||
this._self.close()
|
||||
|
||||
@ -35,16 +35,10 @@ public actual open class ErrorOnConcurrentAccessState : CloseableState.Externall
|
||||
|
||||
@ManualStateManipulation
|
||||
public actual final override fun startUsage() {
|
||||
if (this.startUsageIfNotClosed())
|
||||
this.throwClosed()
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
public actual final override fun startUsageIfNotClosed(): Boolean {
|
||||
when (this._state.compareAndExchange(State.OPEN, State.IN_USE)) {
|
||||
State.OPEN -> return false;
|
||||
State.OPEN -> {}
|
||||
State.IN_USE -> this.throwConcurrent()
|
||||
State.CLOSED -> return true
|
||||
State.CLOSED -> this.throwClosed()
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,7 +59,6 @@ public actual open class ErrorOnConcurrentAccessState : CloseableState.Externall
|
||||
public actual final override fun finishUsage(close: Boolean): Unit =
|
||||
this._finishUsage(if (close) State.CLOSED else State.OPEN)
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
public actual final override fun close() {
|
||||
when (this._state.compareAndExchange(State.OPEN, State.CLOSED)) {
|
||||
|
||||
@ -38,21 +38,11 @@ public actual open class UsagesCounter : CloseableState.AllowsConcurrency {
|
||||
}
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
public actual final override fun startUsageIfNotClosed(): Boolean {
|
||||
this._value.update { o ->
|
||||
if (o < 0) return true
|
||||
return@update o + 1
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@ManualStateManipulation
|
||||
public actual final override fun finishUsage() {
|
||||
this._value.update(Long::dec)
|
||||
}
|
||||
|
||||
@Destructor
|
||||
@ManualStateManipulation
|
||||
public actual final override fun close() {
|
||||
val state = this._value.compareAndExchange(0, _MiscMultiplatform.CLOSED_STATE_VALUE)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user