Commit Graph

2826 Commits

Author SHA1 Message Date
Matthias Krüger
aec535f805
Rollup merge of #94559 - m-ou-se:thread-scope-spawn-closure-without-arg, r=Mark-Simulacrum
Remove argument from closure in thread::Scope::spawn.

This implements ```@danielhenrymantilla's``` [suggestion](https://github.com/rust-lang/rust/issues/93203#issuecomment-1040798286) for improving the scoped threads interface.

Summary:

The `Scope` type gets an extra lifetime argument, which represents basically its own lifetime that will be used in `&'scope Scope<'scope, 'env>`:

```diff
- pub struct Scope<'env> { .. };
+ pub struct Scope<'scope, 'env: 'scope> { .. }

  pub fn scope<'env, F, T>(f: F) -> T
  where
-     F: FnOnce(&Scope<'env>) -> T;
+     F: for<'scope> FnOnce(&'scope Scope<'scope, 'env>) -> T;
```

This simplifies the `spawn` function, which now no longer passes an argument to the closure you give it, and now uses the `'scope` lifetime for everything:

```diff
-     pub fn spawn<'scope, F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
+     pub fn spawn<F, T>(&'scope self, f: F) -> ScopedJoinHandle<'scope, T>
      where
-         F: FnOnce(&Scope<'env>) -> T + Send + 'env,
+         F: FnOnce() -> T + Send + 'scope,
-         T: Send + 'env;
+         T: Send + 'scope;
```

The only difference the user will notice, is that their closure now takes no arguments anymore, even when spawning threads from spawned threads:

```diff
  thread::scope(|s| {
-     s.spawn(|_| {
+     s.spawn(|| {
          ...
      });
-     s.spawn(|s| {
+     s.spawn(|| {
          ...
-         s.spawn(|_| ...);
+         s.spawn(|| ...);
      });
  });
```

<details><summary>And, as a bonus, errors get <em>slightly</em> better because now any lifetime issues point to the outermost <code>s</code> (since there is only one <code>s</code>), rather than the innermost <code>s</code>, making it clear that the lifetime lasts for the entire <code>thread::scope</code>.

</summary>

```diff
  error[E0373]: closure may outlive the current function, but it borrows `a`, which is owned by the current function
   --> src/main.rs:9:21
    |
- 7 |         s.spawn(|s| {
-   |                  - has type `&Scope<'1>`
+ 6 |     thread::scope(|s| {
+   |                    - lifetime `'1` appears in the type of `s`
  9 |             s.spawn(|| println!("{:?}", a)); // might run after `a` is dropped
    |                     ^^                  - `a` is borrowed here
    |                     |
    |                     may outlive borrowed value `a`
    |
  note: function requires argument type to outlive `'1`
   --> src/main.rs:9:13
    |
  9 |             s.spawn(|| println!("{:?}", a)); // might run after `a` is dropped
    |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  help: to force the closure to take ownership of `a` (and any other referenced variables), use the `move` keyword
    |
  9 |             s.spawn(move || println!("{:?}", a)); // might run after `a` is dropped
    |                     ++++
"
```
</details>

The downside is that the signature of `scope` and `Scope` gets slightly more complex, but in most cases the user wouldn't need to write those, as they just use the argument provided by `thread::scope` without having to name its type.

Another downside is that this does not work nicely in Rust 2015 and Rust 2018, since in those editions, `s` would be captured by reference and not by copy. In those editions, the user would need to use `move ||` to capture `s` by copy. (Which is what the compiler suggests in the error.)
2022-03-08 11:04:51 +01:00
Mara Bos
a3d269e91c
Use f instead of || f().
Co-authored-by: Mark Rousskov <mark.simulacrum@gmail.com>
2022-03-07 22:14:02 +00:00
Eric Holk
7723506d13 Stabilize const_fn_fn_ptr_basics and const_fn_trait_bound 2022-03-07 08:47:15 -08:00
bors
2631aeef82 Auto merge of #94272 - tavianator:readdir-reclen-for-real, r=cuviper
fs: Don't dereference a pointer to a too-small allocation

ptr::addr_of!((*ptr).field) still requires ptr to point to an
appropriate allocation for its type.  Since the pointer returned by
readdir() can be smaller than sizeof(struct dirent), we need to entirely
avoid dereferencing it as that type.

Link: https://github.com/rust-lang/miri/pull/1981#issuecomment-1048278492
Link: https://github.com/rust-lang/rust/pull/93459#discussion_r795089971
2022-03-07 04:48:23 +00:00
fee1-dead
8ea3f236dc
Rollup merge of #94649 - ChrisDenton:unix-absolute-fix, r=Dylan-DPC
Unix path::absolute: Fix leading "." component

Testing leading `.` and `..` components were missing from the unix tests.

This PR adds them and fixes the leading `.` case. It also fixes the test cases so that they do an exact comparison.

This problem reported by ``@axetroy``
2022-03-06 22:35:31 +11:00
bors
c274e4969f Auto merge of #94648 - RalfJung:rollup-4iorcrd, r=RalfJung
Rollup of 4 pull requests

Successful merges:

 - #94630 (Update note about tier 2 docs.)
 - #94633 (Suggest removing a semicolon after derive attributes)
 - #94642 (Fix source code pages scroll)
 - #94645 (do not attempt to open cgroup files under Miri)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-03-05 19:53:45 +00:00
Chris Denton
0421af9a46
Use as_os_str to compare exact paths 2022-03-05 17:58:08 +00:00
Chris Denton
e8b7371a23
Unix path::absolute: Fix leading "." component
Testing leading `.` and `..` components were missing from the unix tests.
2022-03-05 17:57:12 +00:00
Ralf Jung
51b4ea2ba1 do not attempt to open cgroup files under Miri 2022-03-05 11:23:25 -05:00
Mara Bos
3b9e214c40 Small fixes in thread local code. 2022-03-05 11:39:03 +01:00
Mara Bos
c68c384b88 Update documentation in thread/local.rs. 2022-03-05 11:39:03 +01:00
Mara Bos
36c904594e Add debug asserts in thread local cell set methods. 2022-03-05 11:39:03 +01:00
Mara Bos
93c409d6e2 Add tracking issue number for local_key_cell_methods. 2022-03-05 11:39:03 +01:00
Mara Bos
88a693c4f4 Rename LocalKey's with_{ref,mut} to with_borrow{,_mut}. 2022-03-05 11:39:03 +01:00
Mara Bos
52ce11996b Implement RFC 3184 - thread local cell methods. 2022-03-05 11:39:03 +01:00
bors
86067bb461 Auto merge of #94546 - JmPotato:std-features-cleanup, r=m-ou-se
Clean up the std library's #![feature]s

Signed-off-by: JmPotato <ghzpotato@gmail.com>

This is part of https://github.com/rust-lang/rust/issues/87766.

r? `@m-ou-se`
2022-03-05 07:26:54 +00:00
Dylan DPC
3e1e9b4866
Rollup merge of #94446 - rusticstuff:remove_dir_all-illumos-fix, r=cuviper
UNIX `remove_dir_all()`: Try recursing first on the slow path

This only affects the _slow_ code path - if there is no `dirent.d_type` or if it is `DT_UNKNOWN`.

POSIX specifies that calling `unlink()` or `unlinkat(..., 0)` on a directory is allowed to succeed:
> The _path_ argument shall not name a directory unless the process has appropriate privileges and the implementation supports using _unlink()_ on directories.

This however can cause dangling inodes requiring an fsck e.g. on Illumos UFS, so we have to avoid that in the common case. We now just try to recurse into it first and unlink() if we can't open it as a directory.

The other two commits integrate the Macos x86-64 implementation reducing redundancy. Split into two commits for better reviewing.

Fixes #94335.
2022-03-05 04:46:37 +01:00
JmPotato
9b952b7d3a Clean up the std library's #![feature]s
Signed-off-by: JmPotato <ghzpotato@gmail.com>
2022-03-05 11:17:43 +08:00
bors
69f11fff33 Auto merge of #94628 - Dylan-DPC:rollup-v2slupe, r=Dylan-DPC
Rollup of 5 pull requests

Successful merges:

 - #94362 (Add well known values to `--check-cfg` implementation)
 - #94577 (only disable SIMD for doctests in Miri (not for the stdlib build itself))
 - #94595 (Fix invalid `unresolved imports` errors for a single-segment import)
 - #94596 (Delay bug in expr adjustment when check_expr is called multiple times)
 - #94618 (Don't round stack size up for created threads in Windows)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
2022-03-05 00:15:54 +00:00
Dylan DPC
629e7aa718
Rollup merge of #94618 - lewisclark:remove-stack-size-rounding, r=yaahc
Don't round stack size up for created threads in Windows

Fixes #94454

Windows does the rounding itself, so there isn't a need to explicity do the rounding beforehand, as mentioned by ```@ChrisDenton``` in #94454

> The operating system rounds up the specified size to the nearest multiple of the system's allocation granularity (typically 64 KB). To retrieve the allocation granularity of the current system, use the [GetSystemInfo](https://docs.microsoft.com/en-us/windows/win32/api/sysinfoapi/nf-sysinfoapi-getsysteminfo) function.

https://docs.microsoft.com/en-us/windows/win32/procthread/thread-stack-size
2022-03-04 22:58:37 +01:00
bors
5a7e4c6b5a Auto merge of #94298 - Urgau:rustbuild-check-cfg, r=Mark-Simulacrum
Enable conditional compilation checking on the Rust codebase

This pull-request enable conditional compilation checking on every rust project build by the `bootstrap` tool.

To be more specific, this PR only enable well known names checking + extra names (bootstrap, parallel_compiler, ...).

r? `@Mark-Simulacrum`
2022-03-04 21:52:34 +00:00
Lewis Clark
6843dd5013 Don't round stack size up for created threads 2022-03-04 18:04:43 +00:00
Matthias Krüger
ee3a2c7a8c
Rollup merge of #94549 - m-ou-se:thread-is-finished, r=yaahc
Rename JoinHandle::is_running to is_finished.

This is renaming `is_running` to `is_finished` as discussed on the tracking issue here: https://github.com/rust-lang/rust/issues/90470#issuecomment-1050188499

Taking some of the docs suggestions from https://github.com/rust-lang/rust/pull/94033
2022-03-04 17:31:06 +01:00
Matthias Krüger
904c6ca95c
Rollup merge of #94236 - reez12g:add_track_caller_87707, r=yaahc
Add #[track_caller] to track callers when initializing poisoned Once

This PR is for this Issue.
https://github.com/rust-lang/rust/issues/87707

With this fix, we expect to be able to track the caller when poisoned Once is initialized.
2022-03-04 17:31:04 +01:00
Hans Kratz
735f60c34f Integrate macos x86-64 remove_dir_all() impl. Step 2: readd 2022-03-04 13:47:50 +01:00
Hans Kratz
41b4423cdf Integrate macos x86-64 remove_dir_all() impl. Step 1: remove 2022-03-04 13:47:36 +01:00
Hans Kratz
e427333071 remove_dir_all(): try recursing first instead of trying to unlink()
This only affects the `slow` code path, if there is no `dirent.d_type` or if
the type is `DT_UNKNOWN`.

POSIX specifies that calling `unlink()` or `unlinkat(..., 0)` on a directory can
succeed:
> "The _path_ argument shall not name a directory unless the process has
> appropriate privileges and the implementation supports using _unlink()_ on
> directories."
This however can cause orphaned directories requiring an fsck e.g. on Illumos
UFS, so we have to avoid that in the common case. We now just try to recurse
into it first and unlink() if we can't open it as a directory.
2022-03-04 13:33:35 +01:00
Mara Bos
9099353ea8
Use '_ for irrelevant lifetimes in Debug impl.
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
2022-03-04 10:41:39 +00:00
Loïc BRANSTETT
a93c7abc69 Add #![allow(unexpected_cfgs)] in preparation of global --check-cfg 2022-03-04 11:34:51 +01:00
reez12g
bca67fe02f Add #[track_caller] to track callers when initializing poisoned Once 2022-03-03 22:41:27 -05:00
Dylan DPC
308efafc77
Rollup merge of #94572 - sunfishcode:sunfishcode/handle-or, r=joshtriplett
Use `HandleOrNull` and `HandleOrInvalid` in the Windows FFI bindings.

Use the new `HandleOrNull` and `HandleOrInvalid` types that were introduced
as part of [I/O safety] in a few functions in the Windows FFI bindings.

This factors out an `unsafe` block and two `unsafe` function calls in the
Windows implementation code.

And, it helps test `HandleOrNull` and `HandleOrInvalid`, and indeed, it
turned up a bug: `OwnedHandle` also needs to be `#[repr(transparent)]`,
as it's used inside of `HandleOrNull` and `HandleOrInvalid` which are also
`#[repr(transparent)]`.

r? ```@joshtriplett```

[I/O safety]: https://github.com/rust-lang/rust/issues/87074
2022-03-04 02:06:42 +01:00
Dylan DPC
cdfb39ef07
Rollup merge of #93965 - Mark-Simulacrum:owned-stdio, r=dtolnay
Make regular stdio lock() return 'static handles

This also deletes the unstable API surface area previously added to expose this
functionality on new methods rather than built into the current set.

Closes #86845 (tracking issue for unstable API needed without this)

r? ``````@dtolnay`````` to kick off T-libs-api FCP
2022-03-04 02:06:39 +01:00
Dylan DPC
4c70200476
Rollup merge of #88805 - krhancoc:master, r=dtolnay
Clarification of default socket flags

This PR outlines the decision to disable inheritance of socket objects when possible to child processes in the documentation.
2022-03-04 02:06:37 +01:00
Dan Gohman
35606490ab Use HandleOrNull and HandleOrInvalid in the Windows FFI bindings.
Use the new `HandleOrNull` and `HandleOrInvalid` types that were introduced
as part of [I/O safety] in a few functions in the Windows FFI bindings.

This factors out an `unsafe` block and two `unsafe` function calls in the
Windows implementation code.

And, it helps test `HandleOrNull` and `HandleOrInvalid`, which indeed turned
up a bug: `OwnedHandle` also needs to be `#[repr(transparent)]`, as it's
used inside of `HandleOrNull` and `HandleOrInvalid` which are also
`#[repr(transparent)]`.

[I/O safety]: https://github.com/rust-lang/rust/issues/87074
2022-03-03 11:20:49 -08:00
Matthias Krüger
a638f50d8d
Rollup merge of #92697 - the8472:cgroups, r=joshtriplett
Use cgroup quotas for calculating `available_parallelism`

Automated tests for this are possible but would require a bunch of assumptions. It requires root + a recent kernel, systemd and maybe docker. And even then it would need a helper binary since the test has to run in a separate process.

Limitations

* only supports cgroup v2 and assumes it's mounted under `/sys/fs/cgroup`
* procfs must be available
* the quota gets mixed into `sched_getaffinity`, so if the latter doesn't work then quota information gets ignored too

Manually tested via

```
// spawn a new cgroup scope for the current user
$ sudo systemd-run -p CPUQuota="300%" --uid=$(id -u) -tdS

// quota.rs
#![feature(available_parallelism)]
fn main() {
    println!("{:?}", std:🧵:available_parallelism()); // prints Ok(3)
}
```

strace:

```
sched_getaffinity(3041643, 32, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]) = 32
openat(AT_FDCWD, "/proc/self/cgroup", O_RDONLY|O_CLOEXEC) = 3
statx(0, NULL, AT_STATX_SYNC_AS_STAT, STATX_ALL, NULL) = -1 EFAULT (Bad address)
statx(3, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0444, stx_size=0, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
read(3, "0::/system.slice/run-u31477.serv"..., 128) = 36
read(3, "", 92)                         = 0
close(3)                                = 0
statx(AT_FDCWD, "/sys/fs/cgroup/system.slice/run-u31477.service/cgroup.controllers", AT_STATX_SYNC_AS_STAT, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0444, stx_size=0, ...}) = 0
openat(AT_FDCWD, "/sys/fs/cgroup/system.slice/run-u31477.service/cpu.max", O_RDONLY|O_CLOEXEC) = 3
statx(3, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=0, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
read(3, "300000 100000\n", 20)          = 14
read(3, "", 6)                          = 0
close(3)                                = 0
openat(AT_FDCWD, "/sys/fs/cgroup/system.slice/cpu.max", O_RDONLY|O_CLOEXEC) = 3
statx(3, "", AT_STATX_SYNC_AS_STAT|AT_EMPTY_PATH, STATX_ALL, {stx_mask=STATX_BASIC_STATS|STATX_MNT_ID, stx_attributes=0, stx_mode=S_IFREG|0644, stx_size=0, ...}) = 0
lseek(3, 0, SEEK_CUR)                   = 0
read(3, "max 100000\n", 20)             = 11
read(3, "", 9)                          = 0
close(3)                                = 0
openat(AT_FDCWD, "/sys/fs/cgroup/cpu.max", O_RDONLY|O_CLOEXEC) = -1 ENOENT (No such file or directory)
sched_getaffinity(0, 128, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]) = 40
```

r? ```````@joshtriplett```````
cc ```````@yoshuawuyts```````

Tracking issue and previous discussion: #74479
2022-03-03 20:01:43 +01:00
Mara Bos
24fe35a3e1 Remove argument from closure in thread::Scope::spawn. 2022-03-03 13:04:14 +01:00
Mara Bos
af86b55735 Remove unnecessary #![feature]s from doctest. 2022-03-03 12:09:18 +01:00
Mara Bos
c021ac35fa Update test. 2022-03-03 12:09:18 +01:00
Mara Bos
dadf2adbe9 Rename JoinHandle::is_running to is_finished and update docs.
Co-authored-by: Josh Triplett <josh@joshtriplett.org>
2022-03-03 12:09:17 +01:00
Matthias Krüger
6f1730c9e3
Rollup merge of #94534 - bstrie:cffistd, r=Mark-Simulacrum
Re-export (unstable) core::ffi types from std::ffi
2022-03-03 11:02:53 +01:00
Matthias Krüger
afd6f5c478
Rollup merge of #93562 - sunfishcode:sunfishcode/io-docs, r=joshtriplett
Update the documentation for `{As,Into,From}Raw{Fd,Handle,Socket}`.

This change weakens the descriptions of the
`{as,into,from}_raw_{fd,handle,socket}` descriptions from saying that
they *do* express ownership relations to say that they are *typically used*
in ways that express ownership relations. This is needed since, for
example, std's own [`RawFd`] implements `{As,From,Into}Fd` without any of
the ownership relationships.

This adds proper `# Safety` comments to `from_raw_{fd,handle,socket}`,
adds the requirement that raw handles be not opened with the
`FILE_FLAG_OVERLAPPED` flag, and merges the `OwnedHandle::from_raw_handle`
comment into the main `FromRawHandle::from_raw_handle` comment.

And, this changes `HandleOrNull` and `HandleOrInvalid` to not implement
`FromRawHandle`, since they are intended for limited use in FFI situations,
and not for generic use, and they have constraints that are stronger than
the those of `FromRawHandle`.

[`RawFd`]: https://doc.rust-lang.org/stable/std/os/unix/io/type.RawFd.html
2022-03-03 11:02:49 +01:00
Dan Gohman
8253cfef7a Remove the comment about FILE_FLAG_OVERLAPPED.
There may eventually be something to say about `FILE_FLAG_OVERLAPPED` here,
however this appears to be independent of the other changes in this PR,
so remove them from this PR so that it can be discussed separately.
2022-03-02 16:25:31 -08:00
Dylan DPC
c9dc44be24
Rollup merge of #93663 - sunfishcode:sunfishcode/as-raw-name, r=joshtriplett
Rename `BorrowedFd::borrow_raw_fd` to `BorrowedFd::borrow_raw`.

Also, rename `BorrowedHandle::borrow_raw_handle` and
`BorrowedSocket::borrow_raw_socket` to `BorrowedHandle::borrow_raw` and
`BorrowedSocket::borrow_raw`.

This is just a minor rename to reduce redundancy in the user code calling
these functions, and to eliminate an inessential difference between
`BorrowedFd` code and `BorrowedHandle`/`BorrowedSocket` code.

While here, add a simple test exercising `BorrowedFd::borrow_raw_fd`.

r? ``````@joshtriplett``````
2022-03-03 01:09:10 +01:00
Dylan DPC
bc1a8905d6
Rollup merge of #93354 - sunfishcode:sunfishcode/document-borrowedfd-toowned, r=joshtriplett
Add documentation about `BorrowedFd::to_owned`.

Following up on #88564, this adds documentation explaining why
`BorrowedFd::to_owned` returns another `BorrowedFd` rather than an
`OwnedFd`. And similar for `BorrowedHandle` and `BorrowedSocket`.

r? `````@joshtriplett`````
2022-03-03 01:09:09 +01:00
The 8472
e18abbf2ac update available_parallelism docs since cgroups and sched_getaffinity are now taken into account 2022-03-03 00:43:46 +01:00
The 8472
af6d2ed245 hardcode /sys/fs/cgroup instead of doing a lookup via mountinfo
this avoids parsing mountinfo which can be huge on some systems and
something might be emulating cgroup fs for sandboxing reasons which means
it wouldn't show up as mountpoint

additionally the new implementation operates on a single pathbuffer, reducing allocations
2022-03-03 00:43:46 +01:00
The 8472
bac5523ea0 Use cgroup quotas for calculating available_parallelism
Manually tested via


```
// spawn a new cgroup scope for the current user
$ sudo systemd-run -p CPUQuota="300%" --uid=$(id -u) -tdS


// quota.rs
#![feature(available_parallelism)]
fn main() {
    println!("{:?}", std:🧵:available_parallelism()); // prints Ok(3)
}
```


Caveats

* cgroup v1 is ignored
* funky mountpoints (containing spaces, newlines or control chars) for cgroupfs will not be handled correctly since that would require unescaping /proc/self/mountinfo
  The escaping behavior of procfs seems to be undocumented. systemd and docker default to `/sys/fs/cgroup` so it should be fine for most systems.
* quota will be ignored when `sched_getaffinity` doesn't work
* assumes procfs is mounted under `/proc` and cgroupfs mounted and readable somewhere in the directory tree
2022-03-03 00:43:45 +01:00
Dan Gohman
af642bb466 Fix a broken doc link on Windows. 2022-03-02 12:39:36 -08:00
bstrie
9aed829fe6 Re-export core::ffi types from std::ffi 2022-03-02 13:52:31 -05:00
Josh Triplett
335c9609c6 Provide C FFI types via core::ffi, not just in std
The ability to interoperate with C code via FFI is not limited to crates
using std; this allows using these types without std.

The existing types in `std::os::raw` become type aliases for the ones in
`core::ffi`. This uses type aliases rather than re-exports, to allow the
std types to remain stable while the core types are unstable.

This also moves the currently unstable `NonZero_` variants and
`c_size_t`/`c_ssize_t`/`c_ptrdiff_t` types to `core::ffi`, while leaving
them unstable.
2022-03-01 17:16:05 -08:00