Skip to content
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

Add an optional target queue to for database operations #801

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

sharplet
Copy link

The optional target queue is useful for synchronising other work with respect to database operations. Provided the target queue is also a serial queue, work submitted to the private connection queue and work submitted directly to the target queue will still be executed in serial.

The queue context is now set on the target queue if present. The connection queue inherits values set on the target queue, so the call to DispatchQueue.getSpecific, using queueKey, returns queueContext as expected. For this logic to still be thread-safe, the target queue must be a serial queue. queueKey is now an instance variable so that every connection gets a unique key, ensuring that connections can't write over one another's context variables.

The attributes: parameter is also removed from the call to initialise the database queue, as [] (i.e., serial) is already the default.

sharplet and others added 2 commits April 21, 2018 14:11
The optional target queue is useful for synchronising other work with
respect to database operations. Provided the target queue is also a
serial queue, work submitted to the private connection queue and work
submitted directly to the target queue will still be executed in serial.

The queue context is now set on the target queue if present. The
connection queue inherits values set on the target queue, so the call to
`DispatchQueue.getSpecific`, using `queueKey`, returns `queueContext` as
expected. For this logic to still be thread-safe, the target queue must
be a serial queue. `queueKey` is now an instance variable so that every
connection gets a unique key, ensuring that connections can't write over
one another's context variables.

The `attributes:` parameter is also removed from the call to initialise
the database queue, as `[]` (i.e., serial) is already the default.
/// synchronized with respect to database operations.
///
/// **Important:** The target queue **must not** be a concurrent queue, or access
/// to the returned connection is no longer guaranteed be thread-safe.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be asserted in code?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be nice! Currently I'm not aware of public API to check whether a queue is serial or concurrent. 🤔

Sources/SQLite/Core/Connection.swift Show resolved Hide resolved
@nathanfallet
Copy link
Collaborator

Perhaps related to #937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants