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 lock sync primitives #52

Merged
merged 5 commits into from
Jun 15, 2023
Merged

Add lock sync primitives #52

merged 5 commits into from
Jun 15, 2023

Conversation

ethanfrey
Copy link
Collaborator

Based on the theoretical work in #49

Define a Lockable<T> type that can be stored in an Item or Map and manages pending read or write locks in storage,
so as to hold them over multiple blocks until an IBC ack comes in.

The "normal" use now needs to be via val.read()? or val.write()? to get &T or &mut T, so a slight change to calling code, but not too bad. And there are new methods to lock or unlock it which can control whether the previous are allowed.
Also, usage in higher-level constructs (like Map::range) should enforce this and error if a single element has a write-lock.

@ethanfrey ethanfrey mentioned this pull request Jun 9, 2023
9 tasks
@ethanfrey ethanfrey marked this pull request as ready for review June 9, 2023 11:46
@ethanfrey ethanfrey mentioned this pull request Jun 9, 2023
Copy link
Collaborator

@maurolacy maurolacy left a comment

Choose a reason for hiding this comment

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

LGTM.

#[test]
fn modify_item_with_locks() {
let mut store = MockStorage::new();
const PERSON: Item<Lockable<Person>> = Item::new("person");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Interesting functionality of a persistent lock. This can prove very handy.

packages/sync/src/locks.rs Outdated Show resolved Hide resolved
.unwrap_err();
assert_eq!(err, PersonError::Lock(LockError::WriteLocked));

// We can get count (kind of edge case bug, but I don't think we can change this or it matters)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically, this is correct. The map itself is unlocked, and so, its state can change.

The same with removal of a locked item, I guess? Would be good to add a test for it, just to showcase it.

Copy link
Collaborator

@maurolacy maurolacy Jun 11, 2023

Choose a reason for hiding this comment

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

Unless we consider removing a locked item an error, and use a wrapper around remove to try and enforce that.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Unless we consider removing a locked item an error, and use a wrapper around remove to try and enforce that.

Yeah, it seemed a new LockedMap type would be a whole lot of work, so I was trying to figure out how to do this with minimal intervention.

I think it is correct that count works, but remove should definitely be prohibited. Any ideas how to do so without copying over the entire Map API?

Copy link
Collaborator

@maurolacy maurolacy Jun 12, 2023

Choose a reason for hiding this comment

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

Rust composition over inheritance makes writing this kind of minimally modified containers a pain.

The only way I see around a LockedMap / LockableMap and similar is a Lockable::remove method. It feels unnatural, and enforcement will be responsibility of the user (as there's no way to avoid calling the base / original remove), but it's perhaps preferable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I will add such a method as helper.
Maybe another PR to add some sort of LockableMap alternative which enforces everything

Copy link
Collaborator

@JakeHartnell JakeHartnell left a comment

Choose a reason for hiding this comment

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

Very cool and useful package. 🧠

@ethanfrey ethanfrey merged commit 6ac212d into main Jun 15, 2023
@ethanfrey ethanfrey deleted the add-lock-sync-primitives branch June 15, 2023 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants