Article View: comp.lang.c++
Article #1180Re: FtxSema is a binary semaphore, not a general one (and a counting futex version)
From: Chris M. Thomasson
Date: Sat, 05 Dec 2026 21:09
Date: Sat, 05 Dec 2026 21:09
74 lines
3713 bytes
3713 bytes
On 12/4/2026 4:33 PM, Chris M. Thomasson wrote:
> In the futex vs. system semaphore benchmark, FtxSema is named a
> semaphore, but it's a binary semaphore (effectively an auto-reset
> event). release() does m_ftx = 1, so releases don't accumulate:
>
> FtxSema s(false);
> s.release();
> s.release();
> s.acquire();
> s.acquire(); // blocks forever; a counting semaphore would not
>
> This is deterministic and single-threaded. It matters for any use where
> releases can outnumber acquires, such as producer/consumer queues where
> N posts must allow N waits.
>
> For some reason I was under the impression that it was a normal
> semaphore. Sigh. Sorry.
Just off the top of my head... Warning, NOT checked, not verified!
Bonita, I "think" this might "work" for an actual semaphore wrt your
futex version, mixing in the benaphore as a wait-free fast path? Wait
free on x86/x64 where fetch_add is not emulated with LL/SC, ugg.
I need to code it up in Relacy to blast all of the damn crap out of it!
:^)
typing into the newsreader. I don't even know if it will compile!
_______________________________
struct ct_futex_sema
{
ct_futex_sema(int count) : m_count(count) {}
void
post()
{
if (m_count.fetch_add(1, std::memory_order_release) < 0)
{
m_wake.fetch_add(1, std::memory_order_release);
m_wake.notify_one();
}
}
void
wait()
{
if (m_count.fetch_sub(1, std::memory_order_acquire) < 1)
{
int w = m_wake.load(std::memory_order_acquire);
for (;;)
{
while (w == 0)
{
m_wake.wait(0, std::memory_order_acquire);
w = m_wake.load(std::memory_order_acquire);
}
if (m_wake.compare_exchange_weak(w, w - 1,
std::memory_order_acquire,
std::memory_order_relaxed))
break;
}
}
}
std::atomic<int> m_count;
std::atomic<int> m_wake{0};
};
_______________________________
It might crap out if I missed something. See, Relacy is a good thing.
Might have some more time to test it later on tonight. If it fails, shit
happens! ;^)
Technical Details
Message-ID:
<11f1uik$3ave0$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11evm5c$27kcm$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> Newsgroups: comp.lang.c++ Subject: Re: FtxSema is a binary semaphore, not a general one (and a counting futex version) Date: Sat, 5 Dec 2026 13:09:37 -0800 Organization: A noiseless patient Spider Lines: 74 Message-ID: <11f1uik$3ave0$1@dont-email.me> References: <11evm5c$27kcm$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Sat, 05 Dec 2026 21:09:41 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3505600"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/1buYt+4imMcCyavHJmxF1q5ydNSqEy0A="; posting-host="2b66e3b9720a5537939852d7aa1235e3" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:CkFqv4o5uO7Mx5X6KFPrg3PVj6E= sha256:uLYS387ESSmFcqEm4fA7/3fE36XmvJRxC6E66YSwZI0= sha1:xB8XmR9xupufs0ya1VfkZYPIcKo= sha256:2e0i+O56nZuF+COHJ8qStfyZGSOpmqV8DEfJf/bWjak= In-Reply-To: <11evm5c$27kcm$1@dont-email.me> Content-Language: en-US