🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2169

Re: Why ordered mutexed don't work ...

#2169
From: Ross Finlayson
Date: Fri, 04 Dec 2026 08:25
224 lines
11749 bytes
On 12/03/2026 01:52 PM, Chris M. Thomasson wrote:
> On 12/3/2026 8:48 AM, Ross Finlayson wrote:
>> On 12/03/2026 08:19 AM, Ross Finlayson wrote:
>>> On 12/02/2026 10:23 AM, Ross Finlayson wrote:
>>>> On 12/02/2026 09:06 AM, Kaz Kylheku wrote:
>>>>> On 2026-12-02, Bonita Montero <Bonita.Montero@gmail.com> wrote:
>>>>>> Just for fun, I tried writing a mutex today that wakes up threads
>>>>>> in the same order they began waiting. I thought I was finished, but
>>>>>> while cycling into the old town for a meal, it occurred to me: I can
>>>>>> control the order in which threads start waiting, but I cannot
>>>>>> control
>>>>>> the order in which the scheduler wakes them up.
>>>>>
>>>>> The problem is racing between the waiting thread which your mutex
>>>>> algorithm chose to wake up (e.g. FIFO) and one or more non-waiting
>>>>> threads that just happened to come along trying to get the mutex.
>>>>>
>>>>> If you want strict order, you have to eliminate the possibility of
>>>>> creating a time window where a running thread can opportunistically
>>>>> grab
>>>>> a mutex that you're trying to pass to the next waiting thread.
>>>>>
>>>>>> Furthermore, if there
>>>>>> isn’t a free CPU core available, there’s no guarantee that a thread
>>>>>> will be woken up immediately.
>>>>>
>>>>> Yes? And what is "immediately" anyway? Even if a thread is woken
>>>>> immediately, it doesn't immediately get into the application code; it
>>>>> has to run some instructions of the system before coming out of the
>>>>> mutex wait. So if "immediately" means something like "within 10
>>>>> nanoseconds of being given the mutex, the thread must execute the
>>>>> first
>>>>> instruction guarded by the mutex", you will not hit that even if a
>>>>> free
>>>>> core is available.
>>>>>
>>>>> The partial solution for the requirement of something having to
>>>>> execute
>>>>> immediately even if no core is available is priority.  If something is
>>>>> so important that it must execute immediately even if the schedule is
>>>>> full, if it has a higher priority than at least one running task,
>>>>> it can
>>>>> take the core away from that task.
>>>>>
>>>>> You must then provision the system so that you don't ever have n + k
>>>>> highest priority tasks competing for n processors.
>>>>>
>>>>> Also remember that interrupts can take away the processor from
>>>>> threads.
>>>>>
>>>>> (It is possible to create a priority scheme whereby some threads
>>>>> can be assigned higher priority than some interrupts, such that
>>>>> when thos threads are running, lower priority interrupts are masked.
>>>>> I have first hand experience of getting something like that working
>>>>> and solving a real problem with it.)
>>>>>
>>>>>> Once I got home, well-fed, I deleted
>>>>>> the entire project.
>>>>>> In any case, no operating system offers such mutexes, and runtime
>>>>>> environments don’t provide them either. I suspect I’m not the first
>>>>>> person to come up with this idea.
>>>>>
>>>>> Tony Hoare's "monitors" had strict semantics w.r.t. which waiting
>>>>> thread gets into the monitor.
>>>>>
>>>>> Semaphores will also give you this. If a semaphore is signaled and one
>>>>> or more threads are waiting, the signal operation chooses one of
>>>>> them to
>>>>> proceed; the semaphore can't be "sniped" by some already executing
>>>>> thread.
>>>>>
>>>>
>>>>
>>>> One imagines that if there are a fixed number of threads turn_takers,
>>>> say, half as many as bits in a compare-exchange or user-space atomic
>>>> word, for example 32-bits, that threads get a number 0-15 turn_id, to
>>>> split the word into bit-flags of "took_turn" and a serially incremented
>>>> "turns_taken", then if there's a system-call "yield" that basically
>>>> only intends to mark a thread as ephemerally un-ready since
>>>> it's not its turn, then if the took-turn is zero and the turns-taken %
>>>> turn_takers == turn_id, then to increment turns_taken and make
>>>> turns_taken % turn_takers, and clear took_turn << turn_id, then though
>>>> would involve a discipline of setting turn_id's bit before entering any
>>>> wait, marking that it already took its turn, on that particular
>>>> kernel-space lock or mutex as it would be to work with the O.S.
>>>> scheduler, makes for a co-operative sort of account of round-robin.
>>>>
>>>> It's sometimes said "co-operative multi-threading is easy to do right
>>>> and somebody will do it wrong".
>>>>
>>>> That a semaphore would be "fair" or "FIFO" is usually called "strong",
>>>> also it's usually not the way of commodity (i.e., PC) operating
>>>> systems.
>>>>
>>>>
>>>> I just learned about fast user-space mutex, yet only want the best
>>>> for my threads, best anti-starvation being fair and a strong semaphore.
>>>>
>>>>
>>>> union {
>>>>    uint32_t u;
>>>>    struct {
>>>>      unsigned took_turn_00:1;
>>>>      unsigned took_turn_01:1;
>>>>      unsigned took_turn_02:1;
>>>>      unsigned took_turn_03:1;
>>>>      unsigned took_turn_04:1;
>>>>      unsigned took_turn_05:1;
>>>>      unsigned took_turn_06:1;
>>>>      unsigned took_turn_07:1;
>>>>      unsigned took_turn_08:1;
>>>>      unsigned took_turn_09:1;
>>>>      unsigned took_turn_10:1;
>>>>      unsigned took_turn_11:1;
>>>>      unsigned took_turn_12:1;
>>>>      unsigned took_turn_13:1;
>>>>      unsigned took_turn_14:1;
>>>>      unsigned took_turn_15:1;
>>>>      uint8_t turns_taken;
>>>>    } s;
>>>> } userspace_turn_tracker_t;
>>>>
>>>>
>>>> Then, if the operating system was somehow pathological and for whatever
>>>> reason the "yield_turn", say, marking a thread as simply yet only
>>>> ephemerlaly un-ready (suggestions?), kept scheduling threads when
>>>> it's not their turn, makes for a "turns_not_taken" then constantly
>>>> incrementing that and taking turns out-of-turn that not solving the
>>>> problem yet kicking it down the road. It's figured though that
>>>> the operating system will schedule ready threads before threads marking
>>>> themselves as unready with some "yield_turn".
>>>>
>>>>
>>>>
>>>>
>>>
>>>
>>> Haven't found "yield_turn" per se yet there's "nanosleep(1)",
>>> thus probably if you've ever seen "nanosleep(1)" it's a thing.
>>>
>>> This sort of approach causes churn and puts contention on
>>> the scheduler instead of on the lock, yet, helps prevent
>>> pathologies in scheduling from favoring tasks that often
>>> have very short duty-cycles, thus appearing "efficient".
>>>
>>>
>>>
>>> So, then looking to "CAS", "volatile", and "nanosleep"
>>> to make for a brief model of co-operative turn-taking,
>>> then has that for example if there are more than 16 threads,
>>> to make the top a thread-group and underneath it 16 threads
>>> in a thread-group and so on, for example in powers of 16,
>>> then though that vending the thread a serial ID, gets into
>>> the challenge of the "synchronize, unique: same" and
>>> "synchronize, unique: different" again, about that being
>>> basically an abstract intuitive account of inversions
>>> like a potentialistic setting, about what it means for
>>> two clocks to have the same time.
>>>
>>> I'd be looking to "volatile" for free reads, then having
>>> usually a publish-protocol or "root pointer", i.e.
>>> a pointer-pointer, that writers just update under
>>> the root-pointer or root-token and readers always
>>> check that since it's volatile, then that readers
>>> should _copy_ the relevant datum they're to read
>>> so that if it goes away it doesn't matter since
>>> they have their own copy.
>>>
>>>
>>
>>
>> About RCU and basically having that readers checkout
>> their own copy instead of taking a reader lock and
>> basically locking-the-loo and tallying the comings
>> and goings and leaving the writer and generational
>> machinery to keep track of things, i.e. "not RCU",
>> basically has whether reads are large or reads are
>> small, large meaning an iterator is valid and small
>> meaning a datum is fungible atomically, makes for
>> various accounts of "one-size-fits-all" having
>> at least two accounts of what size that is.
> [...]
>
> Fwiw, RCU can be used to iterate a shared linked list directly. Just
> iterate all of the nodes. The nodes do not need to be copied or
> anything. Just flow through the pointers.


