I wanted to write a semaphore with a futex. I incremented a counter
and did the notify. Oh the other side I did a wait and after the wait
I do a decrement of the counter if it was non-zero, otherwise I wait
again.
The problem with that is that is that I want to wake up another thread
and immediatly wait on the same semaphore on the notifying side it's
very likely that the notifying thread eats up its own notify and the
other thread doesn't consume the notify. This doesn't happen with com- pletely kernel-managed semaphores.
So it is possible to write a semaphore with a futex without this limi-
tation ?
And: Chris, this isn't a question eligible for short-circuited toughts.
Fwiw, why not use a kernel semaphore as a slow path? Is this just for fun?
Am 10.11.2026 um 22:32 schrieb Chris M. Thomasson:
Fwiw, why not use a kernel semaphore as a slow path? Is this just for
fun?
Because signalling or waiting on a semaphore can fail, whereas a futex
only has invalid parameters error codes. I need a solution that is like
a futes in that sense.
Am 10.11.2026 um 22:32 schrieb Chris M. Thomasson:
Fwiw, why not use a kernel semaphore as a slow path? Is this just for
fun?
Because signalling or waiting on a semaphore can fail, whereas a futex
only has invalid parameters error codes. I need a solution that is like
a futes in that sense.
I see. A fun failure on a sem_wait is EINTR.
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar function frankly deserves what they get.
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar
function
frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
push_back, pretty much must use compare-and-swap the only atomic
primitive on the chip, though for example with having a buffer and using conditional-move after compare-and-swap to copy items into the buffer
then appending multiple items from the buffer atomically. Then, the
consumer, should have a non-blocking peek() and pop(), peek_front and pop_front, that should return an error if EWOULDBLOCK, then that it can
park in a syscall when the queue's empty, so that it idles in park with
0% CPU. Then pushing items onto the queue should change it back to non-blocking.
So, when it's running it should be un-blocked, and appends must be non-blocking, when it's empty, then it should block to "park".
In C and C++ and in POSIX, how's this.
On 11/12/2026 11:35 AM, Ross Finlayson wrote:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar
function
frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
A 1000? You have a 1000 cores handy? ;^)
Fwiw, a MPSC LIFO is pretty straightforward. A CAS for the push and a
single XCHG for the pop. Adding multiple nodes is easy. push the head
and tail.
Now. For a FIFO reversing the XCHG, done. Now, there is the classic MPMC lock free queue. Iirc, I think there is a way to specialize it on the
single consumer case.
To make this be able to wait, ahh... The wonderful eventcount comes into play here. It can be created with a futex or other means. Its basically
a "condition variable" for lock/wait-free.
Have you ever used them before?
push_back, pretty much must use compare-and-swap the only atomic
primitive on the chip, though for example with having a buffer and using
conditional-move after compare-and-swap to copy items into the buffer
then appending multiple items from the buffer atomically. Then, the
consumer, should have a non-blocking peek() and pop(), peek_front and
pop_front, that should return an error if EWOULDBLOCK, then that it can
park in a syscall when the queue's empty, so that it idles in park with
0% CPU. Then pushing items onto the queue should change it back to
non-blocking.
So, when it's running it should be un-blocked, and appends must be
non-blocking, when it's empty, then it should block to "park".
In C and C++ and in POSIX, how's this.
On 11/12/2026 11:59 AM, Chris M. Thomasson wrote:
On 11/12/2026 11:35 AM, Ross Finlayson wrote:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar
function
frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
A 1000? You have a 1000 cores handy? ;^)
Fwiw, a MPSC LIFO is pretty straightforward. A CAS for the push and a
single XCHG for the pop. Adding multiple nodes is easy. push the head
and tail.
I should clarify that a single XCHG for the pop is basically flushing
the whole list. But its easy. So, if that works for you fine. Now, this
can get almost "too" much work for a thread that pops a huge number of
nodes. But, we can distribute the MPSC to try to balance the load. Using
a single XCHG for the single consumer is basically the best we can get.
Now. For a FIFO reversing the XCHG, done. Now, there is the classic
MPMC lock free queue. Iirc, I think there is a way to specialize it on
the single consumer case.
To make this be able to wait, ahh... The wonderful eventcount comes
into play here. It can be created with a futex or other means. Its
basically a "condition variable" for lock/wait-free.
Have you ever used them before?
push_back, pretty much must use compare-and-swap the only atomic
primitive on the chip, though for example with having a buffer and using >>> conditional-move after compare-and-swap to copy items into the buffer
then appending multiple items from the buffer atomically. Then, the
consumer, should have a non-blocking peek() and pop(), peek_front and
pop_front, that should return an error if EWOULDBLOCK, then that it can
park in a syscall when the queue's empty, so that it idles in park with
0% CPU. Then pushing items onto the queue should change it back to
non-blocking.
So, when it's running it should be un-blocked, and appends must be
non-blocking, when it's empty, then it should block to "park".
In C and C++ and in POSIX, how's this.
On 11/12/2026 12:05 PM, Chris M. Thomasson wrote:
On 11/12/2026 11:59 AM, Chris M. Thomasson wrote:
On 11/12/2026 11:35 AM, Ross Finlayson wrote:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar
function
frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
A 1000? You have a 1000 cores handy? ;^)
Fwiw, a MPSC LIFO is pretty straightforward. A CAS for the push and a
single XCHG for the pop. Adding multiple nodes is easy. push the head
and tail.
I should clarify that a single XCHG for the pop is basically flushing
the whole list. But its easy. So, if that works for you fine. Now, this
can get almost "too" much work for a thread that pops a huge number of
nodes. But, we can distribute the MPSC to try to balance the load. Using
a single XCHG for the single consumer is basically the best we can get.
Now. For a FIFO reversing the XCHG, done. Now, there is the classic
MPMC lock free queue. Iirc, I think there is a way to specialize it on
the single consumer case.
To make this be able to wait, ahh... The wonderful eventcount comes
into play here. It can be created with a futex or other means. Its
basically a "condition variable" for lock/wait-free.
Have you ever used them before?
push_back, pretty much must use compare-and-swap the only atomic
primitive on the chip, though for example with having a buffer and
using
conditional-move after compare-and-swap to copy items into the buffer
then appending multiple items from the buffer atomically. Then, the
consumer, should have a non-blocking peek() and pop(), peek_front and
pop_front, that should return an error if EWOULDBLOCK, then that it can >>>> park in a syscall when the queue's empty, so that it idles in park with >>>> 0% CPU. Then pushing items onto the queue should change it back to
non-blocking.
So, when it's running it should be un-blocked, and appends must be
non-blocking, when it's empty, then it should block to "park".
In C and C++ and in POSIX, how's this.
Hm. There are 1000 "logical" producers, subjects, say, of a topic,
from 3 or 4 threads, and it's usually figured that these will all
be pinned to one core, since the idea is that the entire process they're
in has thusly affinity of their memory, and that the process-group
scales in processes about kernel resources per-process
and these kinds of things.
It's figured that items are 64B or 128B, i.e., cache-lines or cache-lines-pairs size, and that the heap backing the queue is of 4KiB
i.e. PAGE_SIZE size pages. Or, the items may be larger, yet, fixed-size.
It's only a FIFO, so, there's only push (push_back) and pop (pop_front) relevant.
The queue may grow or is unbounded, bounded by RAM, in user-space,
it's not a ring buffer.
When there's activity on the system, the queue will likely be non-empty,
yet when the activity is low, it can go in and out of idle.
When the activity is idle, it shall block in park and not be scheduled
by the CPU, the single thread that consumes it.
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar function >> frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(), >push_back, pretty much must use compare-and-swap the only atomic
On Thu, 12 Nov 2026 11:35:24 -0800
Ross Finlayson <ross.a.finlayson@gmail.com> gabbled:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar
function
frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
push_back, pretty much must use compare-and-swap the only atomic
Blah blah.
Sorry, what's your point exactly? That people shouldn't bother reading the man page about a function or whatever other documentation is available
before
they try to use it?
On 11/13/2026 02:07 AM, boltar@caprica.universe wrote:
On Thu, 12 Nov 2026 11:35:24 -0800
Ross Finlayson <ross.a.finlayson@gmail.com> gabbled:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar
function
frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
push_back, pretty much must use compare-and-swap the only atomic
Blah blah.
Sorry, what's your point exactly? That people shouldn't bother reading
the
man page about a function or whatever other documentation is available
before
they try to use it?
Ah, good old linear thinking.
Basically looking to make sense of the great circle of things,
of course a lot depends on "the stability of fact", vis-a-vis
something like Magritte's "The Treachery of Images" vis-a-vis
"The Empire of Light", point being that I've already read the
man pages most of them. (Man pages, RFC's, standards & specifications,
like from ITU and ISO.)
So, my point is a sort of retro approach to simple and practical
systems, to live in a world of modern systems, since the logical
and abstract is about things like "C, C++, and POSIX", in a world
of "many-core hyper-virtualized containerized boxes", and about
things like "Internet protocols" and "distributed systems".
The idea is to write what are of the good old specifications
in the good old code, with modern expectations of the runtime.
Then, the code should also be well-designed and written from
the beginning to cover the relevant surface of the runtime,
while having the nice abstraction and these sorts of things.
On 11/12/2026 11:59 AM, Chris M. Thomasson wrote:
On 11/12/2026 11:35 AM, Ross Finlayson wrote:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
A 1000? You have a 1000 cores handy? ;^)
Fwiw, a MPSC LIFO is pretty straightforward. A CAS for the push and a
single XCHG for the pop. Adding multiple nodes is easy. push the head
and tail.
I should clarify that a single XCHG for the pop is basically flushing
the whole list. But its easy. So, if that works for you fine. Now, this
can get almost "too" much work for a thread that pops a huge number of nodes. But, we can distribute the MPSC to try to balance the load. Using
a single XCHG for the single consumer is basically the best we can get.
Now. For a FIFO reversing the XCHG, done. Now, there is the classic
MPMC lock free queue. Iirc, I think there is a way to specialize it on
the single consumer case.
On 11/13/2026 02:07 AM, boltar@caprica.universe wrote:
Sorry, what's your point exactly? That people shouldn't bother reading the >> man page about a function or whatever other documentation is available
before
they try to use it?
Ah, good old linear thinking.
Basically looking to make sense of the great circle of things,
of course a lot depends on "the stability of fact", vis-a-vis
something like Magritte's "The Treachery of Images" vis-a-vis
On 11/12/26 3:05 PM, Chris M. Thomasson wrote:
On 11/12/2026 11:59 AM, Chris M. Thomasson wrote:
On 11/12/2026 11:35 AM, Ross Finlayson wrote:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
A 1000? You have a 1000 cores handy? ;^)
Fwiw, a MPSC LIFO is pretty straightforward. A CAS for the push and a
single XCHG for the pop. Adding multiple nodes is easy. push the head
and tail.
I should clarify that a single XCHG for the pop is basically flushing
the whole list. But its easy. So, if that works for you fine. Now,
this can get almost "too" much work for a thread that pops a huge
number of nodes. But, we can distribute the MPSC to try to balance the
load. Using a single XCHG for the single consumer is basically the
best we can get.
You don't need to do an XCHG.-a You have a single consumer.-a It owns the queue.-a It just needs to do a load of the queue tail pointer and just process the queue.-a Of course it needs to keep track of things like the
last tail pointer value it read so it knows when it has processed all
the new nodes.-a It may also want to defer deallocating the node
referenced by the tail pointer to avoid the ABA problem.
You could do the XCHG if you wanted a way for the consumer to indicate
it was going to block and needed to be woken up.-a There are other
ways that don't need this logic so it's a design choice.
Now. For a FIFO reversing the XCHG, done. Now, there is the classic
MPMC lock free queue. Iirc, I think there is a way to specialize it
on the single consumer case.
It's unlikely anyone has program logic that requires FIFO, what little
of it is actually observable.-a You really care about forward progress guarantees.-a As long as the consumer keeps up with the producers, you
should be ok doing batches in LIFO order.-a If not, you have other
problems.
And if you drop the FIFO requirement then you could just have multiple consumers doing XCHG on the queue tail pointer.
On 11/12/2026 12:05 PM, Chris M. Thomasson wrote:
On 11/12/2026 11:59 AM, Chris M. Thomasson wrote:
On 11/12/2026 11:35 AM, Ross Finlayson wrote:
On 11/12/2026 08:41 AM, boltar@caprica.universe wrote:
On Wed, 11 Nov 2026 15:05:43 -0800
"Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> gabbled:
On 11/11/2026 2:47 PM, Bonita Montero wrote:
Am 11.11.2026 um 23:12 schrieb Chris M. Thomasson:
I see. A fun failure on a sem_wait is EINTR.
That's not a out of recources error and it is recoverable.
Its scary though when people fail to know about it.
Anyone who doesn't read the man page before they use an unfamiliar
function
frankly deserves what they get.
Here's one for you.
So, I want a MPSC queue, multiple-producer single-consumer queue,
producers on the order of 1000, with that appends to the queue push(),
A 1000? You have a 1000 cores handy? ;^)
Fwiw, a MPSC LIFO is pretty straightforward. A CAS for the push and a
single XCHG for the pop. Adding multiple nodes is easy. push the head
and tail.
I should clarify that a single XCHG for the pop is basically flushing
the whole list. But its easy. So, if that works for you fine. Now, this
can get almost "too" much work for a thread that pops a huge number of
nodes. But, we can distribute the MPSC to try to balance the load. Using
a single XCHG for the single consumer is basically the best we can get.
Now. For a FIFO reversing the XCHG, done. Now, there is the classic
MPMC lock free queue. Iirc, I think there is a way to specialize it on
the single consumer case.
To make this be able to wait, ahh... The wonderful eventcount comes
into play here. It can be created with a futex or other means. Its
basically a "condition variable" for lock/wait-free.
Have you ever used them before?
push_back, pretty much must use compare-and-swap the only atomic
primitive on the chip, though for example with having a buffer and
using
conditional-move after compare-and-swap to copy items into the buffer
then appending multiple items from the buffer atomically. Then, the
consumer, should have a non-blocking peek() and pop(), peek_front and
pop_front, that should return an error if EWOULDBLOCK, then that it can >>>> park in a syscall when the queue's empty, so that it idles in park with >>>> 0% CPU. Then pushing items onto the queue should change it back to
non-blocking.
So, when it's running it should be un-blocked, and appends must be
non-blocking, when it's empty, then it should block to "park".
In C and C++ and in POSIX, how's this.
Hm. There are 1000 "logical" producers, subjects, say, of a topic,
from 3 or 4 threads, and it's usually figured that these will all
be pinned to one core, since the idea is that the entire process they're
in has thusly affinity of their memory, and that the process-group
scales in processes about kernel resources per-process
and these kinds of things.
It's figured that items are 64B or 128B, i.e., cache-lines or cache-lines-pairs size, and that the heap backing the queue is of 4KiB
i.e. PAGE_SIZE size pages. Or, the items may be larger, yet, fixed-size.
It's only a FIFO, so, there's only push (push_back) and pop (pop_front) relevant.
The queue may grow or is unbounded, bounded by RAM, in user-space,
it's not a ring buffer.
When there's activity on the system, the queue will likely be non-empty,
yet when the activity is low, it can go in and out of idle.
When the activity is idle, it shall block in park and not be scheduled
by the CPU, the single thread that consumes it.
And if you drop the FIFO requirement then you could just have multiple consumers doing XCHG on the queue tail pointer.
It's unlikely anyone has program logic that requires FIFO, what little
of it is actually observable.-a You really care about forward progress guarantees.-a As long as the consumer keeps up with the producers, you
should be ok doing batches in LIFO order.-a If not, you have other
problems.
And if you drop the FIFO requirement then you could just have multiple consumers doing XCHG on the queue tail pointer.
On 11/13/2026 6:44 AM, Joseph Seigh wrote:
[...]
I have not had time to search for your interesting old cancel method for
the fast semaphore on c.p.t, basically a benaphore iirc. Can you find it?
On 11/14/26 6:16 PM, Chris M. Thomasson wrote:
On 11/13/2026 6:44 AM, Joseph Seigh wrote:
[...]
I have not had time to search for your interesting old cancel method
for the fast semaphore on c.p.t, basically a benaphore iirc. Can you
find it?
Probably the one using bits 63-1 as the count and bit 0 as a cancel
flag, 1 = active, 0 = canceled.
Who uses semaphores?-a They're not
especially useful.
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 76 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 54:21:22 |
| Calls: | 1,265 |
| Calls today: | 1 |
| Files: | 1,362 |
| D/L today: |
3 files (2,684K bytes) |
| Messages: | 285,563 |