Article View: comp.lang.c
Article #2097Re: Why ordered mutexed don't work ...
From: Ross Finlayson
Date: Wed, 02 Dec 2026 18:23
Date: Wed, 02 Dec 2026 18:23
122 lines
6867 bytes
6867 bytes
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".
Technical Details
Message-ID:
<lLCdnXR8RdSj_432nZ2dnZfqnPqdnZ2d@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-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mailReferences:
<11ep150$2vi4k$1@raubtier-asyl.eternal-september.org> <20261202073550.188@kylheku.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-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 02 Dec 2026 18:23:58 +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> From: Ross Finlayson <ross.a.finlayson@gmail.com> Date: Wed, 2 Dec 2026 10:23:55 -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: <20261202073550.188@kylheku.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Message-ID: <lLCdnXR8RdSj_432nZ2dnZfqnPqdnZ2d@giganews.com> Lines: 122 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-EzFlunre3zn2EqwCviBBjo+Ri931RvWnR116IUqFtNElKOpirpf214+HiRBbPw0NSNdI+tCgXsxIkMA!0hZtyWfBvr1ANlCHgw7v8bzbsYTXNVZYB/jqqatw/tzk1KBWkn9W81eBvpnuzuQVVVP/Nz8THK/6 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