🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2141

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

#2141
From: Ross Finlayson
Date: Thu, 03 Dec 2026 16:19
157 lines
8560 bytes
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.

Technical Details
Message-ID: <Vc2dnbIrHsE7C4z2nZ2dnZfqn_qdnZ2d@giganews.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.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-2.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>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.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-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 03 Dec 2026 16:19:50 +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>
From: Ross Finlayson <ross.a.finlayson@gmail.com>
Date: Thu, 3 Dec 2026 08:19:15 -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: <lLCdnXR8RdSj_432nZ2dnZfqnPqdnZ2d@giganews.com>
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 8bit
Message-ID: <Vc2dnbIrHsE7C4z2nZ2dnZfqn_qdnZ2d@giganews.com>
Lines: 157
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-JzTWi3zQdk7NLiLvqfQPQlD7oBmHypOerJ/gtQ+36vcVhfzddLUbwW/tym7cnSy8KcTSDZ53/vPxU97!PJNSAUa70ZwHylXWHGvIBhSznq/QZEXQCwAUQZMwE7Z5KLkLpbB4VcGvspbmCHi4j2IG/FGWkkdS
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