Skip to content

Commit d8068b6

Browse files
committed
ch05: use stablized naked_functions feature
The `naked_functions` feature was stabilized in rust-lang/rust#134213. Update the code to use this stabilized feature. This still requires nightly Rust until rust-lang/rust#134213 is released in stable Rust.
1 parent 47b4f7d commit d8068b6

File tree

6 files changed

+23
-18
lines changed

6 files changed

+23
-18
lines changed

ch05/c-fibers/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ run the example just fine.
1414

1515
## Running the example
1616

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

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

ch05/c-fibers/src/main.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
///
77
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
88
/// for more information.
9-
#![feature(naked_functions)]
109
use std::arch::{asm, naked_asm};
1110

1211
const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
@@ -147,7 +146,7 @@ fn guard() {
147146
};
148147
}
149148

150-
#[naked]
149+
#[unsafe(naked)]
151150
unsafe extern "C" fn skip() {
152151
naked_asm!("ret")
153152
}
@@ -159,7 +158,7 @@ pub fn yield_thread() {
159158
};
160159
}
161160

162-
#[naked]
161+
#[unsafe(naked)]
163162
#[no_mangle]
164163
#[cfg_attr(target_os = "macos", export_name = "\x01switch")] // see: How-to-MacOS-M.md for explanation
165164
unsafe extern "C" fn switch() {
@@ -199,11 +198,16 @@ fn main() {
199198
runtime.spawn(|| {
200199
println!("THREAD 2 STARTING");
201200
let id = 2;
202-
for i in 0..15 {
201+
for i in 0..7 {
202+
println!("thread: {} counter: {}", id, i);
203+
}
204+
yield_thread();
205+
for i in 7..15 {
203206
println!("thread: {} counter: {}", id, i);
204207
yield_thread();
205208
}
206209
println!("THREAD 2 FINISHED");
207210
});
211+
208212
runtime.run();
209213
}

ch05/d-fibers-closure/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ run the example just fine.
2222

2323
## Running the example
2424

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

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

ch05/d-fibers-closure/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
///
77
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
88
/// for more information.
9-
#![feature(naked_functions)]
109
use std::{arch::{asm, naked_asm}};
1110

1211
const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
@@ -159,7 +158,7 @@ fn call(thread: u64) {
159158
}
160159
}
161160

162-
#[naked]
161+
#[unsafe(naked)]
163162
unsafe extern "C" fn skip() {
164163
naked_asm!("ret")
165164
}
@@ -180,7 +179,7 @@ pub fn yield_thread() {
180179
(*rt_ptr).t_yield();
181180
};
182181
}
183-
#[naked]
182+
#[unsafe(naked)]
184183
#[no_mangle]
185184
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
186185
unsafe extern "C" fn switch() {

ch05/e-fibers-windows/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ AMD desktop CPU's use this architecture).
1313

1414
## Running the example
1515

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

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

@@ -306,4 +307,4 @@ As you see, our code gets just a little bit longer. It's not difficult once you'
306307
307308
## Conclusion
308309

309-
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.
310+
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.

ch05/e-fibers-windows/src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
///
77
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
88
/// for more information.
9-
#![feature(naked_functions)]
109
use std::arch::{asm, naked_asm};
1110

1211
const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
@@ -151,7 +150,7 @@ impl Runtime {
151150
}
152151
}
153152

154-
#[naked]
153+
#[unsafe(naked)]
155154
unsafe extern "C" fn skip() {
156155
naked_asm!("ret")
157156
}
@@ -172,7 +171,7 @@ pub fn yield_thread() {
172171
}
173172

174173
#[cfg(not(target_os = "windows"))]
175-
#[naked]
174+
#[unsafe(naked)]
176175
#[no_mangle]
177176
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
178177
unsafe extern "C" fn switch() {
@@ -278,7 +277,7 @@ impl Runtime {
278277
// reference: https://probablydance.com/2013/02/20/handmade-coroutines-for-windows/
279278
// Contents of TIB on Windows: https://en.wikipedia.org/wiki/Win32_Thread_Information_Block
280279
#[cfg(target_os = "windows")]
281-
#[naked]
280+
#[unsafe(naked)]
282281
#[no_mangle]
283282
unsafe extern "C" fn switch() {
284283
asm!(

0 commit comments

Comments
 (0)