Send/Sync -ability over functions #6446
Replies: 3 comments 1 reply
-
I think this is an understated part of this strategy. A function only has types as inputs, so it can't know if something was passed over a thread previously. One way I could see to make this work would be for any call to a non-send/sync method would have to be bubbled out of a function in its signature so that it could make its way up the stack to where a thread spawn/message passing, so that it can see that there's some non-send/sync use of some field in some structure that is accessible to the object being passed across threads. This would be rather noisy though. |
Beta Was this translation helpful? Give feedback.
-
|
What if mark every object that is passed to a thread as Well maybe not exactly like that. But the idea is to let compiler statically know that we want type to be altered in that way. |
Beta Was this translation helpful? Give feedback.
-
Well... That's true. But it's not like async/normal, its more like mut/const. Send-friendly function can be used with non-Sended objects too. And functions are Send by default, and becoming non-Send when you call non-Send function inside. So I assume most of the general code would be Send by default.
Doesn't recursively applying Sended rules to object fields is not enough? (The same as with "constness")
How exactly "go through a pointer"? To I thought of a The problem I see (both as type and as modifier) is that as a generic argument, they can't be mixed: In this way you couldn't mix Sended and non-Sended T... But some kind of runtime wrapper could be made, that will throw error/panic - if non-send function called for Sended T. Or wrapper that remove all but Send functions (single check on wrapper construction): |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
"Data-race safety" is mentioned as one of the goals in language design.
It looks like you didn't touched that part yet, so there is plenty of space for design choices.
Rust use Send Sync traits over data, to prevent non-thread-safe friendly structures from crossing "thread barrier" (being moved to another thread, or being referenced from the other thread). Looks like that approach gained some traction...
"Sometimes" though, from practical perspective, this approach feels ... too coarse. For example struct may have a single non-Send/Sync field that makes whole struct unusable in MT context:
Yet, that field may be actually never touched from the other thread. [Looks like Rust's common solution for this is struct-slicing]
What if apply Send/Sync attribute to member functions instead? By default function is Send+Sync, if function call non-Send function inside - it's Send attribute removed. The same for Sync. Attributes may be manually re-attached to function (ala
unsafe impl Sendin Rust) by user. For objects that crossed Send/Sync barrier, only member-functions with Send/Sync attributes can be called.All forms of access to object can be represented as function calls... So, if needed, Rust's behavior could be mimicked with this approach too.
If struct have some generic argument (like for example
Vec) - it's functions attributes will automatically align with it's generic type. For exampleVec::clone()will become non-Send ifT::cloneis non-Send, since inside it clone it's elements. YetVecstill remain passable to another thread, where at the very leastlen()remains callable.Modern IDEs can easily show deducted by compiler Send Sync attributes for each function.
P.S. I'm not sure this is the right place to discuss, and in case this was proposed or discussed somewhere else - my apologies in advance.
Beta Was this translation helpful? Give feedback.
All reactions