-
Notifications
You must be signed in to change notification settings - Fork 260
wasm implementation of RawMutex and RawRwlock using UnsafeCell #187
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
Conversation
| #![cfg_attr(feature = "nightly", feature(asm))] | ||
|
|
||
| cfg_if::cfg_if! { | ||
| if #[cfg(target_arch = "wasm32")] { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This wont work correctly when using the multi threading wasm extension.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is true, but neither would the original code without being explicitly designed for the new atomic primitives that aren’t even available on nightly, right? If it needs new code to work I’m not fussed about the exact way it happens to be incorrect now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add
#[cfg(all(target_arch = "wasm32", target_feature = "atomics"))]
compile_error!("parking_lot doesnt yet support multi threaded wasm");Also atomic.notify and atomic.wait are exposed on nightly: https://doc.rust-lang.org/stable/core/arch/wasm32/index.html Normal atomics are accessable using the Atomic* types.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was referring mainly to having to compile libstd from source. Regardless, this is probably a good idea.
|
@alexcrichton You're more familiar with WASM than I am, could you offer your views on this PR? |
|
I don't really know much about the existing primitives and why they won't work "as is" on wasm because there's no contention, but you'll likely want to throw an assert that |
|
I think this might mostly boil down to more accurate error messages. If you fail to acquire a lock in a single threaded environment, that’s kinda your fault and no amount of waiting can fix it (sorry, me) but the error messages look like parking_lot is missing support. Essentially, this fails faster and helps people understand what their logic errors are. At least, it would if I had done the panic statement copywriting better. The compiled binaries do end up a bit smaller as well. Is there a cfg flag for any targets that are single threaded? |
|
I'm not sure if this is the right approach. My understanding is that only Now |
|
@Amanieu I don't really have a choice, and I suspect a lot of other people don't either. I don't depend on parking_lot directly, I just use Nobody is using mutexes on Using a completely different single-threaded 'mutex' or spin-rs is pointless, because if that worked, you would alread fall into the RefCell bucket. If you don't, then you have two choices: swap between I think it's appropriate that a single-threaded RawMutex implementation should exist, in any case. Perhaps you would consider putting the RawMutex implementations it in a different crate and swapping them out in |
|
What I'm trying to understand is why you are using |
|
Basically the point that I'm trying to make is that parking_lot requires some sort of OS support, which |
@Amanieu Yes. I’m making a library with wasm-pack and wasm-bindgen, to be consumed by JavaScript applications. What is it about parking_lot that requires OS support? Of course it requires OS support to park threads and interact with a scheduler, but that’s begging the question, since those only exist on OS targets. But I don’t see anything in the API that actually needs it. You have the wait_ methods in Condvar, but if they return immediately, that’s perfectly fine. They don’t actually have to sleep to be correct as described. Synchronous waiting simulates code that returns immediately. If you mean OS support for Instant, yes, that indeed isn’t supported, but single threaded code should be completely ignoring all time-related values anyway, so that other PR was overkill, you can just pass Instant(0). |
|
You've convinced me that supporting My main concern is that you are duplicating a lot of the existing code, which will make maintenance more difficult in the future. I think that a better approach would be to only change parking_lot_core, which has two uses of |
|
Is there any updates on this one? I would really like to use this together with WASM. Would love to see this being merged at some point 👍 |
|
@Amanieu is right |
|
@cormacrelf @Amanieu Does that mean we are not going forward with this at all? |
|
My previous suggestion still stands:
|
|
I would be happy to accept a PR for it. |
|
@Amanieu By dummy you mean it does nothing? Won't that break the fair timeout then? |
|
Well sure but the lock itself will still work fine. There isn't much else we can do since wasm32-unknown-unknown doesn't give us any timer API we can use. |
|
True, could we rely on the |
|
I would prefer just disabling eventual fairness. We don't want to make any assumptions on the environment (WASM can be used outside of browsers). |
|
Also, would it work for you if we had something like a custom type Detecting the arch with the |
|
Right. But that means the |
|
You can still force fairness by unlocking with |
|
What do you think about the proposed solution with the cfg attribute and the custom |
|
That sounds like a good plan. Please keep the changes internal to parking_lot.rs, the public API should keep using the standard |
|
Yep, I would define that custom type inside |
|
Or what if there was a flag to disable eventual fairness? Would that be something? |
|
I wouldn't bother. Just disable it for wasm because it doesn't have a working |
|
Yea makes sense 👍 |
|
Trying to send a PR shortly. |
|
Really need this 👍 |
|
Here's a PR. Let me know if that works for you or if you want me to change something. |
|
@Amanieu The way that other libraries (such as rand, chrono, reqwest) handle this is to have a |
This is very rough, but it was borne out of necessity. Essentially, parking is not something you can do in wasm, so I was encountering flurries of parking_lot panics, and not just from #166.
So, this reimplements RawMutex and RawRwLock in the same way std has vastly simplified implementations for wasm. There's a bit of cruft, and I can't claim to understand the meaning of the translated
cmpxchgin RwLock but it seems to work, and doesn't panic all the time. Clearly it needs more eyes on it though.Questions