Skip to content

Commit

Permalink
Debug toString for channels
Browse files Browse the repository at this point in the history
  • Loading branch information
elizarov committed Mar 6, 2018
1 parent fcce038 commit e4b6f09
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,11 @@

package kotlinx.coroutines.experimental.channels

import kotlinx.coroutines.experimental.CancellableContinuation
import kotlinx.coroutines.experimental.DisposableHandle
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.internal.*
import kotlinx.coroutines.experimental.intrinsics.startCoroutineUndispatched
import kotlinx.coroutines.experimental.removeOnCancel
import kotlinx.coroutines.experimental.selects.ALREADY_SELECTED
import kotlinx.coroutines.experimental.selects.SelectClause1
import kotlinx.coroutines.experimental.selects.SelectClause2
import kotlinx.coroutines.experimental.selects.SelectInstance
import kotlinx.coroutines.experimental.suspendAtomicCancellableCoroutine
import kotlin.coroutines.experimental.startCoroutine
import kotlinx.coroutines.experimental.intrinsics.*
import kotlinx.coroutines.experimental.selects.*
import kotlin.coroutines.experimental.*

/**
* Abstract send channel. It is a base class for all send channel implementations.
Expand Down Expand Up @@ -374,6 +368,37 @@ public abstract class AbstractSendChannel<E> : SendChannel<E> {
}
}

// ------ debug ------

public override fun toString() =
"$classSimpleName@$hexAddress{$queueDebugStateString}$bufferDebugString"

private val queueDebugStateString: String
get() {
val head = queue.next
if (head === queue) return "EmptyQueue"
var result = when (head) {
is Closed<*> -> head.toString()
is Receive<*> -> "ReceiveQueued"
is Send -> "SendQueued"
else -> "UNEXPECTED:$head" // should not happen
}
val tail = queue.prev
if (tail !== head) {
result += ",queueSize=${countQueueSize()}"
if (tail is Closed<*>) result += ",closedForSend=$tail"
}
return result
}

private fun countQueueSize(): Int {
var size = 0
queue.forEach<LockFreeLinkedListNode> { size++ }
return size
}

protected open val bufferDebugString: String get() = ""

// ------ private ------

private class SendSelect<E, R>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@

package kotlinx.coroutines.experimental.channels

import kotlinx.coroutines.experimental.selects.ALREADY_SELECTED
import kotlinx.coroutines.experimental.selects.SelectInstance
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
import kotlinx.coroutines.experimental.selects.*
import java.util.concurrent.*
import java.util.concurrent.locks.*
import kotlin.concurrent.*

/**
* Broadcast channel with array buffer of a fixed [capacity].
Expand Down Expand Up @@ -353,4 +352,9 @@ class ArrayBroadcastChannel<E>(
return result
}
}

// ------ debug ------

override val bufferDebugString: String
get() = "(buffer:capacity=${buffer.size},size=$size)"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package kotlinx.coroutines.experimental.channels

import kotlinx.coroutines.experimental.selects.ALREADY_SELECTED
import kotlinx.coroutines.experimental.selects.SelectInstance
import java.util.concurrent.locks.ReentrantLock
import kotlin.concurrent.withLock
import kotlinx.coroutines.experimental.selects.*
import java.util.concurrent.locks.*
import kotlin.concurrent.*

/**
* Channel with array buffer of a fixed [capacity].
Expand Down Expand Up @@ -245,4 +244,9 @@ public open class ArrayChannel<E>(
// then clean all queued senders
super.cleanupSendQueueOnCancel()
}

// ------ debug ------

override val bufferDebugString: String
get() = "(buffer:capacity=${buffer.size},size=$size)"
}

0 comments on commit e4b6f09

Please sign in to comment.