bpf_loader_lib/skeleton/
handle.rs

1//!  SPDX-License-Identifier: MIT
2//!
3//! Copyright (c) 2023, eunomia-bpf
4//! All rights reserved.
5//!
6
7use std::sync::{
8    atomic::{AtomicU8, Ordering},
9    Arc,
10};
11
12const PAUSE_BIT: u8 = 1 << 0;
13const TERMINATING_BIT: u8 = 1 << 1;
14
15#[derive(Debug, Clone)]
16#[repr(transparent)]
17/// A handle to control the polling process
18pub struct PollingHandle {
19    state: Arc<AtomicU8>,
20}
21impl PollingHandle {
22    pub(crate) fn new() -> Self {
23        Self {
24            state: Arc::new(AtomicU8::new(0)),
25        }
26    }
27    pub(crate) fn reset(&self) {
28        self.state.store(0, Ordering::Release);
29    }
30    #[inline]
31    pub(crate) fn should_pause(&self) -> bool {
32        let t = self.state.load(Ordering::SeqCst);
33        t & PAUSE_BIT != 0
34    }
35    #[inline]
36    pub(crate) fn should_terminate(&self) -> bool {
37        self.state.load(Ordering::SeqCst) & TERMINATING_BIT != 0
38    }
39    /// Set the pause state of the poller
40    pub fn set_pause(&self, pause: bool) {
41        if pause {
42            self.state.fetch_or(PAUSE_BIT, Ordering::Relaxed);
43        } else {
44            self.state.fetch_and(!PAUSE_BIT, Ordering::Relaxed);
45        }
46    }
47    /// Terminate the poller. It will allow `wait_and_poll_to_handler` to return
48    pub fn terminate(&self) {
49        self.state.fetch_or(TERMINATING_BIT, Ordering::Relaxed);
50    }
51}