Skip to content

Commit 0d4b978

Browse files
authored
Change the naming convention for the thread API. (#98)
Change from functions like `create_thread` to `thread::create`, so that users can just import the public `thread` module instead of `thread::*`. I considered also changing things like `thread::join(t)` to `t.join()`, but decided against it for now, and added comments to the thread module discussing this.
1 parent a719540 commit 0d4b978

13 files changed

Lines changed: 182 additions & 176 deletions

File tree

example-crates/basic/src/main.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,35 @@
1-
use origin::program::*;
2-
use origin::thread::*;
1+
use origin::{program, thread};
32

43
fn main() {
54
eprintln!("Hello from main thread");
65

7-
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
8-
at_thread_exit(Box::new(|| {
9-
eprintln!("Hello from a main-thread at_thread_exit handler")
6+
program::at_exit(Box::new(|| {
7+
eprintln!("Hello from an `program::at_exit` handler")
8+
}));
9+
thread::at_exit(Box::new(|| {
10+
eprintln!("Hello from a main-thread `thread::at_exit` handler")
1011
}));
1112

1213
let thread = unsafe {
13-
create_thread(
14+
thread::create(
1415
|_args| {
1516
eprintln!("Hello from child thread");
16-
at_thread_exit(Box::new(|| {
17-
eprintln!("Hello from child thread's at_thread_exit handler")
17+
thread::at_exit(Box::new(|| {
18+
eprintln!("Hello from child thread's `thread::at_exit` handler")
1819
}));
1920
None
2021
},
2122
&[],
22-
default_stack_size(),
23-
default_guard_size(),
23+
thread::default_stack_size(),
24+
thread::default_guard_size(),
2425
)
2526
.unwrap()
2627
};
2728

2829
unsafe {
29-
join_thread(thread);
30+
thread::join(thread);
3031
}
3132

3233
eprintln!("Goodbye from main");
33-
exit(0);
34+
program::exit(0);
3435
}

example-crates/external-start/src/main.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ extern crate libc;
1313
use alloc::boxed::Box;
1414
use atomic_dbg::{dbg, eprintln};
1515
use core::sync::atomic::{AtomicBool, Ordering};
16-
use origin::program::*;
17-
use origin::thread::*;
16+
use origin::{program, thread};
1817

1918
#[panic_handler]
2019
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
@@ -59,29 +58,31 @@ static EARLY_INIT_ARRAY: unsafe extern "C" fn(i32, *mut *mut u8) = {
5958
unsafe fn origin_main(_argc: usize, _argv: *mut *mut u8, _envp: *mut *mut u8) -> i32 {
6059
eprintln!("Hello from main thread");
6160

62-
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
63-
at_thread_exit(Box::new(|| {
64-
eprintln!("Hello from a main-thread at_thread_exit handler")
61+
program::at_exit(Box::new(|| {
62+
eprintln!("Hello from an `program::at_exit` handler")
63+
}));
64+
thread::at_exit(Box::new(|| {
65+
eprintln!("Hello from a main-thread `thread::at_exit` handler")
6566
}));
6667

67-
let thread = create_thread(
68+
let thread = thread::create(
6869
|_args| {
6970
eprintln!("Hello from child thread");
70-
at_thread_exit(Box::new(|| {
71-
eprintln!("Hello from child thread's at_thread_exit handler")
71+
thread::at_exit(Box::new(|| {
72+
eprintln!("Hello from child thread's `thread::at_exit` handler")
7273
}));
7374
None
7475
},
7576
&[],
76-
default_stack_size(),
77-
default_guard_size(),
77+
thread::default_stack_size(),
78+
thread::default_guard_size(),
7879
)
7980
.unwrap();
8081

81-
join_thread(thread);
82+
thread::join(thread);
8283

8384
eprintln!("Goodbye from main");
84-
exit(0);
85+
program::exit(0);
8586
}
8687

8788
// Libc calls `main` so we need to provide a definition to satisfy the

example-crates/no-std/src/main.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ extern crate alloc;
1010

1111
use alloc::boxed::Box;
1212
use atomic_dbg::{dbg, eprintln};
13-
use origin::program::*;
14-
use origin::thread::*;
13+
use origin::{program, thread};
1514

1615
#[panic_handler]
1716
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
@@ -29,31 +28,33 @@ static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::Glob
2928
fn start(_argc: isize, _argv: *const *const u8) -> isize {
3029
eprintln!("Hello from main thread");
3130

32-
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
33-
at_thread_exit(Box::new(|| {
34-
eprintln!("Hello from a main-thread at_thread_exit handler")
31+
program::at_exit(Box::new(|| {
32+
eprintln!("Hello from an `program::at_exit` handler")
33+
}));
34+
thread::at_exit(Box::new(|| {
35+
eprintln!("Hello from a main-thread `thread::at_exit` handler")
3536
}));
3637

3738
let thread = unsafe {
38-
create_thread(
39+
thread::create(
3940
|_args| {
4041
eprintln!("Hello from child thread");
41-
at_thread_exit(Box::new(|| {
42-
eprintln!("Hello from child thread's at_thread_exit handler")
42+
thread::at_exit(Box::new(|| {
43+
eprintln!("Hello from child thread's `thread::at_exit` handler")
4344
}));
4445
None
4546
},
4647
&[],
47-
default_stack_size(),
48-
default_guard_size(),
48+
thread::default_stack_size(),
49+
thread::default_guard_size(),
4950
)
5051
.unwrap()
5152
};
5253

5354
unsafe {
54-
join_thread(thread);
55+
thread::join(thread);
5556
}
5657

5758
eprintln!("Goodbye from main");
58-
exit(0);
59+
program::exit(0);
5960
}

example-crates/origin-start-lto/src/main.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ extern crate compiler_builtins;
1111

1212
use alloc::boxed::Box;
1313
use atomic_dbg::{dbg, eprintln};
14-
use origin::program::*;
15-
use origin::thread::*;
14+
use origin::{program, thread};
1615

1716
#[panic_handler]
1817
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
@@ -30,27 +29,29 @@ static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::Glob
3029
unsafe fn origin_main(_argc: usize, _argv: *mut *mut u8, _envp: *mut *mut u8) -> i32 {
3130
eprintln!("Hello from main thread");
3231

33-
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
34-
at_thread_exit(Box::new(|| {
35-
eprintln!("Hello from a main-thread at_thread_exit handler")
32+
program::at_exit(Box::new(|| {
33+
eprintln!("Hello from an `program::at_exit` handler")
34+
}));
35+
thread::at_exit(Box::new(|| {
36+
eprintln!("Hello from a main-thread `thread::at_exit` handler")
3637
}));
3738

38-
let thread = create_thread(
39+
let thread = thread::create(
3940
|_args| {
4041
eprintln!("Hello from child thread");
41-
at_thread_exit(Box::new(|| {
42-
eprintln!("Hello from child thread's at_thread_exit handler")
42+
thread::at_exit(Box::new(|| {
43+
eprintln!("Hello from child thread's `thread::at_exit` handler")
4344
}));
4445
None
4546
},
4647
&[],
47-
default_stack_size(),
48-
default_guard_size(),
48+
thread::default_stack_size(),
49+
thread::default_guard_size(),
4950
)
5051
.unwrap();
5152

52-
join_thread(thread);
53+
thread::join(thread);
5354

5455
eprintln!("Goodbye from main");
55-
exit(0);
56+
program::exit(0);
5657
}

example-crates/origin-start-no-alloc/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
extern crate compiler_builtins;
1010

1111
use atomic_dbg::{dbg, eprintln};
12-
use origin::program::*;
12+
use origin::program;
1313

1414
#[panic_handler]
1515
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
@@ -27,5 +27,5 @@ unsafe fn origin_main(_argc: usize, _argv: *mut *mut u8, _envp: *mut *mut u8) ->
2727
// Unlike origin-start, this example can't create threads because origin's
2828
// thread support requires an allocator.
2929

30-
exit(0);
30+
program::exit(0);
3131
}

example-crates/origin-start/src/main.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ extern crate compiler_builtins;
1111

1212
use alloc::boxed::Box;
1313
use atomic_dbg::{dbg, eprintln};
14-
use origin::program::*;
15-
use origin::thread::*;
14+
use origin::{program, thread};
1615

1716
#[panic_handler]
1817
fn panic(panic: &core::panic::PanicInfo<'_>) -> ! {
@@ -30,27 +29,29 @@ static GLOBAL_ALLOCATOR: rustix_dlmalloc::GlobalDlmalloc = rustix_dlmalloc::Glob
3029
unsafe fn origin_main(_argc: usize, _argv: *mut *mut u8, _envp: *mut *mut u8) -> i32 {
3130
eprintln!("Hello from main thread");
3231

33-
at_exit(Box::new(|| eprintln!("Hello from an at_exit handler")));
34-
at_thread_exit(Box::new(|| {
35-
eprintln!("Hello from a main-thread at_thread_exit handler")
32+
program::at_exit(Box::new(|| {
33+
eprintln!("Hello from an `program::at_exit` handler")
34+
}));
35+
thread::at_exit(Box::new(|| {
36+
eprintln!("Hello from a main-thread `thread::at_exit` handler")
3637
}));
3738

38-
let thread = create_thread(
39+
let thread = thread::create(
3940
|_args| {
4041
eprintln!("Hello from child thread");
41-
at_thread_exit(Box::new(|| {
42-
eprintln!("Hello from child thread's at_thread_exit handler")
42+
thread::at_exit(Box::new(|| {
43+
eprintln!("Hello from child thread's `thread::at_exit` handler")
4344
}));
4445
None
4546
},
4647
&[],
47-
default_stack_size(),
48-
default_guard_size(),
48+
thread::default_stack_size(),
49+
thread::default_guard_size(),
4950
)
5051
.unwrap();
5152

52-
join_thread(thread);
53+
thread::join(thread);
5354

5455
eprintln!("Goodbye from main");
55-
exit(0);
56+
program::exit(0);
5657
}

src/program.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
//! mean that origin can avoid doing any work that users might not need.
2929
3030
#[cfg(feature = "origin-thread")]
31-
use crate::thread::{initialize_main_thread, initialize_startup_thread_info};
31+
use crate::thread;
3232
#[cfg(feature = "alloc")]
3333
use alloc::boxed::Box;
3434
#[cfg(not(feature = "origin-program"))]
@@ -209,11 +209,11 @@ unsafe fn init_runtime(mem: *mut usize, envp: *mut *mut u8) {
209209

210210
// Read the program headers and extract the TLS info.
211211
#[cfg(feature = "origin-thread")]
212-
initialize_startup_thread_info();
212+
thread::initialize_startup_info();
213213

214214
// Initialize the main thread.
215215
#[cfg(feature = "origin-thread")]
216-
initialize_main_thread(mem.cast());
216+
thread::initialize_main(mem.cast());
217217
}
218218

219219
/// Functions registered with [`at_exit`].
@@ -270,7 +270,7 @@ pub fn at_exit(func: Box<dyn FnOnce() + Send>) {
270270
pub fn exit(status: c_int) -> ! {
271271
// Call functions registered with `at_thread_exit`.
272272
#[cfg(all(feature = "thread", feature = "origin-program"))]
273-
crate::thread::call_thread_dtors(crate::thread::current_thread());
273+
crate::thread::call_dtors(crate::thread::current());
274274

275275
// Call all the registered functions, in reverse order. Leave `DTORS`
276276
// unlocked while making the call so that functions can add more functions

src/thread/libc.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ extern "C" {
2323

2424
/// An opaque pointer to a thread.
2525
///
26-
/// This type does not detach on drop. It just leaks the thread. To detach or
27-
/// join, call `detach_thread` or `join_thread` explicitly.
26+
/// This type does not detach or free resources on drop. It just leaks the
27+
/// thread. To detach or join, call [`detach`] or [`join`] explicitly.
2828
#[derive(Copy, Clone)]
2929
pub struct Thread(libc::pthread_t);
3030

@@ -63,7 +63,7 @@ impl Thread {
6363
///
6464
/// `fn_(arg)` on the new thread must have defined behavior, and the return
6565
/// value must be valid to use on the eventual joining thread.
66-
pub unsafe fn create_thread(
66+
pub unsafe fn create(
6767
fn_: unsafe fn(&mut [*mut c_void]) -> Option<NonNull<c_void>>,
6868
args: &[Option<NonNull<c_void>>],
6969
stack_size: usize,
@@ -134,7 +134,7 @@ pub unsafe fn create_thread(
134134
}
135135

136136
/// Registers a function to call when the current thread exits.
137-
pub fn at_thread_exit(func: Box<dyn FnOnce()>) {
137+
pub fn at_exit(func: Box<dyn FnOnce()>) {
138138
extern "C" fn call(arg: *mut c_void) {
139139
unsafe {
140140
let arg = arg.cast::<Box<dyn FnOnce()>>();
@@ -153,7 +153,7 @@ pub fn at_thread_exit(func: Box<dyn FnOnce()>) {
153153
/// Return a raw pointer to the data associated with the current thread.
154154
#[inline]
155155
#[must_use]
156-
pub fn current_thread() -> Thread {
156+
pub fn current() -> Thread {
157157
unsafe { Thread(libc::pthread_self()) }
158158
}
159159

@@ -163,7 +163,7 @@ pub fn current_thread() -> Thread {
163163
/// field in the runtime rather than making a system call.
164164
#[inline]
165165
#[must_use]
166-
pub fn current_thread_id() -> ThreadId {
166+
pub fn current_id() -> ThreadId {
167167
// Actually, in the pthread implementation here we do just make a system
168168
// call, because we don't have access to the pthread internals.
169169
rustix::thread::gettid()
@@ -192,7 +192,7 @@ pub unsafe fn set_current_thread_id_after_a_fork(tid: ThreadId) {
192192
/// Return the TLS address for the given `offset` for the current thread.
193193
#[inline]
194194
#[must_use]
195-
pub fn current_thread_tls_addr(offset: usize) -> *mut c_void {
195+
pub fn current_tls_addr(offset: usize) -> *mut c_void {
196196
let p = [1, offset];
197197
unsafe { __tls_get_addr(&p) }
198198
}
@@ -233,7 +233,7 @@ pub unsafe fn thread_stack(thread: Thread) -> (*mut c_void, usize, usize) {
233233
/// `thread` must point to a valid thread record that has not yet been detached
234234
/// and will not be joined.
235235
#[inline]
236-
pub unsafe fn detach_thread(thread: Thread) {
236+
pub unsafe fn detach(thread: Thread) {
237237
let thread = thread.0;
238238

239239
assert_eq!(libc::pthread_detach(thread), 0);
@@ -248,7 +248,7 @@ pub unsafe fn detach_thread(thread: Thread) {
248248
///
249249
/// `thread` must point to a valid thread record that has not already been
250250
/// detached or joined.
251-
pub unsafe fn join_thread(thread: Thread) -> Option<NonNull<c_void>> {
251+
pub unsafe fn join(thread: Thread) -> Option<NonNull<c_void>> {
252252
let thread = thread.0;
253253

254254
let mut return_value: *mut c_void = null_mut();
@@ -289,7 +289,7 @@ pub fn default_guard_size() -> usize {
289289

290290
/// Yield the current thread, encouraging other threads to run.
291291
#[inline]
292-
pub fn yield_current_thread() {
292+
pub fn yield_current() {
293293
let _ = unsafe { libc::sched_yield() };
294294
}
295295

0 commit comments

Comments
 (0)