// xsemaphore.h
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
Am 14.12.2025 um 22:15 schrieb Chris M. Thomasson:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
There's no architectural description for this.
On 12/14/2025 9:12 PM, Bonita Montero wrote:
Am 14.12.2025 um 22:15 schrieb Chris M. Thomasson:What do you mean?
On 12/14/2025 10:00 AM, Bonita Montero wrote:There's no architectural description for this.
// xsemaphore.h[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
Am 15.12.2025 um 21:50 schrieb Chris M. Thomasson:
On 12/14/2025 9:12 PM, Bonita Montero wrote:
Am 14.12.2025 um 22:15 schrieb Chris M. Thomasson:What do you mean?
On 12/14/2025 10:00 AM, Bonita Montero wrote:There's no architectural description for this.
// xsemaphore.h[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I'm interested in the implementation, not how to use it.
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system, but it
was a mutex rather than a semaphore paired with an atomic counter.-a The result was massively faster than the original mutex.
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system, but
it was a mutex rather than a semaphore paired with an atomic counter.
The result was massively faster than the original mutex.
Can you clarify a bit? A benaphore can be used as a mutex. But, it turns
it into a bakery type algo...
On 16/12/2025 21:12, Chris M. Thomasson wrote:
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system,
but it was a mutex rather than a semaphore paired with an atomic
counter.-a The result was massively faster than the original mutex.
Can you clarify a bit? A benaphore can be used as a mutex. But, it
turns it into a bakery type algo...
Mutexes and semaphores are different mechanisms for different uses.
Terminology varies, and I don't know of any kind of "official" definitions.-a So I am just going to describe the terms as I am used to
them from the RTOS's and systems I am familiar with.
A semaphore is a signalling mechanism, and can be used to synchronise between threads and control waking and sleeping. Critical to semaphore behaviour is that they are counters that can be incremented or
decremented by different threads - producer threads increment the
counters, and consumer threads decrement them.-a A consumer that tries
to decrement a semaphore that is currently at zero, blocks.-a When a producer increments a semaphore from zero, one or more sleeping
threads may be woken.-a A binary semaphore is just a semaphore that
should not be incremented past 1.
A "benephore", as I understand it, is an atomic counter paired with a
binary semaphore.-a Instead of using potentially costly OS-level
increment and decrement operation for all changes to the semaphore
count, it uses cheap atomic operations except in the cases of changes between 0 and 1 - those use the OS semaphore mechanisms so that
threads can be blocked or woken as appropriate.
A mutex is a resource control mechanism for handling exclusive access
to a protected resource (data structure, hardware, whatever).-a When a thread locks the mutex, any other thread attempting to lock the same
mutex will block until the first thread releases the mutex.-a Locked
mutex's have an owner, can only be released by that owner.-a
(Semaphores have no owner and can be incremented and decremented by
any thread.)-a In an RTOS, you also usually have some kind of priority inheritance system - if a high priority thread tries to take a mutex
that is currently locked by a low priority thread, that low priority
thread gets temporarily boosted to a high priority level so that the
real high priority task does not wait too long.
So mutex's and semaphores are very different types of object, used for
very different purposes - even though their implementations may share
a lot of common code.-a (In FreeRTOS, for example, they are both specialisations of the same structure - a message queue.)
The "frutex" (fast recursive mutex) mechanism I used was build from an atomic state variable, a counter for recursive use in the same thread,
an owner variable, and a mutex.-a Here the mutex is normally in the
locked state, and the atomic state variable was normally 0 (unlocked).
When a thread wants to lock the frutex, it first checks if it is the
current owner - if so, it can increment the recursion counter (without
even needing atomic accesses) and return "locked".-a Otherwise, inside
a critical section (much easier in a single processor embedded system)
it checks if the frutex state is free - if so, set it to locked, set
the owner to the current thread, set the recursive counter to 1, and
exit the critical section and the "lock" function.-a If the state
variable shows that the lock is not free, then increment the state
(which then tracks the number of waiting threads), transfer the
ownership of the OS-level mutex to the current owner of the frutex,
and exit the critical section (but not the lock function - we still
don't have the lock).-a Now we do a normal blocking OS lock of the
mutex, and when we get it we set the frutex owner to ourself and the
count to 1.-a Unlocking is mostly simple - if we own the frutex
recursively, decrement the counter.-a If no one else is waiting for the
lock (the state is 1), clear the owner and set the state to 0.-a But if there is one or more thread waiting (state is greater than 1),
decrement the state and release the OS mutex.-a That will immediately
wake a thread that is waiting for it.
All this means that the common case - taking and releasing the lock
with there is no contention - is done with a few reads and writes of variables within a critical section.-a (Critical sections are cheap on
small single-core embedded systems.)-a Actual OS mutex handling, along
with thread priority changes and potential scheduler calls, is only
used when required to handle contention.
On 16/12/2025 21:12, Chris M. Thomasson wrote:
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system, but
it was a mutex rather than a semaphore paired with an atomic counter.
The result was massively faster than the original mutex.
Can you clarify a bit? A benaphore can be used as a mutex. But, it
turns it into a bakery type algo...
Mutexes and semaphores are different mechanisms for different uses.
On 12/16/2025 4:44 AM, Bonita Montero wrote:
Am 15.12.2025 um 21:50 schrieb Chris M. Thomasson:
On 12/14/2025 9:12 PM, Bonita Montero wrote:
Am 14.12.2025 um 22:15 schrieb Chris M. Thomasson:What do you mean?
On 12/14/2025 10:00 AM, Bonita Montero wrote:There's no architectural description for this.
// xsemaphore.h[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I'm interested in the implementation, not how to use it.
Well, the implementation of it only uses fetch-and-add, (XADD) over in x86/x64 world. Also, its loopless.
On 12/17/2025 1:31 AM, David Brown wrote:
On 16/12/2025 21:12, Chris M. Thomasson wrote:
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system,
but it was a mutex rather than a semaphore paired with an atomic
counter. The result was massively faster than the original mutex.
Can you clarify a bit? A benaphore can be used as a mutex. But, it
turns it into a bakery type algo...
Mutexes and semaphores are different mechanisms for different uses.
Indeed.
[...]
Distributed mutex? Yes, I know about them. There were some interesting
algos back in the day. It's going to be hard to try to find my old
threads on comp.arch, sigh.
One point, recursive locks are, well, I personally hate them... I had to debug a case where somebody was using recursive locks and I had to artificially step and pause threads to a point where I found the
deadlock. Tedious, uggg. A single thread was deadlocked several
recursions down. What a nightmare. Anyway, I digress.
On 12/17/2025 1:31 AM, David Brown wrote:
On 16/12/2025 21:12, Chris M. Thomasson wrote:
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system,
but it was a mutex rather than a semaphore paired with an atomic
counter. The result was massively faster than the original mutex.
Can you clarify a bit? A benaphore can be used as a mutex. But, it
turns it into a bakery type algo...
Mutexes and semaphores are different mechanisms for different uses.
Indeed.
[...]
Distributed mutex? Yes, I know about them. There were some interesting
algos back in the day. It's going to be hard to try to find my old
threads on comp.arch, sigh.
One point, recursive locks are, well, I personally hate them... I had to debug a case where somebody was using recursive locks and I had to artificially step and pause threads to a point where I found the
deadlock. Tedious, uggg. A single thread was deadlocked several
recursions down. What a nightmare. Anyway, I digress.
On 17/12/2025 21:48, Chris M. Thomasson wrote:
On 12/17/2025 1:31 AM, David Brown wrote:
On 16/12/2025 21:12, Chris M. Thomasson wrote:
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system,
but it was a mutex rather than a semaphore paired with an atomic
counter. The result was massively faster than the original mutex.
Can you clarify a bit? A benaphore can be used as a mutex. But, it
turns it into a bakery type algo...
Mutexes and semaphores are different mechanisms for different uses.
Indeed.
[...]
Distributed mutex? Yes, I know about them. There were some interesting
algos back in the day. It's going to be hard to try to find my old
threads on comp.arch, sigh.
One point, recursive locks are, well, I personally hate them... I had
to debug a case where somebody was using recursive locks and I had to
artificially step and pause threads to a point where I found the
deadlock. Tedious, uggg. A single thread was deadlocked several
recursions down. What a nightmare. Anyway, I digress.
Certainly recursive locks can make some things harder to understand -
but they can also make it easier to make well-structured code if you
need locking at different levels.
In my case, the locks were for protecting access to a bus that used
indirect access.-a That is, instead of a nice, simple "read data from address X" instruction, you would first put address X into one register,
put the "read" command into another register, and then read the data register.-a And clearly it would be a bad idea if another thread mixed in its own accesses to the bus at the same time.-a Thus the "read_data_reg" function would take this lock, handle the indirect access, then release
the lock.
But sometimes I needed to read (or write) multiple registers in an
atomic unit.-a So the code for that would take the lock, do the "read_data_reg" or "write_data_reg" operations, then release the lock.
If the lock were not recursive, I'd have to duplicate code with "read_data_reg_already_locked" functions, and so on.
There are, of course, plenty of alternative setups - templates and overloaded functions could avoid duplication of source code when making "already locked" and "needs locks" variants of the functions.-a The functions could use a std::optional<> wrapping a lock guard, which could
be created when needed while still being sure it got released at the
right point.-a Ultimately, however, you have to track whether you have
got the lock already or whether you need it, and a recursive mutex does
that for you.-a Any alternative is likely to be at least as difficult to follow in the source code.
And since this is all for a real-time embedded system, speed is
important - and in particular, consistency and maximum time is
important.-a So you don't use new and delete (or any dynamic memory), you don't use varying backoff times, you don't use spinlocks (on a single process system, a spinlock just kills the system), and you reduce the overheads that you can.
On 12/18/2025 12:13 AM, David Brown wrote:
On 17/12/2025 21:48, Chris M. Thomasson wrote:
On 12/17/2025 1:31 AM, David Brown wrote:
On 16/12/2025 21:12, Chris M. Thomasson wrote:
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html
I implemented something similar in an RTOS on an embedded system, >>>>>> but it was a mutex rather than a semaphore paired with an atomic
counter. The result was massively faster than the original mutex.
Can you clarify a bit? A benaphore can be used as a mutex. But, it
turns it into a bakery type algo...
Mutexes and semaphores are different mechanisms for different uses.
Indeed.
[...]
Distributed mutex? Yes, I know about them. There were some
interesting algos back in the day. It's going to be hard to try to
find my old threads on comp.arch, sigh.
One point, recursive locks are, well, I personally hate them... I had
to debug a case where somebody was using recursive locks and I had to
artificially step and pause threads to a point where I found the
deadlock. Tedious, uggg. A single thread was deadlocked several
recursions down. What a nightmare. Anyway, I digress.
Certainly recursive locks can make some things harder to understand -
but they can also make it easier to make well-structured code if you
need locking at different levels.
Well, yeah. I am a bit biased because of all the recursive nightmare
code from others that I had to debug in the past. One bad one was a
server loop that locked its per-socket state then did a callback into
user code that would call into per-socket functions that would take the
damn lock again. A deadlock would occur around say, 7 recursions deep.
Sigh!
On 19/12/2025 01:45, Chris M. Thomasson wrote:
On 12/18/2025 12:13 AM, David Brown wrote:
On 17/12/2025 21:48, Chris M. Thomasson wrote:
On 12/17/2025 1:31 AM, David Brown wrote:
On 16/12/2025 21:12, Chris M. Thomasson wrote:
On 12/16/2025 11:54 AM, David Brown wrote:
On 14/12/2025 22:15, Chris M. Thomasson wrote:
On 12/14/2025 10:00 AM, Bonita Montero wrote:I implemented something similar in an RTOS on an embedded system, >>>>>>> but it was a mutex rather than a semaphore paired with an atomic >>>>>>> counter. The result was massively faster than the original mutex. >>>>>>>
// xsemaphore.h
[...]
What about the simple semaphore?
https://www.haiku-os.org/legacy-docs/benewsletter/Issue1-26.html >>>>>>>
Can you clarify a bit? A benaphore can be used as a mutex. But, it >>>>>> turns it into a bakery type algo...
Mutexes and semaphores are different mechanisms for different uses.
Indeed.
[...]
Distributed mutex? Yes, I know about them. There were some
interesting algos back in the day. It's going to be hard to try to
find my old threads on comp.arch, sigh.
One point, recursive locks are, well, I personally hate them... I
had to debug a case where somebody was using recursive locks and I
had to artificially step and pause threads to a point where I found
the deadlock. Tedious, uggg. A single thread was deadlocked several
recursions down. What a nightmare. Anyway, I digress.
Certainly recursive locks can make some things harder to understand -
but they can also make it easier to make well-structured code if you
need locking at different levels.
Well, yeah. I am a bit biased because of all the recursive nightmare
code from others that I had to debug in the past. One bad one was a
server loop that locked its per-socket state then did a callback into
user code that would call into per-socket functions that would take
the damn lock again. A deadlock would occur around say, 7 recursions
deep. Sigh!
Locks should be held around as small an area as possible (both in terms
of time, and code) - and you certainly should not be calling unknown external code while holding a lock.
The people behind that particular
bit of software architecture would probably have made an even more incomprehensible and unreliable solution if they did not have recursive locks - such as using a home-made recursive mutex using a non-recursive
lock and a counter without appropriate care for ordering and atomicity.
When you are feeling frustrated and annoyed dealing with bad code,
remember it could always have been worse :-)
| Sysop: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 54 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 19:20:00 |
| Calls: | 742 |
| Files: | 1,218 |
| D/L today: |
5 files (8,203K bytes) |
| Messages: | 184,913 |
| Posted today: | 1 |