Refine some lock usages

This commit is contained in:
Ruihan Li
2024-09-19 19:13:56 +08:00
committed by Tate, Hongliang Tian
parent b13a82dc61
commit eb3a033496
2 changed files with 13 additions and 9 deletions

View File

@ -11,7 +11,7 @@ use alloc::{
};
use keyable_arc::KeyableArc;
use ostd::sync::{LocalIrqDisabled, RwLock, SpinLock, SpinLockGuard};
use ostd::sync::{LocalIrqDisabled, PreemptDisabled, RwLock, SpinLock, SpinLockGuard};
use smoltcp::{
iface::{SocketHandle, SocketSet},
phy::Device,
@ -27,7 +27,7 @@ use crate::{
pub struct IfaceCommon<E> {
interface: SpinLock<smoltcp::iface::Interface, LocalIrqDisabled>,
sockets: SpinLock<SocketSet<'static>, LocalIrqDisabled>,
used_ports: RwLock<BTreeMap<u16, usize>>,
used_ports: SpinLock<BTreeMap<u16, usize>, PreemptDisabled>,
bound_sockets: RwLock<BTreeSet<KeyableArc<AnyBoundSocketInner<E>>>>,
closing_sockets: SpinLock<BTreeSet<KeyableArc<AnyBoundSocketInner<E>>>, LocalIrqDisabled>,
ext: E,
@ -40,7 +40,7 @@ impl<E> IfaceCommon<E> {
Self {
interface: SpinLock::new(interface),
sockets: SpinLock::new(socket_set),
used_ports: RwLock::new(used_ports),
used_ports: SpinLock::new(used_ports),
bound_sockets: RwLock::new(BTreeSet::new()),
closing_sockets: SpinLock::new(BTreeSet::new()),
ext,
@ -69,7 +69,7 @@ impl<E> IfaceCommon<E> {
/// Alloc an unused port range from 49152 ~ 65535 (According to smoltcp docs)
fn alloc_ephemeral_port(&self) -> Option<u16> {
let mut used_ports = self.used_ports.write();
let mut used_ports = self.used_ports.lock();
for port in IP_LOCAL_PORT_START..=IP_LOCAL_PORT_END {
if let Entry::Vacant(e) = used_ports.entry(port) {
e.insert(0);
@ -81,7 +81,7 @@ impl<E> IfaceCommon<E> {
#[must_use]
fn bind_port(&self, port: u16, can_reuse: bool) -> bool {
let mut used_ports = self.used_ports.write();
let mut used_ports = self.used_ports.lock();
if let Some(used_times) = used_ports.get_mut(&port) {
if *used_times == 0 || can_reuse {
// FIXME: Check if the previous socket was bound with SO_REUSEADDR.
@ -97,7 +97,7 @@ impl<E> IfaceCommon<E> {
/// Release port number so the port can be used again. For reused port, the port may still be in use.
pub(crate) fn release_port(&self, port: u16) {
let mut used_ports = self.used_ports.write();
let mut used_ports = self.used_ports.lock();
if let Some(used_times) = used_ports.remove(&port) {
if used_times != 1 {
used_ports.insert(port, used_times - 1);

View File

@ -46,7 +46,7 @@ impl<E> AnyBoundSocket<E> {
/// that the old observer will never be called after the setting. Users should be aware of this
/// and proactively handle the race conditions if necessary.
pub fn set_observer(&self, new_observer: Weak<dyn SocketEventObserver>) {
*self.0.observer.write() = new_observer;
*self.0.observer.write_irq_disabled() = new_observer;
self.0.on_iface_events();
}
@ -111,8 +111,12 @@ pub(crate) struct AnyBoundSocketInner<E> {
impl<E> AnyBoundSocketInner<E> {
pub(crate) fn on_iface_events(&self) {
if let Some(observer) = Weak::upgrade(&*self.observer.read()) {
observer.on_events();
// We never hold the write lock in IRQ handlers, so we don't need to disable IRQs when we
// get the read lock.
let observer = Weak::upgrade(&*self.observer.read());
if let Some(inner) = observer {
inner.on_events();
}
}