瀏覽代碼

Regression: `AmlContext: Send + Sync` again.

Before the introduction of native methods, the `AmlContext` structure was
both `Send` and `Sync`, allowing it to be stored inside containers that
require this such as `spin::Once`. I use this to allow the global structure to
be shared without reparsing the AML.

To do this it makes two changes:

1. The `Fn` for native methods in `MethodCode::Native` is now required to
be `Send` and `Sync`.
2. It now wraps the native function in an `Arc` instead of `Rc`
Michael Melanson 3 年之前
父節點
當前提交
92ab90a36d
共有 2 個文件被更改,包括 5 次插入5 次删除
  1. 1 1
      aml/src/lib.rs
  2. 4 4
      aml/src/value.rs

+ 1 - 1
aml/src/lib.rs

@@ -673,7 +673,7 @@ impl AmlContext {
 }
 
 // TODO: docs
-pub trait Handler {
+pub trait Handler: Send + Sync {
     fn read_u8(&self, address: usize) -> u8;
     fn read_u16(&self, address: usize) -> u16;
     fn read_u32(&self, address: usize) -> u32;

+ 4 - 4
aml/src/value.rs

@@ -1,5 +1,5 @@
 use crate::{misc::ArgNum, AmlContext, AmlError, AmlHandle, AmlName};
-use alloc::{rc::Rc, string::String, vec::Vec};
+use alloc::{string::String, sync::Arc, vec::Vec};
 use bit_field::BitField;
 use core::{cmp, fmt, fmt::Debug};
 
@@ -154,7 +154,7 @@ pub enum AmlType {
 #[derive(Clone)]
 pub enum MethodCode {
     Aml(Vec<u8>),
-    Native(Rc<dyn Fn(&mut AmlContext) -> Result<AmlValue, AmlError>>),
+    Native(Arc<dyn Fn(&mut AmlContext) -> Result<AmlValue, AmlError> + Send + Sync>),
 }
 
 impl fmt::Debug for MethodCode {
@@ -218,10 +218,10 @@ impl AmlValue {
 
     pub fn native_method<F>(arg_count: u8, serialize: bool, sync_level: u8, f: F) -> AmlValue
     where
-        F: Fn(&mut AmlContext) -> Result<AmlValue, AmlError> + 'static,
+        F: (Fn(&mut AmlContext) -> Result<AmlValue, AmlError>) + 'static + Send + Sync,
     {
         let flags = MethodFlags::new(arg_count, serialize, sync_level);
-        AmlValue::Method { flags, code: MethodCode::Native(Rc::new(f)) }
+        AmlValue::Method { flags, code: MethodCode::Native(Arc::new(f)) }
     }
 
     pub fn type_of(&self) -> AmlType {