Skip to content

Commit

Permalink
oro-std: add initial Thread and ThreadId boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
Qix- committed Jan 14, 2025
1 parent 023e7eb commit 7de5831
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ oro-ra-x86_64 = "check --quiet --message-format=json --keep-going --target ./oro

oro-ra-aarch64 = "check --quiet --message-format=json --keep-going --target ./oro-arch-aarch64/aarch64-unknown-oro.json --bin oro-kernel-aarch64 --bin oro-limine-aarch64 -Zunstable-options -Zbuild-std=core,compiler_builtins,alloc -Zbuild-std-features=compiler-builtins-mem"

oro-examples = "build --target=x86_64-unknown-none --target=aarch64-unknown-none -p example-noop -p example-spin -p example-std-noop"
oro-examples = "build --target=x86_64-unknown-none --target=aarch64-unknown-none -p example-noop -p example-spin -p example-std-noop -p example-std-noop-nightly"
8 changes: 8 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ members = [
"examples/no-std/noop",
"examples/no-std/spin",
"examples/std/noop",
"examples/std/noop-nightly",
]

[workspace.dependencies]
Expand Down
20 changes: 20 additions & 0 deletions examples/std/noop-nightly/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "example-std-noop-nightly"
description = "Example module that exits immediately."
version = "0.0.0"
publish = false
edition = "2021"

build = "build.rs"

[dependencies]
std = { version = "0.0.0", package = "oro-std", path = "../../../oro-std", features = ["nightly"] }

[dependencies.oro]
path = "../../../oro"
features = ["runtime"]

[build-dependencies.oro]
path = "../../../oro"
features = ["build"]
default-features = false
3 changes: 3 additions & 0 deletions examples/std/noop-nightly/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
::oro::build();
}
6 changes: 6 additions & 0 deletions examples/std/noop-nightly/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![no_main]

#[no_mangle]
fn main() {
// Left intentionally blank
}
4 changes: 4 additions & 0 deletions oro-std/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ workspace = true
[features]
default = []
oro = []
nightly = []

# `std` features
thread_id_value = []

# NOTE(qix-): This crate MUST NOT have any workspace (only) dependencies
# NOTE(qix-): as it is published publicly.
Expand Down
23 changes: 22 additions & 1 deletion oro-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! std = { git = "https://github.com/oro-os/kernel.git", package = "oro-std" }
//! ```
//!
//! ## OS-Specific `std::os::oro` Module
//! ### OS-Specific `std::os::oro` Module
//! By default, the `std::os::oro` module is **not** enabled. To enable it, you must include the
//! `oro` feature in your `Cargo.toml`:
//!
Expand All @@ -27,8 +27,29 @@
//!
//! Note that `std::os::oro` is just a re-export of the `oro` crate. If you wish to write
//! less-fragile code for the future, you may choose to depend on `oro` directly.
//!
//! ### Nightly Features
//! To enable certain nightly-only features that also exist in `std`, you can enable the `nightly`
//! feature in your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! std = { git = "https://github.com/oro-os/kernel.git", package = "oro-std", features = ["nightly"] }
//! ```
//!
//! Further, any feature typically enabled with `#![feature(...)]` in nightly Rust can be enabled
//! in `oro-std` by adding the feature to the `Cargo.toml` file. **Any `core` features must be
//! included using the normal `#![feature(...)]` syntax in your code.** This may change in the
//! future.
//!
//! > **NOTE:** Using the `oro-std` crates **always requires nightly** Rust, as the `oro` crate uses
//! > unstable features. However, this doesn't intrinsically mean application code also wants to
//! > use nightly features. Therefore, the `nightly` feature is **not** automatically enabled simply
//! > by building using a nightly toolchain.
#![no_std]
#![deny(unsafe_op_in_unsafe_fn)]
#![cfg_attr(doc, feature(doc_cfg, doc_auto_cfg))]
#![feature(cfg_target_thread_local)]

#[expect(unused_imports)]
use ::oro;
Expand Down
57 changes: 56 additions & 1 deletion oro-std/src/thread.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Native threads.
use crate::{fmt, num::NonZero};

/// A unique identifier for a running thread.
///
/// A `ThreadId` is an opaque object that uniquely identifies each thread created during
Expand All @@ -9,4 +11,57 @@
/// of a thread identifier – the two concepts cannot, therefore, be used interchangeably.
/// A `ThreadId` can be retrieved from the `id` method on a Thread.
#[derive(Eq, PartialEq, Clone, Copy, Hash, Debug)]
pub struct ThreadId(u64);
pub struct ThreadId(NonZero<u64>);

/// This returns a numeric identifier for the thread identified by this
/// `ThreadId`.
///
/// As noted in the documentation for the type itself, it is essentially an
/// opaque ID, but is guaranteed to be unique for each thread. The returned
/// value is entirely opaque -- only equality testing is stable. Note that
/// it is not guaranteed which values new threads will return, and this may
/// change across Rust versions.
impl ThreadId {
/// This returns a numeric identifier for the thread identified by this
/// `ThreadId`.
///
/// As noted in the documentation for the type itself, it is essentially an
/// opaque ID, but is guaranteed to be unique for each thread. The returned
/// value is entirely opaque -- only equality testing is stable. Note that
/// it is not guaranteed which values new threads will return, and this may
/// change across Rust versions.
#[cfg(all(feature = "nightly", feature = "thread_id_value"))]
#[must_use]
pub fn as_u64(&self) -> NonZero<u64> {
self.0
}
}

/// A handle to a thread.
#[derive(Clone)]
pub struct Thread {
id: ThreadId,
}

impl Thread {
/// Gets the thread’s unique identifier.
#[must_use]
pub fn id(&self) -> ThreadId {
self.id
}

/// Gets the thread's name.
#[must_use]
pub fn name(&self) -> Option<&str> {
None
}
}

impl fmt::Debug for Thread {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Thread")
.field("id", &self.id())
.field("name", &self.name())
.finish_non_exhaustive()
}
}

0 comments on commit 7de5831

Please sign in to comment.