Comments (4)
My specific use case is that I need to be able to lock in the current state when some event happens (without blocking that thread)
I’m interested in potentially implementing this, but I don’t think I’m understanding the use case completely. Looking at the gist, the read
implementation doesn’t lock in the current state of the protected data until the Deref
finishes spinning (since there may still be a writer mutating the data). Is the idea here just to signal early that future writers should wait so a reader is less likely to wait?
from spin-rs.
You are essentially correct, the most important part of this is to signal that future writers need to wait, however in my system this approach serves a few purposes.
- Relatively efficient reading:: When I take a readlock I don't tend to use that state right away, so there is generally time for the active writer to finish up, and my readers won't actually spin when they go to use the data.
- Locking in the state at any time:: This includes if I am still currently updating it and goes hand-in-hand with my first point.
- Not blocking to get the value until I actually need to use it:: This also goes hand-in-hand with my first point.
To put this into a little context, I have an actor system where each actor exposes some "state". There are events in the system which require me to lock in various actor's states as quickly as possible, and pass those locks onto other actors to be consumed (in their own threads) whenever they can get around to it.
Here is an updated gist with the code I am currently using in production: https://gist.github.com/Visic/3c951421b1e15c809f207dfa34bff30e
Edit: This approach has been working wonderfully for me for almost a year now. I am happy to answer questions, but I might need to be a little vague as to the actual system I am working on.
from spin-rs.
Awesome, thanks for the explanation! I guess I'm still a little confused on this part:
Locking in the state at any time:: This includes if I am still currently updating it and goes hand-in-hand with my first point.
Based on the implementation you have in the gist and your description, I don't fully understand what you mean by "locking in the state", since a current writer isn't prevented from writing after read
returns but before deref
returns. It's might just be that a different API is needed for this, but it seems like this breaks the semantics of read
.
from spin-rs.
I think I understand your confusion, in the new gist you'll see I removed the write guard in favor of a "try_update" method, meaning that a write lock can only be used for a single update operation. This equates to "locking in the state" since the only write that can be performed after a read lock is requested, is one that is already in progress.
I'm sure the same could be done if I gave out write locks still by only using them for a single update before dropping the lock, so with the original interface, that point is really more about how I was using it rather than the actual guarentees, and it would be more accurate to just say that a new writer couldn't be given out once a reader was requested.
from spin-rs.
Related Issues (20)
- Inconsistent feature name HOT 2
- `lock_api` feature is not available in version 0.7.1 and 0.8.0. HOT 4
- Cannot borrow `lazy::Lazy<T>` mutably? HOT 5
- Remove `panicked` field from Once's Finish guard HOT 2
- Implement Once::get_mut_unchecked and Once::into_inner_unchecked HOT 4
- unwrap function in MutexGuard? HOT 4
- Request: Add support for atomic-polyfill HOT 7
- `Once::call_once` doesn't guarantee that the given closure is called only if this is the first time `call_once` has been called. HOT 2
- RwLock::try_read is unsound HOT 2
- Non-reserved-keyword identifier for spin::Once::try HOT 1
- Interest in crate maintenance. HOT 1
- Changing Ownership HOT 5
- Releasing a new version HOT 10
- Provide alternatives for `std::lazy::SyncLazy` type? HOT 4
- Add some good examples
- Implement a ticket lock for greater fairness HOT 2
- Add `std` feature with support for thread yielding HOT 1
- Switching to GitHub actions HOT 1
- Need a spinlock that never uses std::thread::yield_now HOT 9
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from spin-rs.