Re: [PATCH] Make ENOSPC not fatal in semaphore creation - Mailing list pgsql-hackers

From Thomas Munro
Subject Re: [PATCH] Make ENOSPC not fatal in semaphore creation
Date
Msg-id CA+hUKGLUaT2hMHtqm9ih3vBsGMvGXuHtoToFTtr_5m4vemDWiA@mail.gmail.com
Whole thread Raw
In response to Re: [PATCH] Make ENOSPC not fatal in semaphore creation  (Thomas Munro <thomas.munro@gmail.com>)
Responses Re: [PATCH] Make ENOSPC not fatal in semaphore creation
List pgsql-hackers
On Mon, Oct 18, 2021 at 10:07 AM Thomas Munro <thomas.munro@gmail.com> wrote:
> Note: The best kind would be *unnamed* POSIX semas, where we get to
> control their placement in existing memory; that's what we do on Linux
> and FreeBSD.  They weren't supported on OpenBSD last time we checked:
> it rejects requests for shared ones.  I wonder if someone could
> implement them with just a few lines of user space code, using atomic
> counters and futex() for waiting.

I meant that it'd be cool if OpenBSD implemented shared memory unnamed
semas that way (as other OSes do), but just for fun I tried
implementing that in PostgreSQL.  I already had a patch to provide a
wrapper API for futexes on a bunch of OSes including OpenBSD (because
I've been looking into ways to rewrite lwlock.c to use futexes
directly and skip all the per-backend semaphore stuff).  That made it
easy to write a quick-and-dirty clone of sem_{init,wait,post}() using
atomics and futexes.

Sadly, although the attached proof-of-concept patch allows a
PREFERRED_SEMAPHORES=FUTEX build to pass tests on macOS (which also
lacks native unnamed semas), FreeBSD and Linux (which don't need this
but are interesting to test), and it also works on OpenBSD with
shared_memory_type=sysv, it doesn't work on OpenBSD with
shared_memory_type=mmap (the default).  I suspect OpenBSD's futex(2)
has a bug: inherited anonymous shared mmap memory seems to confuse it
so that wakeups are lost.  Arrrgh!

Attachment

pgsql-hackers by date:

Previous
From: Kenaniah Cerny
Date:
Subject: Re: Proposal: allow database-specific role memberships
Next
From: Bharath Rupireddy
Date:
Subject: Re: pg_receivewal starting position