Thanks for writing. Yeah I notice that a lot of data-structures
have the idea to describe themselves as "RCU-safe", which basically
means that paths through the data-structures can be
editioned/revisioned, so that old & new can read through,
then though that "one-size-fits-all" is going to vary.

Then, how the data-structure is to be cleaned up, basically
looks like ref-counting the nodes at some point, and "last
reader" to go through collecting the slips.

Then, "the Maple-tree", for example, "many-readers/few-writers,
aligned to cache-size, RCU-safe", is an example of fitting into
the framework, vis-a-vis the "Julia-tree", which was the coolest
tree data-structure of a time quite before the Julia language,
out of of HP research instead of Oracle.

Here though it's considered quite a bit more simply, i.e, simple,
since mostly the idea is to have systolic the pump data-in/data-out.

Then, like we were talking about I/O-multiplexing, where
for example io_uring isn't always the framework,
it's similar for accounts of publish-protocols and editions/revisions
for what mostly is "caches" vis-a-vis data-structures, basically
about "volatile and the root-token and compare-and-swap
switching the view of the world", which is on the order of a memory
page, then that readers can make a copy, and the original can be
deleted, or otherwise un-mapped to the great virtual allocator.

In the use-case where the published data is on the order of a page
instead of on the order of a heap, it's just an example of that
"one-size-fits-all" varies for things on the order of a page
vis-a-vis things on the order of the heap.



