-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
libstd: add an RAII utility for sys_common::mutex::Mutex #51529
Conversation
(rust_highfive has picked a reviewer for you, use r? to override) |
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.
What's the motivation for this change? I don't disapprove at all, I'm just curious!
src/libstd/io/lazy.rs
Outdated
@@ -20,6 +20,9 @@ pub struct Lazy<T> { | |||
init: fn() -> Arc<T>, | |||
} | |||
|
|||
// wish we had a "polymorphic constant" of type *mut Arc<T>... |
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.
You can create a generic const fn
src/libstd/sys/unix/args.rs
Outdated
let ret = (0..ARGC).map(|i| { | ||
let cstr = CStr::from_ptr(*ARGV.offset(i) as *const libc::c_char); | ||
OsStringExt::from_vec(cstr.to_bytes().to_vec()) | ||
}).collect(); | ||
LOCK.unlock(); | ||
return ret |
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.
The temporary ret
can be removed now
src/libstd/sys/unix/os.rs
Outdated
@@ -427,7 +426,6 @@ pub fn env() -> Env { | |||
iter: result.into_iter(), | |||
_dont_send_or_sync_me: PhantomData, | |||
}; | |||
ENV_LOCK.unlock(); | |||
return ret |
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.
The temporary ret
can be removed now
src/libstd/sys/unix/os.rs
Outdated
let s = libc::getenv(k.as_ptr()) as *const libc::c_char; | ||
let ret = if s.is_null() { | ||
None | ||
} else { | ||
Some(OsStringExt::from_vec(CStr::from_ptr(s).to_bytes().to_vec())) | ||
}; | ||
ENV_LOCK.unlock(); | ||
return Ok(ret) |
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.
The temporary ret
can be removed now
src/libstd/sys/unix/os.rs
Outdated
let ret = cvt(libc::setenv(k.as_ptr(), v.as_ptr(), 1)).map(|_| ()); | ||
ENV_LOCK.unlock(); | ||
return ret |
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.
Here too
src/libstd/sys/unix/os.rs
Outdated
@@ -480,9 +476,8 @@ pub fn unsetenv(n: &OsStr) -> io::Result<()> { | |||
let nbuf = CString::new(n.as_bytes())?; | |||
|
|||
unsafe { | |||
ENV_LOCK.lock(); | |||
let _guard = ENV_LOCK.lock(); | |||
let ret = cvt(libc::unsetenv(nbuf.as_ptr())).map(|_| ()); |
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.
and here
/// A simple RAII utility for the above Mutex without the poisoning semantics. | ||
pub struct MutexGuard<'a>(&'a imp::Mutex); | ||
|
||
impl<'a> Drop for MutexGuard<'a> { |
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'm wondering whether a closure based approach would be better, since you are often creating a new scope for the lock guard anyway.
@oli-obk Well, just a refactoring? I wasn't sure about the flexibility of a closure-based approach (it surely works for the existing rustc codebase.) And we'd need I will remove unnecessary |
3859cf7
to
9ab19bc
Compare
Signed-off-by: NODA, Kai <nodakai@gmail.com>
9ab19bc
to
b81da27
Compare
@oli-obk Updated accordingly. Thanks for the comments! |
@bors r+ |
📌 Commit b81da27 has been approved by |
libstd: add an RAII utility for sys_common::mutex::Mutex It is indeed debatable whether or not we should introduce more sophistication like this to the lowest layer of a system library. In fact, `Drop::drop()` cannot be `unsafe` (IIRC there was a discussion on introducing an unsafe variant of `Drop` whose entire scope must be within `unsafe`)
☀️ Test successful - status-appveyor, status-travis |
@@ -423,12 +422,10 @@ pub fn env() -> Env { | |||
} | |||
environ = environ.offset(1); | |||
} | |||
let ret = Env { | |||
return Env { |
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 return
is unnecessary, right?
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.
Unfortunately the inner function definition fn parse()
below confuses the rustc parser if we omit the return
https://play.rust-lang.org/?gist=c39c88d10b00029672798633965295b3&version=stable&mode=debug
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.
Oh, I see. This is pretty weird - I'd have expected the inner function to precede the actual code of the function (or to be inlined, since it has just one caller).
Thanks for explaining.
It is indeed debatable whether or not we should introduce more sophistication like this to the lowest layer of a system library. In fact,
Drop::drop()
cannot beunsafe
(IIRC there was a discussion on introducing an unsafe variant ofDrop
whose entire scope must be withinunsafe
)