Using Rust main from a custom entry point (Part 2)7 min read June 25, 2022 #rust #tianocore #gsoc22 #uefi
Hello everyone. I will continue where I left off in post with the Rust main in this post. Since we now have an allocator, the
Thread::new statement at
library/std/src/rt.rs works. So we need to fix the line where we set the main thread with guard information. This will be a short post since it turned out easier than I initially thought.
Thread Local Macro
thread_local! wraps any number of static declarations and makes them thread-local. To get a better understanding of what this macro does, we can take a look at this example from the docs:
use RefCell; use thread; thread_local!; FOO.with; // each thread starts out with the initial value of 1 let t = spawn; // wait for the thread to complete and bail out on panic t.join.unwrap; // we retain our original value of 2 despite the child thread FOO.with;
As you can see, it allows the creation of static variables local to each thread.
std::sys_common::thread_info module defines a thread-local variable
THREAD_INFO using the
thread_local! macro. The error occurs when this variable is lazily initialized in the
init() function at
__thread_local_internal macro contains conditional compilation for
wasm target without
atomics. Since UEFI is single-threaded (I know there is a way to execute code in other cores, but it’s not exactly true multi-threading, from what I understand), and I don’t have any
atomics (at least not yet), I just decided to use the wasm implementation. This maps to a simple mutable static which should be fine to do for now. The new
__thread_local_internal looks like:
We will also need to add
target_os = "uefi" to conditional compilation of
After that, it works perfectly. I’m not sure if this is the correct implementation, but it also fixes stdio (which I will implement in the next post) for me, so I think it is acceptable for now. However, anyone who understands this better is free to contact me through mail.
As I stated earlier, this is a pretty short post. While I could post an empty
main function, it’s useless without having the ability to print to screen from
main(). So this is where I will conclude for now. I promise we will print “Hello World!” from
main() next time.
Back to top