fix: remove unused TSSynchronized

This commit is contained in:
Akash Yadav
2023-10-22 23:02:15 +05:30
parent f61b150395
commit 5db4f33721
11 changed files with 46 additions and 594 deletions
@@ -1,60 +0,0 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.treesitter.api
import java.util.concurrent.TimeUnit
import java.util.concurrent.locks.ReentrantLock
/**
* @author Akash Yadav
*/
internal class DefaultSynchronized(
// disabled by default
private val isEnabled: Boolean = false
) : TSSynchronized {
private val lock = ReentrantLock()
override fun lock(timeout: Long, unit: TimeUnit): Boolean {
return if (isEnabled) {
require(timeout > 0) { "Timeout must be greater than 0" }
lock.tryLock() || lock.tryLock(timeout, unit)
} else {
false
}
}
override fun unlock() {
if (isEnabled) {
lock.unlock()
}
}
override fun <T> withLock(timeout: Long, unit: TimeUnit, action: () -> T): T {
if (!isEnabled) {
return action()
}
check(lock(timeout, unit)) { "Failed to acquire lock" }
try {
return action()
} finally {
unlock()
}
}
}
@@ -1,54 +0,0 @@
/*
* This file is part of AndroidIDE.
*
* AndroidIDE is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* AndroidIDE is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
*/
package com.itsaky.androidide.treesitter.api
import java.util.concurrent.TimeUnit
/**
* Synchronization for tree sitter objects.
*
* @author Akash Yadav
*/
interface TSSynchronized {
/**
* Acquires the lock. Throws an [IllegalStateException] if the lock can not be acquired within
* the given [timeout].
*
* @param timeout The timeout duration.
* @param unit The time unit for the timeout duration.
* @return Whether the lock was held or not.
*/
fun lock(timeout: Long = 3, unit: TimeUnit = TimeUnit.SECONDS): Boolean
/**
* Releases the lock.
*/
fun unlock()
/**
* Execute the given [action] with the lock. The lock will be held before executing the action
* and finally released. Throws an [IllegalStateException] if the lock can not be acquired within
* * the given [timeout].
*
* @param timeout The timeout duration.
* @param unit The time unit for the timeout duration.
* @param action The action to perform.
*/
fun <T : Any?> withLock(timeout: Long = 3, unit: TimeUnit = TimeUnit.SECONDS, action: () -> T): T
}
@@ -30,7 +30,7 @@ class TreeSitterInputEdit @JvmOverloads internal constructor(
oldEndPoint: TSPoint? = null,
newEndPoint: TSPoint? = null
) : TSInputEdit(startByte, oldEndByte, newEndByte, startPoint, oldEndPoint, newEndPoint),
RecyclableObjectPool.Recyclable by DefaultRecyclable(), TSSynchronized by DefaultSynchronized() {
RecyclableObjectPool.Recyclable by DefaultRecyclable() {
companion object {
@@ -57,15 +57,13 @@ class TreeSitterInputEdit @JvmOverloads internal constructor(
}
override fun recycle() {
withLock {
this.startByte = 0
this.oldEndByte = 0
this.newEndByte = 0
this.startPoint = null
this.oldEndPoint = null
this.newEndPoint = null
this.startByte = 0
this.oldEndByte = 0
this.newEndByte = 0
this.startPoint = null
this.oldEndPoint = null
this.newEndPoint = null
returnToPool()
}
returnToPool()
}
}
@@ -17,7 +17,6 @@
package com.itsaky.androidide.treesitter.api
import com.itsaky.androidide.treesitter.TSLanguage
import com.itsaky.androidide.treesitter.TSLookaheadIterator
import com.itsaky.androidide.utils.DefaultRecyclable
import com.itsaky.androidide.utils.RecyclableObjectPool
@@ -26,8 +25,7 @@ import com.itsaky.androidide.utils.RecyclableObjectPool
* @author Akash Yadav
*/
class TreeSitterLookaheadIterator @JvmOverloads internal constructor(pointer: Long = 0) :
TSLookaheadIterator(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable(),
TSSynchronized by DefaultSynchronized() {
TSLookaheadIterator(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable() {
companion object {
@@ -39,47 +37,9 @@ class TreeSitterLookaheadIterator @JvmOverloads internal constructor(pointer: Lo
}
}
override fun next(): Boolean {
return withLock {
super.next()
}
}
override fun getCurrentSymbol(): Short {
return withLock {
super.getCurrentSymbol()
}
}
override fun getCurrentSymbolName(): String {
return withLock {
super.getCurrentSymbolName()
}
}
override fun resetState(stateId: Short): Boolean {
return withLock {
super.resetState(stateId)
}
}
override fun reset(language: TSLanguage?, stateId: Short): Boolean {
return withLock {
super.reset(language, stateId)
}
}
override fun getLanguage(): TSLanguage {
return withLock {
super.getLanguage()
}
}
override fun close() {
withLock {
super.close()
recycle()
}
super.close()
recycle()
}
override fun recycle() {
@@ -18,9 +18,6 @@
package com.itsaky.androidide.treesitter.api
import com.itsaky.androidide.treesitter.TSNode
import com.itsaky.androidide.treesitter.TSPoint
import com.itsaky.androidide.treesitter.TSTree
import com.itsaky.androidide.treesitter.TSTreeCursor
import com.itsaky.androidide.utils.DefaultRecyclable
import com.itsaky.androidide.utils.RecyclableObjectPool
@@ -35,7 +32,7 @@ class TreeSitterNode @JvmOverloads internal constructor(
id: Long = 0,
tree: Long = 0
) : TSNode(context0, context1, context2, context3, id, tree),
RecyclableObjectPool.Recyclable by DefaultRecyclable(), TSSynchronized by DefaultSynchronized() {
RecyclableObjectPool.Recyclable by DefaultRecyclable() {
companion object {
@@ -54,173 +51,14 @@ class TreeSitterNode @JvmOverloads internal constructor(
}
}
override fun getTree(): TSTree {
return withLock { super.getTree() }
}
override fun getChild(index: Int): TSNode {
return withLock { super.getChild(index) }
}
override fun getNamedChild(index: Int): TSNode {
return withLock { super.getNamedChild(index) }
}
override fun getChildByFieldName(fieldName: String?): TSNode {
return withLock { super.getChildByFieldName(fieldName) }
}
override fun walk(): TSTreeCursor {
return withLock { super.walk() }
}
override fun findNodeWithType(type: String?, namedOnly: Boolean): TSNode {
return withLock { super.findNodeWithType(type, namedOnly) }
}
override fun findChildrenWithType(type: String?, reverseSearch: Boolean,
namedOnly: Boolean): MutableList<TSNode> {
return withLock { super.findChildrenWithType(type, reverseSearch, namedOnly) }
}
override fun findChildrenWithTypeReverse(type: String?, namedOnly: Boolean): MutableList<TSNode> {
return withLock { super.findChildrenWithTypeReverse(type, namedOnly) }
}
override fun getParent(): TSNode {
return withLock { super.getParent() }
}
override fun getFieldNameForChild(p0: Int): String {
return withLock { super.getFieldNameForChild(p0) }
}
override fun getChildByFieldId(p0: Int): TSNode {
return withLock { super.getChildByFieldId(p0) }
}
override fun getNextSibling(): TSNode {
return withLock { super.getNextSibling() }
}
override fun getPreviousSibling(): TSNode {
return withLock { super.getPreviousSibling() }
}
override fun getNextNamedSibling(): TSNode {
return withLock { super.getNextNamedSibling() }
}
override fun getPreviousNamedSibling(): TSNode {
return withLock { super.getPreviousNamedSibling() }
}
override fun getFirstChildForByte(p0: Int): TSNode {
return withLock { super.getFirstChildForByte(p0) }
}
override fun getFirstNamedChildForByte(p0: Int): TSNode {
return withLock { super.getFirstNamedChildForByte(p0) }
}
override fun getDescendantForByteRange(p0: Int, p1: Int): TSNode {
return withLock { super.getDescendantForByteRange(p0, p1) }
}
override fun getDescendantForPointRange(p0: TSPoint?, p1: TSPoint?): TSNode {
return withLock { super.getDescendantForPointRange(p0, p1) }
}
override fun getNamedDescendantForByteRange(p0: Int, p1: Int): TSNode {
return withLock { super.getNamedDescendantForByteRange(p0, p1) }
}
override fun getNamedDescendantForPointRange(p0: TSPoint?, p1: TSPoint?): TSNode? {
return withLock { super.getNamedDescendantForPointRange(p0, p1) }
}
override fun isEqualTo(p0: TSNode?): Boolean {
return withLock { super.isEqualTo(p0) }
}
override fun getChildCount(): Int {
return withLock { super.getChildCount() }
}
override fun getNamedChildCount(): Int {
return withLock { super.getNamedChildCount() }
}
override fun getNodeString(): String {
return withLock { super.getNodeString() }
}
override fun getStartByte(): Int {
return withLock { super.getStartByte() }
}
override fun getEndByte(): Int {
return withLock { super.getEndByte() }
}
override fun getStartPoint(): TSPoint {
return withLock { super.getStartPoint() }
}
override fun getEndPoint(): TSPoint {
return withLock { super.getEndPoint() }
}
override fun getType(): String {
return withLock { super.getType() }
}
override fun getSymbol(): Int {
return withLock { super.getSymbol() }
}
override fun isNull(): Boolean {
return withLock { super.isNull() }
}
override fun isNamed(): Boolean {
return withLock { super.isNamed() }
}
override fun isExtra(): Boolean {
return withLock { super.isExtra() }
}
override fun isMissing(): Boolean {
return withLock { super.isMissing() }
}
override fun hasChanges(): Boolean {
return withLock { super.hasChanges() }
}
override fun hasErrors(): Boolean {
return withLock { super.hasErrors() }
}
override fun isError(): Boolean {
return withLock { super.isError() }
}
override fun getParseState(): Short {
return withLock { super.getParseState() }
}
override fun recycle() {
withLock {
this.context0 = 0
this.context1 = 0
this.context2 = 0
this.context3 = 0
this.id = 0
this.tree = 0
this.mTree = null
returnToPool()
}
this.context0 = 0
this.context1 = 0
this.context2 = 0
this.context3 = 0
this.id = 0
this.tree = 0
this.mTree = null
returnToPool()
}
}
@@ -17,11 +17,7 @@
package com.itsaky.androidide.treesitter.api
import com.itsaky.androidide.treesitter.TSLanguage
import com.itsaky.androidide.treesitter.TSParser
import com.itsaky.androidide.treesitter.TSRange
import com.itsaky.androidide.treesitter.TSTree
import com.itsaky.androidide.treesitter.string.UTF16String
import com.itsaky.androidide.utils.DefaultRecyclable
import com.itsaky.androidide.utils.RecyclableObjectPool
@@ -30,8 +26,7 @@ import com.itsaky.androidide.utils.RecyclableObjectPool
*/
class TreeSitterParser @JvmOverloads internal constructor(
pointer: Long = 0
) : TSParser(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable(),
TSSynchronized by DefaultSynchronized() {
) : TSParser(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable() {
companion object {
@@ -46,38 +41,8 @@ class TreeSitterParser @JvmOverloads internal constructor(
}
override fun close() {
withLock {
super.close()
recycle()
}
}
override fun setLanguage(language: TSLanguage?) {
withLock { super.setLanguage(language) }
}
override fun parseString(oldTree: TSTree?, source: UTF16String?): TSTree {
return withLock { super.parseString(oldTree, source) }
}
override fun setTimeout(microseconds: Long) {
withLock { super.setTimeout(microseconds) }
}
override fun getTimeout(): Long {
return withLock { super.getTimeout() }
}
override fun setIncludedRanges(ranges: Array<out TSRange>?): Boolean {
return withLock { super.setIncludedRanges(ranges) }
}
override fun getIncludedRanges(): Array<TSRange> {
return withLock { super.getIncludedRanges() }
}
override fun reset() {
withLock { super.reset() }
super.close()
recycle()
}
override fun recycle() {
@@ -17,9 +17,7 @@
package com.itsaky.androidide.treesitter.api
import com.itsaky.androidide.treesitter.TSQuantifier
import com.itsaky.androidide.treesitter.TSQuery
import com.itsaky.androidide.treesitter.TSQueryPredicateStep
import com.itsaky.androidide.utils.DefaultRecyclable
import com.itsaky.androidide.utils.RecyclableObjectPool
@@ -28,8 +26,7 @@ import com.itsaky.androidide.utils.RecyclableObjectPool
*/
class TreeSitterQuery @JvmOverloads internal constructor(
pointer: Long = 0
) : TSQuery(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable(),
TSSynchronized by DefaultSynchronized() {
) : TSQuery(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable() {
companion object {
@@ -41,59 +38,9 @@ class TreeSitterQuery @JvmOverloads internal constructor(
}
}
override fun getCaptureCount(): Int {
return withLock { super.getCaptureCount() }
}
override fun getPatternCount(): Int {
return withLock { super.getPatternCount() }
}
override fun getStringCount(): Int {
return withLock { super.getStringCount() }
}
override fun getCaptureNames(): Array<String> {
return withLock { super.getCaptureNames() }
}
override fun getStartByteForPattern(pattern: Int): Int {
return withLock { super.getStartByteForPattern(pattern) }
}
override fun getPredicatesForPattern(pattern: Int): Array<TSQueryPredicateStep> {
return withLock { super.getPredicatesForPattern(pattern) }
}
override fun isPatternRooted(pattern: Int): Boolean {
return withLock { super.isPatternRooted(pattern) }
}
override fun isPatternNonLocal(pattern: Int): Boolean {
return withLock { super.isPatternNonLocal(pattern) }
}
override fun isPatternGuaranteedAtStep(offset: Int): Boolean {
return withLock { super.isPatternGuaranteedAtStep(offset) }
}
override fun getCaptureNameForId(id: Int): String {
return withLock { super.getCaptureNameForId(id) }
}
override fun getStringValueForId(id: Int): String {
return withLock { super.getStringValueForId(id) }
}
override fun getCaptureQuantifierForId(pattern: Int, capture: Int): TSQuantifier {
return withLock { super.getCaptureQuantifierForId(pattern, capture) }
}
override fun close() {
withLock {
super.close()
recycle()
}
super.close()
recycle()
}
override fun recycle() {
@@ -17,11 +17,7 @@
package com.itsaky.androidide.treesitter.api
import com.itsaky.androidide.treesitter.TSNode
import com.itsaky.androidide.treesitter.TSPoint
import com.itsaky.androidide.treesitter.TSQuery
import com.itsaky.androidide.treesitter.TSQueryCursor
import com.itsaky.androidide.treesitter.TSQueryMatch
import com.itsaky.androidide.utils.DefaultRecyclable
import com.itsaky.androidide.utils.RecyclableObjectPool
@@ -30,8 +26,7 @@ import com.itsaky.androidide.utils.RecyclableObjectPool
*/
class TreeSitterQueryCursor(
pointer: Long = 0
) : TSQueryCursor(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable(),
TSSynchronized by DefaultSynchronized() {
) : TSQueryCursor(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable() {
companion object {
@@ -43,43 +38,9 @@ class TreeSitterQueryCursor(
}
}
override fun exec(query: TSQuery?, node: TSNode?) {
withLock { super.exec(query, node) }
}
override fun didExceedMatchLimit(): Boolean {
return withLock { super.didExceedMatchLimit() }
}
override fun getMatchLimit(): Int {
return withLock { super.getMatchLimit() }
}
override fun setMatchLimit(newLimit: Int) {
withLock { super.setMatchLimit(newLimit) }
}
override fun setByteRange(start: Int, end: Int) {
withLock { super.setByteRange(start, end) }
}
override fun setPointRange(start: TSPoint?, end: TSPoint?) {
withLock { super.setPointRange(start, end) }
}
override fun nextMatch(): TSQueryMatch? {
return withLock { super.nextMatch() }
}
override fun removeMatch(id: Int) {
withLock { super.removeMatch(id) }
}
override fun close() {
withLock {
super.close()
recycle()
}
super.close()
recycle()
}
override fun recycle() {
@@ -17,23 +17,16 @@
package com.itsaky.androidide.treesitter.api
import com.itsaky.androidide.treesitter.TSInputEdit
import com.itsaky.androidide.treesitter.TSLanguage
import com.itsaky.androidide.treesitter.TSNode
import com.itsaky.androidide.treesitter.TSRange
import com.itsaky.androidide.treesitter.TSTree
import com.itsaky.androidide.utils.DefaultRecyclable
import com.itsaky.androidide.utils.RecyclableObjectPool
import java.util.concurrent.TimeUnit
/**
* @author Akash Yadav
*/
class TreeSitterTree @JvmOverloads internal constructor(
pointer: Long = 0,
private val synchronized: DefaultSynchronized = DefaultSynchronized()
) : TSTree(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable(),
TSSynchronized by synchronized {
pointer: Long = 0
) : TSTree(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable() {
private var ownerThread: Thread? = null
@@ -48,41 +41,10 @@ class TreeSitterTree @JvmOverloads internal constructor(
}
}
override fun getRootNode(): TSNode {
return withLock { super.getRootNode() }
}
override fun getChangedRanges(oldTree: TSTree?): Array<TSRange> {
return withLock { super.getChangedRanges(oldTree) }
}
override fun copy(): TSTree {
return withLock { super.copy() }
}
override fun edit(edit: TSInputEdit?) {
withLock { super.edit(edit) }
}
override fun getLanguage(): TSLanguage {
return withLock { super.getLanguage() }
}
override fun <T> withLock(timeout: Long, unit: TimeUnit, action: () -> T): T {
// check(ownerThread != null && Thread.currentThread() == ownerThread) {
// "TSTree instances are not thread-safe. Copy the tree if you need to use it on multiple threads." +
// "currentThread=${Thread.currentThread()} initialThread=$ownerThread"
// }
return synchronized.withLock(timeout, unit, action)
}
override fun close() {
withLock {
super.close()
this.nativeObject = 0
recycle()
}
super.close()
this.nativeObject = 0
recycle()
}
override fun recycle() {
@@ -17,10 +17,7 @@
package com.itsaky.androidide.treesitter.api
import com.itsaky.androidide.treesitter.TSNode
import com.itsaky.androidide.treesitter.TSPoint
import com.itsaky.androidide.treesitter.TSTreeCursor
import com.itsaky.androidide.treesitter.TSTreeCursorNode
import com.itsaky.androidide.utils.DefaultRecyclable
import com.itsaky.androidide.utils.RecyclableObjectPool
@@ -29,8 +26,7 @@ import com.itsaky.androidide.utils.RecyclableObjectPool
*/
class TreeSitterTreeCursor @JvmOverloads internal constructor(
pointer: Long = 0
) : TSTreeCursor(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable(),
TSSynchronized by DefaultSynchronized() {
) : TSTreeCursor(pointer), RecyclableObjectPool.Recyclable by DefaultRecyclable() {
companion object {
@@ -42,79 +38,9 @@ class TreeSitterTreeCursor @JvmOverloads internal constructor(
}
}
override fun getCurrentNode(): TSNode {
return withLock { super.getCurrentNode() }
}
override fun getCurrentFieldName(): String {
return withLock { super.getCurrentFieldName() }
}
override fun getCurrentFieldId(): Short {
return withLock { super.getCurrentFieldId() }
}
override fun getCurrentTreeCursorNode(): TSTreeCursorNode {
return withLock { super.getCurrentTreeCursorNode() }
}
override fun gotoFirstChild(): Boolean {
return withLock { super.gotoFirstChild() }
}
override fun gotoFirstChildForByte(byteIndex: Int): Long {
return withLock { super.gotoFirstChildForByte(byteIndex) }
}
override fun gotoFirstChildForPoint(point: TSPoint?): Boolean {
return withLock { super.gotoFirstChildForPoint(point) }
}
override fun gotoLastChild(): Boolean {
return withLock { super.gotoLastChild() }
}
override fun gotoNextSibling(): Boolean {
return withLock { super.gotoNextSibling() }
}
override fun gotoPreviousSibling(): Boolean {
return withLock { super.gotoPreviousSibling() }
}
override fun gotoParent(): Boolean {
return withLock { super.gotoParent() }
}
override fun gotoDescendant(descendantIndex: Int) {
withLock { super.gotoDescendant(descendantIndex) }
}
override fun getCurrentDescendantIndex(): Int {
return withLock { super.getCurrentDescendantIndex() }
}
override fun getDepth(): Int {
return withLock { super.getDepth() }
}
override fun reset(node: TSNode?) {
withLock { super.reset(node) }
}
override fun resetTo(another: TSTreeCursor?) {
withLock { super.resetTo(another) }
}
override fun copy(): TSTreeCursor {
return withLock { super.copy() }
}
override fun close() {
withLock {
super.close()
recycle()
}
super.close()
recycle()
}
override fun recycle() {
@@ -41,9 +41,12 @@ fun <ResultT> TSQueryCursor.safeExecQueryCursor(
whileTrue: ((TSQueryMatch?) -> Boolean)? = null,
onClosedOrEdited: (() -> Unit)? = null,
debugName: String = "",
debugLogging: Boolean = false,
action: (TSQueryMatch) -> ResultT
) : ResultT? {
log.isEnabled = debugLogging
if (tree == null || !tree.canAccess()) {
log.debug("$debugName: Cannot execute query, tree is null or not accessible", "tree=$tree", "tree.canAccess=${tree?.canAccess()}")
return null
@@ -69,6 +72,7 @@ fun <ResultT> TSQueryCursor.safeExecQueryCursor(
whileTrue = whileTrue,
onClosedOrEdited = onClosedOrEdited,
debugName = debugName,
debugLogging = debugLogging,
action = action
)
}
@@ -88,6 +92,7 @@ fun <ResultT> TSQueryCursor.safeExecQueryCursor(
whileTrue: ((TSQueryMatch?) -> Boolean)? = null,
onClosedOrEdited: (() -> Unit)? = null,
debugName: String = "",
debugLogging: Boolean = false,
action: (TSQueryMatch) -> ResultT
) : ResultT? {
@@ -102,6 +107,7 @@ fun <ResultT> TSQueryCursor.safeExecQueryCursor(
whileTrue = whileTrue,
onClosedOrEdited = onClosedOrEdited,
debugName = debugName,
debugLogging = debugLogging,
action = action)
}
@@ -113,9 +119,12 @@ private fun <ResultT> TSQueryCursor.doSafeExecQueryCursor(
whileTrue: ((TSQueryMatch?) -> Boolean)? = null,
onClosedOrEdited: (() -> Unit)? = null,
debugName: String = "",
debugLogging: Boolean = false,
action: (TSQueryMatch) -> ResultT
) : ResultT? {
log.isEnabled = debugLogging
if (!query.canAccess()) {
log.debug("$debugName: Cannot execute query, query is not accessible")
return null