diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-03-28 01:01:06 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-03-28 01:01:06 +0900 |
| commit | 1b48484c169daa48a006f52f48766d358fa8f2bc (patch) | |
| tree | eb4957260d3b38bf0072a22cb31e47284096da43 /internal/uevent | |
| parent | 713bff3eb0a77efff016acc02b6f10913810f887 (diff) | |
internal/uevent: exclusive socket access
This is a much simplified mutex, since blocking is not required.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/uevent')
| -rw-r--r-- | internal/uevent/uevent.go | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/internal/uevent/uevent.go b/internal/uevent/uevent.go index 6a3ff441..12754f46 100644 --- a/internal/uevent/uevent.go +++ b/internal/uevent/uevent.go @@ -4,6 +4,7 @@ package uevent import ( + "sync/atomic" "syscall" "hakurei.app/internal/netlink" @@ -17,7 +18,23 @@ type ( ) // Conn represents a NETLINK_KOBJECT_UEVENT socket. -type Conn struct{ conn *netlink.Conn } +type Conn struct { + conn *netlink.Conn + + // Whether currently between a call to enterExcl and exitExcl. + excl atomic.Bool +} + +// enterExcl must be called entering a critical section that interacts with conn. +func (c *Conn) enterExcl() error { + if !c.excl.CompareAndSwap(false, true) { + return syscall.EAGAIN + } + return nil +} + +// exitExcl must be called exiting a critical section that interacts with conn. +func (c *Conn) exitExcl() { c.excl.Store(false) } // Close closes the underlying socket. func (c *Conn) Close() error { return c.conn.Close() } @@ -29,5 +46,5 @@ func Dial() (*Conn, error) { if err != nil { return nil, err } - return &Conn{c}, err + return &Conn{conn: c}, err } |
