Skip to main content Skip to main content
Pigweed's logo Pigweed
code_xml
  1. Home
  2. Reference
  3. Rust
  4. pw_kernel
  5. src
  6. kernel/object/interrupt.rs

kernel/object/
interrupt.rs

1// Copyright 2025 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7//     https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15use pw_status::Result;
16use pw_time_core::Instant;
17
18use crate::Kernel;
19use crate::object::{KernelObject, ObjectBase, SignalUpdate, Signals, WaitReturn};
20
21/// Object for handling userspace interrupts.
22pub struct InterruptObject<K: Kernel> {
23    base: ObjectBase<K>,
24    ack_irqs: fn(Signals),
25}
26
27impl<K: Kernel> InterruptObject<K> {
28    #[must_use]
29    pub const fn new(ack_irqs: fn(Signals)) -> Self {
30        Self {
31            base: ObjectBase::new(Signals::no_active()),
32            ack_irqs,
33        }
34    }
35
36    pub fn interrupt(&self, kernel: K, signal_mask: Signals) {
37        self.base.signal(kernel, SignalUpdate::raise(signal_mask));
38    }
39}
40
41impl<K: Kernel> KernelObject<K> for InterruptObject<K> {
42    fn base(&self) -> Option<&ObjectBase<K>> {
43        Some(&self.base)
44    }
45
46    fn object_wait(
47        &self,
48        kernel: K,
49        signal_mask: Signals,
50        deadline: Instant<K::Clock>,
51    ) -> Result<WaitReturn> {
52        self.base.wait_until(kernel, signal_mask, deadline)
53    }
54
55    fn interrupt_ack(&self, kernel: K, signal_mask: Signals) -> Result<()> {
56        // Clear the signaled interrupts.
57        self.base.signal(kernel, SignalUpdate::clear(signal_mask));
58        (self.ack_irqs)(signal_mask);
59        Ok(())
60    }
61}