Technical Details
Message-ID: <He2cneq2T_hh5Y_2nZ2dnZfqnPadnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-mo.newsdeef.eu!srv.newsdeef.eu!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!local-4.nntp.ord.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
References: <11ep150$2vi4k$1@raubtier-asyl.eternal-september.org> <20261202073550.188@kylheku.com> <lLCdnXR8RdSj_432nZ2dnZfqnPqdnZ2d@giganews.com> <Vc2dnbIrHsE7C4z2nZ2dnZfqn_qdnZ2d@giganews.com> <3a-cnWFLv9TxAIz2nZ2dnZfqn_adnZ2d@giganews.com> <11esoa6$soh5$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!weretis.net!feeder9.news.weretis.net!border-4.nntp.ord.giganews.com!nntp.giganews.com!local-4.nntp.ord.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Fri, 04 Dec 2026 08:25:32 +0000
Subject: Re: Why ordered mutexed don't work ...
Newsgroups: comp.lang.c
References: <11ep150$2vi4k$1@raubtier-asyl.eternal-september.org>
 <20261202073550.188@kylheku.com>
 <lLCdnXR8RdSj_432nZ2dnZfqnPqdnZ2d@giganews.com>
 <Vc2dnbIrHsE7C4z2nZ2dnZfqn_qdnZ2d@giganews.com>
 <3a-cnWFLv9TxAIz2nZ2dnZfqn_adnZ2d@giganews.com>
 <11esoa6$soh5$1@dont-email.me>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Fri, 4 Dec 2026 00:25:28 -0800
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101
 Thunderbird/38.6.0
MIME-Version: 1.0
In-Reply-To: <11esoa6$soh5$1@dont-email.me>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Message-ID: <He2cneq2T_hh5Y_2nZ2dnZfqnPadnZ2d@giganews.com>
Lines: 224
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-L95duDzsYNJ8Tk/0XP3qws+orubwTzPbSlwgBH/8O1XCCl+zYlnE8VMq5GG3umoAYRZYQXC8Xp6VkvE!QQ5BTrW+8mLe3pllrm5XOT5geVsoRzdhcY3TlzFyex2r0kdE3s83VmzDADzzhVu8d7sMYq6PPh4g
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40