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

Mutex<T> should be Share even if T isn't #13125

Closed
sfackler opened this issue Mar 25, 2014 · 11 comments · Fixed by #13583
Closed

Mutex<T> should be Share even if T isn't #13125

sfackler opened this issue Mar 25, 2014 · 11 comments · Fixed by #13583

Comments

@sfackler
Copy link
Member

I think it's sound? It seems like a straightforward solution would be to add a #[force_share] attribute. We could also add an UnsafeForceShare marker type, but I think that'd be a bit strange since unlike the other markers, this one would remove a restriction on the type, not add one.

The current situation requires some gross workarounds like this: sfackler/rust-postgres@98c92c1

@sfackler
Copy link
Member Author

In an opt-in kind world, this would probably be an #[unsafe_share] attribute that would work like #[unsafe_destructor].

@eddyb
Copy link
Member

eddyb commented Mar 25, 2014

There is still an issue if the wrapped type is not safe to access from a different thread than the one it was created on.
EDIT: @nikomatsakis mentioned impl<T: Send> Share for Mutex<T> on IRC, which would solve the issue I thought of.

@alexcrichton
Copy link
Member

I talked with @nikomatsakis about this on IRC.

He thinks that Unsafe<T> can be special (it's already a lang item), and Unsafe<T> will be Share regardless of whether T is share. This would fix the inference problem. Assuming an opt-in world, everything would look like:

struct Mutex<T> {
   lock: RawMutex,
   data: Unsafe<T>
}

impl<T: Send> Share for Mutex<T> {}

Today's compiler would reject this code because it does not think that Unsafe<T> is Share. If the compiler thought that Unsafe<T> were indeed Share, then the type Mutex<T> would be Share

@sfackler
Copy link
Member Author

@eddyb Methods on Mutex are only implemented for T: Send so that shouldn't be an issue.

@flaper87
Copy link
Contributor

Unsafe<T> could be special cased now too. However, given the fact that opt-in kinds are coming, I don't it'd be worth it.

I'll make sure to do this as part of the opt-in kinds work.

@nikomatsakis
Copy link
Contributor

FWIW, I've updated the RFC to include text about this.

@larsbergstrom
Copy link
Contributor

With the removal of MutexArc (which previously handled this scenario for us - making something that is not Share into Share), this issue is currently blocking Servo's current Rust upgrade, because we cannot simply replace previous uses of MutexArc<T> with Arc<Mutex<T>>.

cc @Ms2ger

@flaper87
Copy link
Contributor

@larsbergstrom As mentioned in my previous comment, with few changes Unsafe could be made always Share now too. It'll be a couple of weeks before the opt-in work is done. This is probably worth discussing in the next meeting.

The issue is already assigned to me, I'm happy to tackle it asap if needed.

@larsbergstrom
Copy link
Contributor

@flaper87 Thanks! I think the biggest things we're hoping for are:

  1. a workaround that we can use for now until the Right Thing lands that isn't as ugly as copying over all of the old sync crate
  2. making sure that this gets fixed at some point in the future

I appreciate the offer, but don't need to you drop everything you're doing and work on this, especially since it sounds invasive enough that we probably couldn't cherry-pick it and we would like to finish off this Rust upgrade before we start in on yet another.

@sfackler
Copy link
Member Author

@larsbergstrom if you can pay for an extra allocation, I've worked around this issue for now by representing an Arc<Mutex<Foo>> as an Arc<Mutex<*()>> and casting between *() and ~Foo as needed. It's a bit hacky, and you'll need to make sure that there's a destructor that can clean up after it, but it's not too horrible.

@larsbergstrom
Copy link
Contributor

@sfackler Thanks! That seems totally reasonable. We'll just have to make sure to clean it up when the fix lands, which we don't always do :-/

flaper87 added a commit to flaper87/rust that referenced this issue Apr 22, 2014
This patch adds a special rule for `Unsafe<T>` and makes it `Share`
regardless of whether T is `Share`.

[breaking-change]

Closes rust-lang#13125
bors added a commit that referenced this issue Apr 22, 2014
This patch adds a special rule for `Unsafe<T>` and makes it `Share`
regardless of whether T is `Share`.

[breaking-change]

Closes #13125

cc @nikomatsakis
flip1995 pushed a commit to flip1995/rust that referenced this issue Jul 25, 2024
Changelog for Clippy 1.80 🌞

Roses are red,
Violets are blue,
Summer is fun,
So much sun

---

### The cat of this release is *Maunzer* submitted by `@llogiq:`

<img height=500 src="https://github.com/rust-lang/rust-clippy/assets/4200835/a1da6948-446d-4ccf-95a7-c816a8afdc3f" alt="The cats of this Clippy release" />

Cats for the next release can be nominated in the comments :D

---

changelog: none

I wish everyone reading this a beautiful and happy day =^.^=
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 a pull request may close this issue.

6 participants