๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #2129

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

#2129
From: Waldek Hebisch
Date: Thu, 03 Dec 2026 08:27
63 lines
4216 bytes
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. Furthermore, if there
> isnโ€™t a free CPU core available, thereโ€™s no guarantee that a thread
> will be woken up immediately. Once I got home, well-fed, I deleted
> the entire project.

AFAICS one can give resonable implementation in user space.
Of course code handling queue of waiting thread is a critical
section. so you need a mutex to protect it.  To handle
suspending and waking up you need per thread semaphore.

To get a ordered mutex a thread first must gain mutex guarding
the implementation.  Once in implementation it check flag
signaling occupancy.  If ordered mutex is occupied, then thead
adds entry for itself to the queue, then leaves critical
section and performs DOWN on its semaphore.  If ordered mutex
is not occupied, then thread sets flag to indicate that
ordered mutex is occupied, leaves the critical section and
go on to perform its work.

To drop ordered mutex a thread first must gain mutex guarding
the implementation.  Once in implementation it checks the
queue and if the queue is nonempty, then removes first thread
(call it T1) from the queue.  If the queue is empty then is sets
flag to signal that ordered mutex is not occupied.  The the
thread leaves the critical section.  After that if it removed
T1 from the queue, then it performs UP on T1-s semaphore.

All what is done in the critical section is a few tests and
pointer operations.  Consequently, there should be almost no
contention for the critical section.  Even if OS wakes up
threads arriving at critical section in wrong order, the
effect should be small enough so that you can ignore it.
Or, to put it differently, on multitasking OS with premption
the time when a thread arrives at the mutex is not measurable.
So we can treat time when thread arrives into the
implementation critical section as the time when it arrived
at ordered mutex.  Since implemetation is inside critical
section we can assume integrity of queue operation, so
threads are dequeued in FIFO order.

Of course, if implementation is part of OS, then OS takes care
to mananage the queue in desired way and there is no need
for per-thread semaphores for suspend and wake up.

From practical point of view it is probably more important that
thead run at desired time, rather than exact time times when
they suspend and wake up.  For that popular choice it to have
thread priorities.  In system with pririties waking up threads
in FIFO order is somewhat unnaturtal.  More procisely, it
basically means that thread at head of the queue has increased
relative priority compared to thread further down.  That
can lead to paradoxes subverting goal of priorities.  But
if problem really demand it, one can try to dynamically adjust
priorites (if needed using implementation similar to the
one outlined above).

-- 
                              Waldek Hebisch
Technical Details
Message-ID: <11er95v$3cgpa$1@paganini.bofh.team>
Path: mbox-mo1.pugleaf.net!news-fra.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
References: <11ep150$2vi4k$1@raubtier-asyl.eternal-september.org>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-fra.pugleaf.net!news.tcpreset.net!newsfeed.bofh.team!paganini.bofh.team!not-for-mail
From: antispam@fricas.org (Waldek Hebisch)
Newsgroups: comp.lang.c
Subject: Re: Why ordered mutexed don't work ...
Date: Thu, 3 Dec 2026 08:27:45 -0000 (UTC)
Organization: bofh.team Usenet service
Message-ID: <11er95v$3cgpa$1@paganini.bofh.team>
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: Thu, 3 Dec 2026 08:27:45 -0000 (UTC)
Injection-Info: paganini.bofh.team; logging-data="3556138"; mail-complaints-to="usenet@bofh.team"; posting-host="d028f9ad2fa2c51f22dbfed0d81d7daf"; posting-account="69e66f1681224ab92235b464ce7a90b6"
User-Agent: tin/2.6.2-20221225 ("Pittyvaich") (Linux/6.1.0-9-amd64 (x86_64))
X-Postfilter: Postfilter-NG 2026.10.2