-
Notifications
You must be signed in to change notification settings - Fork 641
firestore: re-write BackgroundQueue to NOT use the deprecated AsyncTask thread pool #7376
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
MarkDuckworth
merged 7 commits into
main
from
dconeybe/firestore/BackgroundQueueNoMoreAsyncTaskThreadPool
Sep 16, 2025
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0255bf9
firestore: re-write BackgroundQueue to NOT use the deprecated AsyncTa…
dconeybe 4f45dbd
CHANGELOG.md: update PR number
dconeybe 32c3add
use threadlocal instead of synchronization
dconeybe a9dc976
Revert "use threadlocal instead of synchronization"
dconeybe 1b98cc5
avoid asynchronous scheduling if there is only one row in the cursor
dconeybe a4b06c1
Replace `submittingState.run` with directly accessing the properties,…
dconeybe 1a45e57
Merge branch 'main' into dconeybe/firestore/BackgroundQueueNoMoreAsyn…
dconeybe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 0 additions & 54 deletions
54
firebase-firestore/src/main/java/com/google/firebase/firestore/util/BackgroundQueue.java
This file was deleted.
Oops, something went wrong.
88 changes: 88 additions & 0 deletions
88
firebase-firestore/src/main/java/com/google/firebase/firestore/util/BackgroundQueue.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* Copyright 2025 Google LLC | ||
* | ||
* 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 com.google.firebase.firestore.util | ||
|
||
import java.util.concurrent.Executor | ||
import java.util.concurrent.Semaphore | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.asExecutor | ||
|
||
/** | ||
* Manages CPU-bound work on background threads to enable parallel processing. | ||
* | ||
* Instances of this class are _not_ thread-safe. All methods of an instance of this class must be | ||
* called from the same thread. The behavior of an instance is undefined if any of the methods are | ||
* called from multiple threads. | ||
*/ | ||
internal class BackgroundQueue { | ||
|
||
private var state: State = State.Submitting() | ||
|
||
/** | ||
* Submit a task for asynchronous execution on the executor of the owning [BackgroundQueue]. | ||
* | ||
* @throws IllegalStateException if [drain] has been called. | ||
*/ | ||
fun submit(runnable: Runnable) { | ||
val submittingState = this.state | ||
check(submittingState is State.Submitting) { "submit() may not be called after drain()" } | ||
|
||
submittingState.run { | ||
MarkDuckworth marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
taskCount++ | ||
executor.execute { | ||
try { | ||
runnable.run() | ||
} finally { | ||
completedTasks.release() | ||
} | ||
} | ||
} | ||
dconeybe marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
* Blocks until all tasks submitted via calls to [submit] have completed. | ||
* | ||
* @throws IllegalStateException if called more than once. | ||
*/ | ||
fun drain() { | ||
val submittingState = this.state | ||
check(submittingState is State.Submitting) { "drain() may not be called more than once" } | ||
this.state = State.Draining | ||
|
||
submittingState.run { completedTasks.acquire(taskCount) } | ||
} | ||
|
||
private sealed interface State { | ||
class Submitting : State { | ||
val completedTasks = Semaphore(0) | ||
var taskCount: Int = 0 | ||
} | ||
object Draining : State | ||
} | ||
|
||
companion object { | ||
|
||
/** | ||
* The maximum amount of parallelism shared by all instances of this class. | ||
* | ||
* This is equal to the number of processor cores available, or 2, whichever is larger. | ||
*/ | ||
val maxParallelism = Runtime.getRuntime().availableProcessors().coerceAtLeast(2) | ||
|
||
private val executor: Executor = | ||
Dispatchers.IO.limitedParallelism(maxParallelism, "firestore.BackgroundQueue").asExecutor() | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.