Skip to content

ch05: use stablized naked_functions feature #35

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ch05/c-fibers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ run the example just fine.

## Running the example

This example uses the unstable feature "naked_functions" so we need to run it
using nightly Rust. There are two ways to do that.
This example uses the feature "naked_functions" so we need to run it using
nightly Rust until https://github.com/rust-lang/rust/pull/134213 is released in
stable Rust. There are two ways to do that.

1. Tell cargo to use the nightly toolchain when you run the program:

Expand Down
12 changes: 8 additions & 4 deletions ch05/c-fibers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
///
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
/// for more information.
#![feature(naked_functions)]
use std::arch::{asm, naked_asm};

const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
Expand Down Expand Up @@ -147,7 +146,7 @@ fn guard() {
};
}

#[naked]
#[unsafe(naked)]
unsafe extern "C" fn skip() {
naked_asm!("ret")
}
Expand All @@ -159,7 +158,7 @@ pub fn yield_thread() {
};
}

#[naked]
#[unsafe(naked)]
#[no_mangle]
#[cfg_attr(target_os = "macos", export_name = "\x01switch")] // see: How-to-MacOS-M.md for explanation
unsafe extern "C" fn switch() {
Expand Down Expand Up @@ -199,11 +198,16 @@ fn main() {
runtime.spawn(|| {
println!("THREAD 2 STARTING");
let id = 2;
for i in 0..15 {
for i in 0..7 {
println!("thread: {} counter: {}", id, i);
}
yield_thread();
for i in 7..15 {
println!("thread: {} counter: {}", id, i);
yield_thread();
}
println!("THREAD 2 FINISHED");
});

runtime.run();
}
5 changes: 3 additions & 2 deletions ch05/d-fibers-closure/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ run the example just fine.

## Running the example

This example uses the unstable feature "naked_functions" so we need to run it
using nightly Rust. There are two ways to do that.
This example uses the feature "naked_functions" so we need to run it using
nightly Rust until https://github.com/rust-lang/rust/pull/134213 is released in
stable Rust. There are two ways to do that.

1. Tell cargo to use the nightly toolchain when you run the program:

Expand Down
5 changes: 2 additions & 3 deletions ch05/d-fibers-closure/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
///
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
/// for more information.
#![feature(naked_functions)]
use std::{arch::{asm, naked_asm}};

const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
Expand Down Expand Up @@ -159,7 +158,7 @@ fn call(thread: u64) {
}
}

#[naked]
#[unsafe(naked)]
unsafe extern "C" fn skip() {
naked_asm!("ret")
}
Expand All @@ -180,7 +179,7 @@ pub fn yield_thread() {
(*rt_ptr).t_yield();
};
}
#[naked]
#[unsafe(naked)]
#[no_mangle]
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
unsafe extern "C" fn switch() {
Expand Down
7 changes: 4 additions & 3 deletions ch05/e-fibers-windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ AMD desktop CPU's use this architecture).

## Running the example

This example uses the unstable feature "naked_functions" so we need to run it
using nightly Rust. There are two ways to do that.
This example uses the feature "naked_functions" so we need to run it using
nightly Rust until https://github.com/rust-lang/rust/pull/134213 is released in
stable Rust. There are two ways to do that.

1. Tell cargo to use the nightly toolchain when you run the program:

Expand Down Expand Up @@ -306,4 +307,4 @@ As you see, our code gets just a little bit longer. It's not difficult once you'

## Conclusion

So this is all we needed to do. As you see we don't really do anything new here, the difficult part is figuring out how Windows works and what it expects, but now that we have done our job properly we should have a pretty complete context switch for both Unix and Windows platforms.
So this is all we needed to do. As you see we don't really do anything new here, the difficult part is figuring out how Windows works and what it expects, but now that we have done our job properly we should have a pretty complete context switch for both Unix and Windows platforms.
7 changes: 3 additions & 4 deletions ch05/e-fibers-windows/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
///
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
/// for more information.
#![feature(naked_functions)]
use std::arch::{asm, naked_asm};

const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
Expand Down Expand Up @@ -151,7 +150,7 @@ impl Runtime {
}
}

#[naked]
#[unsafe(naked)]
unsafe extern "C" fn skip() {
naked_asm!("ret")
}
Expand All @@ -172,7 +171,7 @@ pub fn yield_thread() {
}

#[cfg(not(target_os = "windows"))]
#[naked]
#[unsafe(naked)]
#[no_mangle]
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
unsafe extern "C" fn switch() {
Expand Down Expand Up @@ -278,7 +277,7 @@ impl Runtime {
// reference: https://probablydance.com/2013/02/20/handmade-coroutines-for-windows/
// Contents of TIB on Windows: https://en.wikipedia.org/wiki/Win32_Thread_Information_Block
#[cfg(target_os = "windows")]
#[naked]
#[unsafe(naked)]
#[no_mangle]
unsafe extern "C" fn switch() {
asm!(
Expand Down