Code size optimizations.

This commit is contained in:
Dario Nieuwenhuis
2021-03-27 01:43:38 +01:00
parent 7a41541ab2
commit 4ce46df160
6 changed files with 41 additions and 25 deletions

View File

@ -4,9 +4,9 @@ use core::cmp::min;
use core::future::Future;
use core::marker::PhantomData;
use core::pin::Pin;
use core::ptr;
use core::ptr::NonNull;
use core::task::{Context, Poll, Waker};
use core::{mem, ptr};
use super::run_queue::{RunQueue, RunQueueItem};
use super::timer_queue::{TimerQueue, TimerQueueItem};
@ -143,6 +143,10 @@ impl<F: Future + 'static> Task<F> {
}
Poll::Pending => {}
}
// the compiler is emitting a virtual call for waker drop, but we know
// it's a noop for our waker.
mem::forget(waker);
}
}

View File

@ -1,7 +1,7 @@
use core::ptr;
use cortex_m::peripheral::NVIC;
use atomic_polyfill::{AtomicPtr, Ordering};
use atomic_polyfill::{compiler_fence, AtomicPtr, Ordering};
pub use embassy_macros::interrupt_declare as declare;
pub use embassy_macros::interrupt_take as take;
@ -58,22 +58,27 @@ pub trait InterruptExt: Interrupt {
impl<T: Interrupt + ?Sized> InterruptExt for T {
fn set_handler(&self, func: unsafe fn(*mut ())) {
compiler_fence(Ordering::SeqCst);
let handler = unsafe { self.__handler() };
handler.func.store(func as *mut (), Ordering::Release);
handler.func.store(func as *mut (), Ordering::Relaxed);
compiler_fence(Ordering::SeqCst);
}
fn remove_handler(&self) {
compiler_fence(Ordering::SeqCst);
let handler = unsafe { self.__handler() };
handler.func.store(ptr::null_mut(), Ordering::Release);
handler.func.store(ptr::null_mut(), Ordering::Relaxed);
compiler_fence(Ordering::SeqCst);
}
fn set_handler_context(&self, ctx: *mut ()) {
let handler = unsafe { self.__handler() };
handler.ctx.store(ctx, Ordering::Release);
handler.ctx.store(ctx, Ordering::Relaxed);
}
#[inline]
fn enable(&self) {
compiler_fence(Ordering::SeqCst);
unsafe {
NVIC::unmask(NrWrap(self.number()));
}
@ -82,6 +87,7 @@ impl<T: Interrupt + ?Sized> InterruptExt for T {
#[inline]
fn disable(&self) {
NVIC::mask(NrWrap(self.number()));
compiler_fence(Ordering::SeqCst);
}
#[inline]

View File

@ -31,6 +31,7 @@ unsafe impl<T> Send for Forever<T> {}
unsafe impl<T> Sync for Forever<T> {}
impl<T> Forever<T> {
#[inline(always)]
pub const fn new() -> Self {
Self {
used: AtomicBool::new(false),
@ -43,10 +44,11 @@ impl<T> Forever<T> {
/// Panics if this `Forever` already has a value.
///
/// Returns a mutable reference to the stored value.
#[inline(always)]
pub fn put(&'static self, val: T) -> &'static mut T {
if self
.used
.compare_exchange(false, true, Ordering::AcqRel, Ordering::Acquire)
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
.is_err()
{
panic!("Forever.put() called multiple times");
@ -60,6 +62,25 @@ impl<T> Forever<T> {
}
}
#[inline(always)]
pub fn put_with(&'static self, val: impl FnOnce() -> T) -> &'static mut T {
if self
.used
.compare_exchange(false, true, Ordering::Relaxed, Ordering::Relaxed)
.is_err()
{
panic!("Forever.put() called multiple times");
}
unsafe {
let p = self.t.get();
let p = (&mut *p).as_mut_ptr();
p.write(val());
&mut *p
}
}
#[inline(always)]
pub unsafe fn steal(&'static self) -> &'static mut T {
let p = self.t.get();
let p = (&mut *p).as_mut_ptr();