Article View: comp.lang.c
Article #2155Re: Why ordered mutexed don't work ...
From: Chris M. Thomasson
Date: Thu, 03 Dec 2026 21:52
Date: Thu, 03 Dec 2026 21:52
179 lines
9721 bytes
9721 bytes
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.
Technical Details
Message-ID:
<11esoa6$soh5$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!news-fra.pugleaf.net!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:
<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>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-fra.pugleaf.net!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: Why ordered mutexed don't work ... Date: Thu, 3 Dec 2026 13:52:05 -0800 Organization: A noiseless patient Spider Lines: 179 Message-ID: <11esoa6$soh5$1@dont-email.me> 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> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Thu, 03 Dec 2026 21:52:07 +0000 (UTC) Injection-Info: dont-email.me; logging-data="942629"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/280cMTH314xk3aDUmnV2l27n2Kunz/Bs="; posting-host="27be630b90107ef0dd0e5ccb8f536ba4" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:3YXCWFNpLPsg3Kkko/mQIVTvzP4= sha256:sFgCo3mWqhqTrjPSC+X1s0lZ+hBggh1ex9iYh4pRVlg= sha1:SW5mtCg8rqLX5AHcC5lYkjjVDM4= sha256:OfAYRjMs0Zst5mkWTWWR7fANQOGSSs777dn2lvRRBX0= Content-Language: en-US In-Reply-To: <3a-cnWFLv9TxAIz2nZ2dnZfqn_adnZ2d@giganews.com>