🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2093

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

#2093
From: Kaz Kylheku
Date: Wed, 02 Dec 2026 17:06
62 lines
4160 bytes
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.

-- 
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
Technical Details
Message-ID: <20261202073550.188@kylheku.com>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu.MISMATCH!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
References: <11ep150$2vi4k$1@raubtier-asyl.eternal-september.org>

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!srv.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Kaz Kylheku <046-301-5902@kylheku.com>
Newsgroups: comp.lang.c
Subject: Re: Why ordered mutexed don't work ...
Date: Wed, 2 Dec 2026 17:06:50 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 62
Message-ID: <20261202073550.188@kylheku.com>
References: <11ep150$2vi4k$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 02 Dec 2026 17:06:51 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3462088"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+gZzmVd8rjpCHPDMpC61GmZsCRmE+x27E="; posting-host="578f78a3435da8d35cab00a350e463cd"
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:6UEeQZjbNoQvsgEPTXkzLI15vBI= sha256:KYcjscsOsCkS4A+EYGtOP8xAGgrl2Jz3VH1MTTIIU/A=
	sha1:/QxKGKYvaFLVUqIfPpQus7FISL8= sha256:lMFptOzfrsM7zbMYcEF3lzEUcM8eNH9Vhp4OhfZuKiY=