Repository navigation
Tracking Issue for restructuring sys_common #84187
Copy link
Copy link
Closed
Labels
A-technical-debtArea: Internal cleanup workArea: Internal cleanup workC-cleanupCategory: PRs that clean code up or issues documenting cleanup.Category: PRs that clean code up or issues documenting cleanup.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCE-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
The relationship between
std::sys_common,std::sysand the rest ofstdis complex, with dependencies going in all directions:stddepending onsys_common,sys_commondepending onsys, andsysdepending onsys_commonandstd. Ideallysys_commonwould be split into two and the dependencies between them all would form a dag.
There is a lot of interdependence between std, sys and sys_common, this is because sys_common contains several types of code:
- abstractions over the different platform implementations in std::sys (for example
std::sys_common::mutex) - code shared between platforms (for example
std::sys_common::alloc) - code that is not platform-dependent (for example
std::sys_common::poison)
In order to reduce the interdependence, sys_common will be restructured:
- A new module
sys::commonis introduced; code that is shared by all platforms will be moved fromsys_commonto this new module. - Code that is shared between some but not all platforms will be moved to
sysand shared using#[path]instead. - Code that is not platform-dependent will be moved out of
sys_commonto the appropriate place instd.
Ideally the end-result of this is sys_common again only containing platform-independent abstractions on top of sys.
Metadata
Metadata
Assignees
Labels
A-technical-debtArea: Internal cleanup workArea: Internal cleanup workC-cleanupCategory: PRs that clean code up or issues documenting cleanup.Category: PRs that clean code up or issues documenting cleanup.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCE-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
[-]Tracking Issue for splitting `sys_common`[/-][+]Tracking Issue for restructuring `sys_common`[/+]Rollup merge of rust-lang#84387 - CDirkx:poison, r=m-ou-se
Rollup merge of rust-lang#84387 - CDirkx:poison, r=m-ou-se
Rollup merge of rust-lang#84387 - CDirkx:poison, r=m-ou-se
joboet commentedon Nov 13, 2022
This guideline came up whilst discussing #104329. However, I find the wording here to be confusing:
sounds to me like the exact opposite of
Furthermore, this part of the guideline is clear
but it is very limiting. Some code (like the generic fallback for
Once) has no place insidesyssince it is shared by not all, but most of the platforms. The current default seems to be the UNIX module, which is problematic since not everything is UNIX (e.g. Windows also uses that code). There definitely exists a need for a place for this kind of shared code to go, andsys_commonis currently that place.An alternate approach could be to move
sysintosys_commonand select (withcfg_if!) the required code for each feature there (selecting either the unique implementation in the formersysmodule or shared code living insys_common). This would get rid of the unidiomatic#[path]imports, and also establish a strictly DAG-based structure (which seems to be a central idea of this guideline).Rollup merge of rust-lang#104402 - joboet:sync_remutex, r=m-ou-se
workingjubilee commentedon Mar 6, 2023
The
#[path]route kiiinda sucks. Better than nothing, but still.I think what's most important is that we eliminate the currently borderline circular dependencies in e.g. the networking code. It's an absolute rat's nest, making it easy to get lost and mistake where things are or where they should go in there. And
#[path]enables such mazy tendencies rather than fighting against them.I agree that
sys_commonshould be more neutral. I think what I want to see is something like:mod aexposes implementation primitives in systemsmod bcomposes parts frommod ainto a single systemmod creifies systems into a uniform abstractionthen
mod cis pulled intostd.This probably gets more complex in reality, with multiple "kinds" of
mod aandmod b.cc @devsnek
joboet commentedon Mar 7, 2023
I like that idea.
By the way: Would it make sense to discuss this synchronously at a libs-team meeting? I'm quite curious as to what the team thinks here.
joboet commentedon Apr 21, 2023
I'm nominating this for T-libs to discuss and brainstorm solutions. A short summary:
There are currently three places in
stdwhere code shared between some, but not all platforms is put:sys_commoncontains the shared thread parker implementations, the sharedOnceimplementations and some shared networking stuffsys::commonnow contains thethread_localmacro implementationssys::unixhas served as a "lowest common denominator" module for things likefutex-locks or raw byteOsStrencodingsThis is inconsistent and makes it difficult to find relevant code. Also, a better structure would hopefully allow getting rid of the unidiomatic
#[path]-based module imports commonly seen on the smaller platforms, which have caused quite some breakage (see #109727, #99800 and possibly others).@rustbot label +I-libs-nominated
CDirkx commentedon Apr 21, 2023
This is indeed like I originally envisioned when working on this (but I haven't been involved with the Rust project for some time).

With
sys_common(ideally with a different name,sys_wrapperorsys_interfaceor something) acting as a wrapper around system specific implementations, andsys::commononly containing simple primitives, and all dependencies forming a DAG. I also remember wanting this for making it possible to splitsysout ofstdinto seperate crates for each platform, but I don't know if that is still a goal of the project.m-ou-se commentedon Jun 21, 2023
We discussed this a few weeks ago in the libs meeting. We concluded that the currrent sys::common and sys_common setup is in such a bad state that basically any consistent new organisation would be a great improvement.
We didn't have strong opinions on what the new organisation should be. But since the organisation of these modules don't result in any stable guarantees, so we don't necessarily need to come up with the perfect new module layout right away.
So, ideally, what I think we need is someone to go through everything that currently exists in
sysandsys_commonand come up with a way to organise all those things into modules in a way that is less confusing than what we currently have. The tricky thing is that there are many cases of items that exist for e.g. only two platforms, or nearly all platforms, or in some other way don't fit into a layout with only platform-specific and 'common' modules.Another thing that makes this tricky, is that it's currently not easy to run the compiler checks (
./x.py check) for all of the different targets supported by std. (See #109099.)Once there is a plan for a layout that seems like it could cover all items in
sys/sys_common, the actual changes can then perhaps happen incrementally.Marking this issue as E-help-wanted.
joboet commentedon Oct 22, 2023
Building on @workingjubilee's proposal and incorporating @ChrisDenton's idea in #116816, I'd like to propose the following structure:
sys::apicontains platform-specific building blocks and safe abstractions.sys::..contains modules for individual features like filesystem support or thread-localsstd(potentially excludingstd::os) now only pulls the unified abstractions fromsysand is forbidden from includingsys::api.sys::apiandsys::*however can use features fromstd, which is necessary to use things likeMutexsynchronization.This structure has the benefit of allowing easier sharing of code between platforms, and is closer to patterns in crates providing cross-platform abstractions (see for instance
getrandom). Unfortunately, some platform-specific code is now split across bothsys::api::platform_nameandsys::feature_name, but this is also the case with the current structure.Examples
(to be expanded as required)
Filesystem support
The UNIX filesystem support is implemented atop the
FileDescabstraction, which provides a safe interface to theread/writesyscalls. This abstraction is moved tosys::api::unix::fd. The unified abstraction atop this interface (the currentsys::unix::fsmodule) is moved tosys::fs::unixand reexported (when the platform is UNIX) insys::fs.Thread parking
Thread parking support is mostly provided via shared implementations like the one utilizing
futex, but some platforms (Darwin, Windows) have their own implementations. The implementations ofParkerare all moved tosys::thread_parking::.., whilesys::api::unix::thread_parkingexposes thefutexwrappers (Linux, ...) or aSemaphorestructure (macOS) or theparkandunparkfunctions (NetBSD).Thread locals
The three thread-local implementations currently living in
sys::common::thread_localare moved tosys::thread_local. Their building blocks (destructor registration (assuming #116850 is merged), lazy keys) are also moved to this module. Insys::apionly remain the platform abstractions like guard enabling or key creation.workingjubilee commentedon Oct 25, 2023
Said this on Zulip, but I'm basically in favor with two details:
std::sys::unix, nor astd::sys::api::unix, but rather something somewhat better divvied-up along the real divisions between "unix-y" platforms. I think we'd get farther with something likestd::sys::api::applestd::sys::api::bsdstd::sys::api::linuxapiseems like a strange name, but it's whatever, ultimately. a common name for this pattern is "PAL", or "platform abstraction layer", so it could bestd::sys::pal, I guess?joboet commentedon Mar 12, 2024
Closing in favour of #117276.