Skip to content

Commit

Permalink
refactor: Extract RingBuffer from PipeRingBuffer for better reusability
Browse files Browse the repository at this point in the history
  • Loading branch information
Stone749990226 committed Feb 28, 2025
1 parent 3d8ce37 commit 46ff143
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 70 deletions.
5 changes: 5 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ members = [
"crates/spinlock",
"crates/timer_list",
"crates/tuple_for_each",
"crates/ringbuffer",
"crates/tty",

"modules/axalloc",
Expand Down
5 changes: 4 additions & 1 deletion api/ruxos_posix_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,16 @@ ruxfdtable = { path = "../../modules/ruxfdtable" }
ruxmm = { path = "../../modules/ruxmm", optional = true }
ruxfutex = { path = "../../modules/ruxfutex", optional = true }
axalloc = { path = "../../modules/axalloc", optional = true }
ruxtask = { path = "../../modules/ruxtask", features = ["notest"], optional = true }
ruxtask = { path = "../../modules/ruxtask", features = [
"notest",
], optional = true }
ruxfs = { path = "../../modules/ruxfs", optional = true }
ruxnet = { path = "../../modules/ruxnet", optional = true }

# Other crates
axio = { path = "../../crates/axio" }
axerrno = { path = "../../crates/axerrno" }
ringbuffer = { path = "../../crates/ringbuffer" }
memory_addr = "0.1.0"
static_assertions = "1.1.0"
spin = { version = "0.9" }
Expand Down
73 changes: 4 additions & 69 deletions api/ruxos_posix_api/src/imp/pipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use alloc::sync::{Arc, Weak};
use core::ffi::c_int;
use ringbuffer::RingBuffer;
use ruxfs::fops;

use axerrno::{LinuxError, LinuxResult};
Expand All @@ -19,82 +20,16 @@ use ruxfdtable::{FileLike, RuxStat};
use crate::{ctypes, sys_fcntl};
use ruxtask::fs::{add_file_like, close_file_like};

#[derive(Copy, Clone, PartialEq)]
enum RingBufferStatus {
Full,
Empty,
Normal,
}

const RING_BUFFER_SIZE: usize = ruxconfig::PIPE_BUFFER_SIZE;

pub struct PipeRingBuffer {
arr: [u8; RING_BUFFER_SIZE],
head: usize,
tail: usize,
status: RingBufferStatus,
}

impl PipeRingBuffer {
pub const fn new() -> Self {
Self {
arr: [0; RING_BUFFER_SIZE],
head: 0,
tail: 0,
status: RingBufferStatus::Empty,
}
}

pub fn write_byte(&mut self, byte: u8) {
self.status = RingBufferStatus::Normal;
self.arr[self.tail] = byte;
self.tail = (self.tail + 1) % RING_BUFFER_SIZE;
if self.tail == self.head {
self.status = RingBufferStatus::Full;
}
}

pub fn read_byte(&mut self) -> u8 {
self.status = RingBufferStatus::Normal;
let c = self.arr[self.head];
self.head = (self.head + 1) % RING_BUFFER_SIZE;
if self.head == self.tail {
self.status = RingBufferStatus::Empty;
}
c
}

/// Get the length of remaining data in the buffer
pub const fn available_read(&self) -> usize {
if matches!(self.status, RingBufferStatus::Empty) {
0
} else if self.tail > self.head {
self.tail - self.head
} else {
self.tail + RING_BUFFER_SIZE - self.head
}
}

/// Get the length of remaining space in the buffer
pub const fn available_write(&self) -> usize {
if matches!(self.status, RingBufferStatus::Full) {
0
} else {
RING_BUFFER_SIZE - self.available_read()
}
}
}

pub struct Pipe {
readable: bool,
buffer: Arc<Mutex<PipeRingBuffer>>,
buffer: Arc<Mutex<RingBuffer>>,
// to find the write end when the read end is closed
_write_end_closed: Option<Weak<Mutex<PipeRingBuffer>>>,
_write_end_closed: Option<Weak<Mutex<RingBuffer>>>,
}

impl Pipe {
pub fn new() -> (Pipe, Pipe) {
let buffer = Arc::new(Mutex::new(PipeRingBuffer::new()));
let buffer = Arc::new(Mutex::new(RingBuffer::new(ruxconfig::PIPE_BUFFER_SIZE)));
let read_end = Pipe {
readable: true,
buffer: buffer.clone(),
Expand Down
11 changes: 11 additions & 0 deletions crates/ringbuffer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "ringbuffer"
version = "0.1.0"
edition = "2021"
authors = ["RuiWei Cheng <1982833213@qq.com>", "Quan Shi <749990226@qq.com>"]
description = "A data structure that uses a single, fixed-size buffer as if it were connected end-to-end"
license = "GPL-3.0-or-later OR Apache-2.0"
homepage = "https://github.com/syswonder/ruxos"
repository = "https://github.com/syswonder/ruxos/tree/dev/crates/ringbuffer"

[build-dependencies]
Loading

0 comments on commit 46ff143

Please sign in to comment.