diff options
author | Patrick Rohr <prohr@google.com> | 2023-08-18 14:10:21 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2023-08-18 14:10:21 +0000 |
commit | 62aa4b02c1c30de87a41bbe8801684c3acd1b8e4 (patch) | |
tree | cb31d4e854c392bcf170642e843b7654c78d58eb | |
parent | 63259b33f01692e54878245fe2fe82e8e8c34605 (diff) | |
parent | 04a85d98bd046108ac6371be80bbb9940be77a40 (diff) | |
download | net-62aa4b02c1c30de87a41bbe8801684c3acd1b8e4.tar.gz |
Merge "TrackRecord: add a method to return callbacks since last poll" into main
-rw-r--r-- | common/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/common/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt b/common/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt index efd77d1b..f24e4f18 100644 --- a/common/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt +++ b/common/testutils/hostdevice/com/android/net/module/util/TrackRecord.kt @@ -213,6 +213,9 @@ class ArrayTrackRecord<E> : TrackRecord<E> { private val slock = StampedLock() private var readHead = 0 + // A special mark used to track the start of the last poll() operation. + private var pollMark = 0 + /** * @return the current value of the mark. */ @@ -223,6 +226,7 @@ class ArrayTrackRecord<E> : TrackRecord<E> { val stamp = slock.tryWriteLock() if (0L == stamp) concurrentAccessDetected() readHead = v + pollMark = v slock.unlockWrite(stamp) } @@ -261,6 +265,7 @@ class ArrayTrackRecord<E> : TrackRecord<E> { fun poll(timeoutMs: Long, predicate: (E) -> Boolean = { true }): E? { val stamp = slock.tryWriteLock() if (0L == stamp) concurrentAccessDetected() + pollMark = readHead try { lock.withLock { val index = pollForIndexReadLocked(timeoutMs, readHead, predicate) @@ -273,6 +278,25 @@ class ArrayTrackRecord<E> : TrackRecord<E> { } /** + * Returns a list of events that were observed since the last time poll() was called on this + * ReadHead. + * + * @return list of events since poll() was called. + */ + fun backtrace(): List<E> { + val stamp = slock.tryReadLock() + if (0L == stamp) concurrentAccessDetected() + + try { + lock.withLock { + return ArrayList(subList(pollMark, mark)) + } + } finally { + slock.unlockRead(stamp) + } + } + + /** * Returns the first element after the mark or null. This never blocks. * * This method is subject to threading restrictions. It can be used concurrently on |