mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-21 08:26:30 +00:00
Refine some lock usages
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
b13a82dc61
commit
eb3a033496
@ -11,7 +11,7 @@ use alloc::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use keyable_arc::KeyableArc;
|
use keyable_arc::KeyableArc;
|
||||||
use ostd::sync::{LocalIrqDisabled, RwLock, SpinLock, SpinLockGuard};
|
use ostd::sync::{LocalIrqDisabled, PreemptDisabled, RwLock, SpinLock, SpinLockGuard};
|
||||||
use smoltcp::{
|
use smoltcp::{
|
||||||
iface::{SocketHandle, SocketSet},
|
iface::{SocketHandle, SocketSet},
|
||||||
phy::Device,
|
phy::Device,
|
||||||
@ -27,7 +27,7 @@ use crate::{
|
|||||||
pub struct IfaceCommon<E> {
|
pub struct IfaceCommon<E> {
|
||||||
interface: SpinLock<smoltcp::iface::Interface, LocalIrqDisabled>,
|
interface: SpinLock<smoltcp::iface::Interface, LocalIrqDisabled>,
|
||||||
sockets: SpinLock<SocketSet<'static>, 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>>>>,
|
bound_sockets: RwLock<BTreeSet<KeyableArc<AnyBoundSocketInner<E>>>>,
|
||||||
closing_sockets: SpinLock<BTreeSet<KeyableArc<AnyBoundSocketInner<E>>>, LocalIrqDisabled>,
|
closing_sockets: SpinLock<BTreeSet<KeyableArc<AnyBoundSocketInner<E>>>, LocalIrqDisabled>,
|
||||||
ext: E,
|
ext: E,
|
||||||
@ -40,7 +40,7 @@ impl<E> IfaceCommon<E> {
|
|||||||
Self {
|
Self {
|
||||||
interface: SpinLock::new(interface),
|
interface: SpinLock::new(interface),
|
||||||
sockets: SpinLock::new(socket_set),
|
sockets: SpinLock::new(socket_set),
|
||||||
used_ports: RwLock::new(used_ports),
|
used_ports: SpinLock::new(used_ports),
|
||||||
bound_sockets: RwLock::new(BTreeSet::new()),
|
bound_sockets: RwLock::new(BTreeSet::new()),
|
||||||
closing_sockets: SpinLock::new(BTreeSet::new()),
|
closing_sockets: SpinLock::new(BTreeSet::new()),
|
||||||
ext,
|
ext,
|
||||||
@ -69,7 +69,7 @@ impl<E> IfaceCommon<E> {
|
|||||||
|
|
||||||
/// Alloc an unused port range from 49152 ~ 65535 (According to smoltcp docs)
|
/// Alloc an unused port range from 49152 ~ 65535 (According to smoltcp docs)
|
||||||
fn alloc_ephemeral_port(&self) -> Option<u16> {
|
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 {
|
for port in IP_LOCAL_PORT_START..=IP_LOCAL_PORT_END {
|
||||||
if let Entry::Vacant(e) = used_ports.entry(port) {
|
if let Entry::Vacant(e) = used_ports.entry(port) {
|
||||||
e.insert(0);
|
e.insert(0);
|
||||||
@ -81,7 +81,7 @@ impl<E> IfaceCommon<E> {
|
|||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn bind_port(&self, port: u16, can_reuse: bool) -> bool {
|
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 let Some(used_times) = used_ports.get_mut(&port) {
|
||||||
if *used_times == 0 || can_reuse {
|
if *used_times == 0 || can_reuse {
|
||||||
// FIXME: Check if the previous socket was bound with SO_REUSEADDR.
|
// 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.
|
/// 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) {
|
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 let Some(used_times) = used_ports.remove(&port) {
|
||||||
if used_times != 1 {
|
if used_times != 1 {
|
||||||
used_ports.insert(port, used_times - 1);
|
used_ports.insert(port, used_times - 1);
|
||||||
|
@ -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
|
/// 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.
|
/// and proactively handle the race conditions if necessary.
|
||||||
pub fn set_observer(&self, new_observer: Weak<dyn SocketEventObserver>) {
|
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();
|
self.0.on_iface_events();
|
||||||
}
|
}
|
||||||
@ -111,8 +111,12 @@ pub(crate) struct AnyBoundSocketInner<E> {
|
|||||||
|
|
||||||
impl<E> AnyBoundSocketInner<E> {
|
impl<E> AnyBoundSocketInner<E> {
|
||||||
pub(crate) fn on_iface_events(&self) {
|
pub(crate) fn on_iface_events(&self) {
|
||||||
if let Some(observer) = Weak::upgrade(&*self.observer.read()) {
|
// We never hold the write lock in IRQ handlers, so we don't need to disable IRQs when we
|
||||||
observer.on_events();
|
// get the read lock.
|
||||||
|
let observer = Weak::upgrade(&*self.observer.read());
|
||||||
|
|
||||||
|
if let Some(inner) = observer {
|
||||||
|
inner.on_events();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user