Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

You will not censor me through bug terrorism. -- James Troup


devel / comp.lang.c++ / A Java- / .NET-like monitor

SubjectAuthor
* A Java- / .NET-like monitorBonita Montero
+* Re: A Java- / .NET-like monitorBonita Montero
|`* Re: A Java- / .NET-like monitorChris M. Thomasson
| +* Re: A Java- / .NET-like monitorChris M. Thomasson
| |+* Re: A Java- / .NET-like monitorBonita Montero
| ||`- Re: A Java- / .NET-like monitorChris M. Thomasson
| |`- Re: A Java- / .NET-like monitorChris M. Thomasson
| `* Re: A Java- / .NET-like monitorBonita Montero
|  `- Re: A Java- / .NET-like monitorChris M. Thomasson
+* Re: A Java- / .NET-like monitorKaz Kylheku
|`* Re: A Java- / .NET-like monitorBonita Montero
| `* Re: A Java- / .NET-like monitorKaz Kylheku
|  `* Re: A Java- / .NET-like monitorBonita Montero
|   +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   |`* Re: A Java- / .NET-like monitorKaz Kylheku
|   | `- Re: A Java- / .NET-like monitorChris M. Thomasson
|   +* Re: A Java- / .NET-like monitorKaz Kylheku
|   |`* Re: A Java- / .NET-like monitorBonita Montero
|   | +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |`* Re: A Java- / .NET-like monitorBonita Montero
|   | | `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |  `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |+- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |`* Re: A Java- / .NET-like monitorBonita Montero
|   | |   | `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |  `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |+- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |`* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   | `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |  +- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |  `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |   `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |`* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    | `- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    +* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |`* Re: A Java- / .NET-like monitorPavel
|   | |   |   |    | `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |  `* Re: A Java- / .NET-like monitorPavel
|   | |   |   |    |   `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |    `* Re: A Java- / .NET-like monitorPavel
|   | |   |   |    |     +- Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     +* Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     |+* Re: A Java- / .NET-like monitorKaz Kylheku
|   | |   |   |    |     ||+- Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     ||+- Re: A Java- / .NET-like monitorPavel
|   | |   |   |    |     ||`* Re: A Java- / .NET-like monitorMichael S
|   | |   |   |    |     || `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||  `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||   `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||    +- Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||    `* Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     ||     `* Re: A Java- / .NET-like monitorKaz Kylheku
|   | |   |   |    |     ||      +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      |`* Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     ||      | +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      | |`- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      | `* Re: A Java- / .NET-like monitorFred. Zwarts
|   | |   |   |    |     ||      |  `* Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     ||      |   `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||      |    `- Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     ||      +* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||      |`* Re: A Java- / .NET-like monitorKaz Kylheku
|   | |   |   |    |     ||      | `* Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     ||      |  +- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      |  `* Re: A Java- / .NET-like monitorMichael S
|   | |   |   |    |     ||      |   +- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      |   `* Re: A Java- / .NET-like monitorDavid Brown
|   | |   |   |    |     ||      |    +* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||      |    |+* Re: A Java- / .NET-like monitorDavid Brown
|   | |   |   |    |     ||      |    ||`* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||      |    || `* Re: A Java- / .NET-like monitorDavid Brown
|   | |   |   |    |     ||      |    ||  +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      |    ||  |`- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      |    ||  `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||      |    ||   `* Re: A Java- / .NET-like monitorDavid Brown
|   | |   |   |    |     ||      |    ||    `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||      |    ||     `* Re: A Java- / .NET-like monitorDavid Brown
|   | |   |   |    |     ||      |    ||      `- Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     ||      |    |`- Re: A Java- / .NET-like monitorScott Lurndal
|   | |   |   |    |     ||      |    `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     ||      |     `- Re: A Java- / .NET-like monitorDavid Brown
|   | |   |   |    |     ||      `- Re: A Java- / .NET-like monitorDavid Brown
|   | |   |   |    |     |+- Re: A Java- / .NET-like monitorPavel
|   | |   |   |    |     |`* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     | `* Re: A Java- / .NET-like monitorKaz Kylheku
|   | |   |   |    |     |  `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     |   `* Re: A Java- / .NET-like monitorKaz Kylheku
|   | |   |   |    |     |    `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     |     `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     |      +* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     |      |`- Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     |      `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     |       `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     |        `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     |         `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     |          `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     |           `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    |     |            `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   |    |     `* Re: A Java- / .NET-like monitorChris M. Thomasson
|   | |   |   |    `* Re: A Java- / .NET-like monitorBonita Montero
|   | |   |   `- Re: A Java- / .NET-like monitorBonita Montero
|   | |   `* Re: A Java- / .NET-like monitorPavel
|   | `* Re: A Java- / .NET-like monitorKaz Kylheku
|   `* Re: A Java- / .NET-like monitorChris M. Thomasson
+* Re: A Java- / .NET-like monitorBonita Montero
`* Re: A Java- / .NET-like monitorBonita Montero

Pages:1234567891011
Re: A Java- / .NET-like monitor

<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2498&group=comp.lang.c%2B%2B#2498

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 20:56:08 +0100
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Nov 2023 19:56:07 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="8087a75e9a50340a40d90ff08d823f7b";
logging-data="1859372"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ds6KojR5K7NYJqWyrbTpeQiEAUXQqer8="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:rLk5xMNMapXdu0S+n4mQN1eEkvQ=
Content-Language: de-DE
In-Reply-To: <20231108114723.256@kylheku.com>
 by: Bonita Montero - Wed, 8 Nov 2023 19:56 UTC

Am 08.11.2023 um 20:49 schrieb Kaz Kylheku:

> No, the "monitor" idea you're proposing is different in this
> way.

That's not true. That spurious wakesups may happen with a mutex and
a condition variable are constituted in that both are separate enti-
ties. Spuriuos wakesups never happen with my implementation, but
stolen wakeups are still possible.

> Monitors as they are understood in computer science (first described by
> C. A. R. Hoare) do not combine the monitor and condition variables into
> one object; they are distinct entities: one monitor, zero to many
> conditions.

The way monitors work does not suggest an implementation, or they can
be based internally on a mutex and a condition variable, but if you
have a monitor that never has spurious wakeups, it is implemented
like mine.

> To avoid muddying the debate with nonstandard terminology, you might
> want to call your cockamamie idea "bonitor".

I've implemented a monitor without spurious wakeups.

Re: A Java- / .NET-like monitor

<20231108114723.256@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2499&group=comp.lang.c%2B%2B#2499

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 19:49:43 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 26
Message-ID: <20231108114723.256@kylheku.com>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
Injection-Date: Wed, 8 Nov 2023 19:49:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="33bc43c0ef23c1905bc74e9bbff4021e";
logging-data="1856511"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0a3l9QF9EgkMnJOK1siiYpT0YoQ5S9mU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:q2AFkOQIHgLjjjw7yRoe9GKkjsA=
 by: Kaz Kylheku - Wed, 8 Nov 2023 19:49 UTC

On 2023-11-08, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 08.11.2023 um 19:16 schrieb Kaz Kylheku:
>
>> POSIX-style mutexes and condition variables are actually Mesa-style
>> monitors.
>
> A monitor is different because the mutex and condition variable
> is joined in a monitor which allows the shown optimization while
> waiting to be notified.

No, the "monitor" idea you're proposing is different in this
way.

Monitors as they are understood in computer science (first described by
C. A. R. Hoare) do not combine the monitor and condition variables into
one object; they are distinct entities: one monitor, zero to many
conditions.

To avoid muddying the debate with nonstandard terminology, you might
want to call your cockamamie idea "bonitor".

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: A Java- / .NET-like monitor

<uih3s2$1r33v$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2501&group=comp.lang.c%2B%2B#2501

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 14:59:14 -0800
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <uih3s2$1r33v$1@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Nov 2023 22:59:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="19f344dc7af9d1b20308a5f94e05a2cd";
logging-data="1936511"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19VcBv2DlQa3BXGvNEI2sf1QYnwov5a+5M="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:aprviYj20ChfN2wLSLx78/S/xKg=
In-Reply-To: <uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Wed, 8 Nov 2023 22:59 UTC

On 11/8/2023 11:56 AM, Bonita Montero wrote:
> Am 08.11.2023 um 20:49 schrieb Kaz Kylheku:
>
>> No, the "monitor" idea you're proposing is different in this
>> way.
>
> That's not true. That spurious wakesups may happen with a mutex and
> a condition variable are constituted in that both are separate enti-
> ties. Spuriuos wakesups never happen with my implementation, but
> stolen wakeups are still possible.
>
>> Monitors as they are understood in computer science (first described by
>> C. A. R. Hoare) do not combine the monitor and condition variables into
>> one object; they are distinct entities: one monitor, zero to many
>> conditions.
>
> The way monitors work does not suggest an implementation, or they can
> be based internally on a mutex and a condition variable, but if you
> have a monitor that never has spurious wakeups, it is implemented
> like mine.
>
>> To avoid muddying the debate with nonstandard terminology, you might
>> want to call your cockamamie idea "bonitor".
>
> I've implemented a monitor without spurious wakeups.
>

Yawn.

A Java- / .NET-like monitor

<uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2502&group=comp.lang.c%2B%2B#2502

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 15:56:54 +0100
Organization: A noiseless patient Spider
Lines: 277
Message-ID: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Nov 2023 14:56:54 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="8087a75e9a50340a40d90ff08d823f7b";
logging-data="1741792"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zmBIj107HuewS9y2WS91fO+1Qm32HAqc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:MRkDkyFjinm8waOiMNBWl/JPAF8=
Content-Language: de-DE
 by: Bonita Montero - Wed, 8 Nov 2023 14:56 UTC

Java and .NET have monitor objects instead of a combination of mutexes
and condition variables. The advantage of a monitor object is that when
you wait for a monitor to be signalled you can wait for that and for the
mutexe's semaphore to be unlocked in _one_ step. With a condition varia-
ble you have first to wait for the notify()-semaphore to be signalled
and for the mutexe's lock in two steps.
The below code simply has a 32 or 64 bit atomic (depening on if the 64
bit variant is lock-free or not) and the lower half is the number of
threads waiting to enter the mutex part and the upper half is the num-
ber of threads waiting to be notified. As all threads wanting to be
notified are also waiting for the mutex the lower half is always >=
the upper half, which I check for in several asserts.
On Windows waiting to be notified and waiting to lock the mutex part
to be unlocked is done by WaitForMultipleObjects(). On Linux there's
no way to wait for mutliple kernel handles to be signalled, but there
are SystemV semaphore sets which may consist of several semaphores and
you may have multiple operations to proceed atomically on this set.
The drawback of combining the mutex and condition-variable parts is
that you can't have multiple conditions associated with the same mutex.

// monitor.h

#pragma once
#if defined(_WIN32)
#define NOMINMAX
#include <Windows.h>
#elif defined(__unix__)
#include <sys/types.h>
#include <sys/sem.h>
#include <sys/stat.h>
#else
#error unsupported platform
#endif
#include <atomic>
#include <semaphore>
#include <type_traits>
#if defined(_WIN32)
#include "xhandle.h"
#endif

struct monitor
{ monitor();
~monitor();
void lock();
void unlock();
void wait();
void notify();
void notify_all();
private:
inline static thread_local char t_dummy;
static constexpr bool USE64 = std::atomic_int64_t::is_always_lock_free;
using atomic_word_t = std::conditional_t<USE64, uint64_t, uint32_t>;
static constexpr unsigned BITS = USE64 ? 32 : 16;
static constexpr atomic_word_t
ENTER_VALUE = 1,
SIGNAL_VALUE = USE64 ? 1ull << 32 : 1,
ENTER_MASK = USE64 ? (uint32_t)-1 : (uint16_t)-1,
SIGNAL_MASK = USE64 ? (uint64_t)(uint32_t)-1 << 32 :
(uint32_t)(uint16_t)-1 << 16;
std::atomic<atomic_word_t> m_atomic;
std::atomic<char *> m_threadId;
std::atomic_uint32_t m_recCount;
#if defined(_WIN32)
static constexpr uint32_t SEM_MAX = std::numeric_limits<LONG>::max();
XHANDLE
m_xhEnterEvt,
m_xhSignalSem;
#elif defined(__unix__)
static constexpr uint32_t SEM_MAX = std::numeric_limits<short>::max();
int m_sems;
int semop( std::initializer_list<sembuf> sems );
#endif
};

// monitor.cpp

#include <iostream>
#include <limits>
#include <system_error>
#include <cassert>
#include "monitor.h"

using namespace std;

monitor::monitor() :
m_atomic( 0 ),
m_threadId( nullptr )
#if defined(_WIN32)
, m_xhEnterEvt( CreateEventA( nullptr, FALSE, FALSE, nullptr ) ),
m_xhSignalSem( CreateSemaphoreA( nullptr, 0, SEM_MAX, nullptr ) )
#elif defined(__unix__)
, m_sems( semget( IPC_PRIVATE, 2, S_IRUSR | S_IWUSR ) )
#endif
{ #if defined(_WIN32)
if( !m_xhEnterEvt.get() || !m_xhSignalSem.get() )
throw system_error( GetLastError(), system_category(), "can't
initialize monitor object" );
#elif defined(__unix__)
union semun { int val; void *p; } su;
su.val = 0;
#if defined(__linux__)
if( m_sems == -1 )
#else
if( m_sems == -1 || semctl( m_sems, 0, SETVAL, su ) == -1 || semctl(
m_sems, 1, SETVAL, su ) == -1 )
#endif
throw system_error( errno, system_category(), "can't initialize
monitor object" );
#endif
}

monitor::~monitor()
{ #if defined(__unix__)
int ret = semctl( m_sems, 0, IPC_RMID );
assert(ret != -1);
#endif
}

void monitor::lock()
{ if( m_threadId.load( memory_order_relaxed ) == &t_dummy )
{
uint32_t oldRecCount = m_recCount.load( memory_order_relaxed );
if( oldRecCount == (uint32_t)-1 )
throw system_error( (int)errc::result_out_of_range,
generic_category(), "montor's recursion count saturated" );
m_recCount.store( oldRecCount + 1, memory_order_relaxed );
return;
}
atomic_word_t ref = m_atomic.load( memory_order_relaxed );
do
{
if( (ref & ENTER_MASK) == ENTER_MASK )
throw system_error( (int)errc::result_out_of_range,
generic_category(), "montor's locker count saturated" );
assert((ref & ENTER_MASK) >= ref >> BITS);
} while( !m_atomic.compare_exchange_strong( ref, ref + 1,
memory_order_acquire, memory_order_relaxed ) );
auto initThread = [&]()
{
m_threadId.store( &t_dummy, memory_order_relaxed );
m_recCount.store( 0, memory_order_relaxed );
};
if( (ref & ENTER_MASK) == ref >> BITS ) [[likely]]
return initThread();
#if defined(_WIN32)
if( WaitForSingleObject( m_xhEnterEvt.get(), INFINITE ) != WAIT_OBJECT_0 )
terminate();
#elif defined(__unix__)
if( semop( { { 0, -1, 0 } } ) )
terminate();
#endif
initThread();
}

void monitor::unlock()
{ if( uint32_t rc; m_threadId.load( memory_order_relaxed ) == &t_dummy &&
(rc = m_recCount.load( memory_order_relaxed )) )
{
m_recCount.store( rc - 1, memory_order_relaxed );
return;
}
atomic_word_t ref = m_atomic.load( memory_order_relaxed );
assert((ref & ENTER_MASK) && m_threadId == &t_dummy);
m_threadId.store( nullptr, memory_order_relaxed );
do
assert((ref & ENTER_MASK) >= ref >> BITS);
while( !m_atomic.compare_exchange_strong( ref, ref - 1,
memory_order_release, memory_order_relaxed ) );
if( (ref & ENTER_MASK) == 1 ) [[likely]]
return;
#if defined(_WIN32)
if( !SetEvent( m_xhEnterEvt.get() ) )
terminate();
#elif defined(__unix__)
if( semop( { { 0, 1, IPC_NOWAIT } } ) )
terminate();
#endif
}

void monitor::wait()
{ assert(m_threadId == &t_dummy && !m_recCount);
m_threadId.store( nullptr, memory_order_relaxed );
atomic_word_t ref = m_atomic.load( memory_order_relaxed );
do
assert((ref & ENTER_MASK) > ref >> BITS);
while( !m_atomic.compare_exchange_strong( ref, ref + SIGNAL_VALUE,
memory_order_release, memory_order_relaxed ) );
if( (ref & ENTER_MASK) - (ref >> BITS) > 1 )
{
#if defined(_WIN32)
if( !SetEvent( m_xhEnterEvt.get() ) )
terminate();
#elif defined(__unix__)
if( semop( { { 0, 1, IPC_NOWAIT } } ) )
terminate();
#endif
}
#if defined(_WIN32)
HANDLE waitFor[2] { m_xhEnterEvt.get(), m_xhSignalSem.get() };
if( WaitForMultipleObjects( 2, waitFor, TRUE, INFINITE ) != WAIT_OBJECT_0 )
terminate();
#elif defined(__unix__)
if( semop( { { 0, -1, 0 }, { 1, -1, 0 } } ) )
terminate();
#endif
m_threadId.store( &t_dummy, memory_order_relaxed );
m_recCount.store( 0, memory_order_relaxed );
}

void monitor::notify()
{ atomic_word_t ref = m_atomic.load( memory_order_relaxed );
assert((ref & ENTER_MASK) > ref >> BITS && m_threadId == &t_dummy);
uint32_t n;
while( (n = (uint32_t)(ref >> BITS)) &&
!m_atomic.compare_exchange_strong( ref, ref - SIGNAL_VALUE,
memory_order_relaxed, memory_order_relaxed ) );
if( !(ref >> BITS) )
return;
#if defined(_WIN32)
if( !ReleaseSemaphore( m_xhSignalSem.get(), 1, nullptr ) )
terminate();
#elif defined(__unix__)
int ret;
do
ret = semop( { { 1, 1, IPC_NOWAIT } } );
while( ret == EAGAIN );
if( ret )
terminate();
#endif
}

void monitor::notify_all()
{ atomic_word_t ref = m_atomic.load( memory_order_relaxed );
assert((ref & ENTER_MASK) > ref >> BITS && m_threadId == &t_dummy);
uint32_t n;
while( (n = (uint32_t)(ref >> BITS)) &&
!m_atomic.compare_exchange_strong( ref, ref & ENTER_MASK,
memory_order_relaxed, memory_order_relaxed ) );
while( n )
{
uint32_t nRelease = n <= SEM_MAX ? n : SEM_MAX;
#if defined(_WIN32)
BOOL succ;
for( ; !(succ = ReleaseSemaphore( m_xhSignalSem.get(), nRelease,
nullptr )) && GetLastError() == ERROR_TOO_MANY_POSTS;
nRelease = nRelease > 1 ? nRelease / 2 : nRelease );
if( !succ )
terminate();
#elif defined(__unix__)
int ret;
for( ; (ret = semop( { { 1, (short)nRelease, IPC_NOWAIT } } )) == EAGAIN;
nRelease = nRelease > 1 ? nRelease / 2 : nRelease );
if( ret )
terminate();
#endif
n -= nRelease;
}
}

#if defined(__unix__)
int monitor::semop( initializer_list<sembuf> sems )
{ int ret;
while( (ret = ::semop( m_sems, const_cast<sembuf *>(sems.begin()),
sems.size() )) == EINTR );
return ret;
} #endif

Re: A Java- / .NET-like monitor

<uigvpj$1q2i4$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2503&group=comp.lang.c%2B%2B#2503

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.samoylyk.net!news.gegeweb.eu!gegeweb.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 13:49:39 -0800
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <uigvpj$1q2i4$2@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
<uigv9g$1q2i4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Nov 2023 21:49:39 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="19f344dc7af9d1b20308a5f94e05a2cd";
logging-data="1903172"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/udvtGWUHZdbesQZlfvtg9/U25NcNLdxU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:8ujD1jX0vSXSsLM9hji68tonhp0=
Content-Language: en-US
In-Reply-To: <uigv9g$1q2i4$1@dont-email.me>
 by: Chris M. Thomasson - Wed, 8 Nov 2023 21:49 UTC

On 11/8/2023 1:41 PM, Chris M. Thomasson wrote:
> On 11/8/2023 9:16 AM, Bonita Montero wrote:
>> Am 08.11.2023 um 15:56 schrieb Bonita Montero:
>>> Java and .NET have monitor objects instead of a combination of mutexes
>>> and condition variables. The advantage of a monitor object is that when
>>> you wait for a monitor to be signalled you can wait for that and for the
>>> mutexe's semaphore to be unlocked in _one_ step. With a condition varia-
>>> ble you have first to wait for the notify()-semaphore to be signalled
>>> and for the mutexe's lock in two steps.
>>> The below code simply has a 32 or 64 bit atomic (depening on if the 64
>>> bit variant is lock-free or not) and the lower half is the number of
>>> threads waiting to enter the mutex part and the upper half is the num-
>>> ber of threads waiting to be notified. As all threads wanting to be
>>> notified are also waiting for the mutex the lower half is always >=
>>> the upper half, which I check for in several asserts.
>>> On Windows waiting to be notified and waiting to lock the mutex part
>>> to be unlocked is done by WaitForMultipleObjects(). On Linux there's
>>> no way to wait for mutliple kernel handles to be signalled, but there
>>> are SystemV semaphore sets which may consist of several semaphores and
>>> you may have multiple operations to proceed atomically on this set.
>>> The drawback of combining the mutex and condition-variable parts is
>>> that you can't have multiple conditions associated with the same mutex.
>
> [snip code]
>
> Model it through Relacy Race Detector first, if you get any issues, we
> can work through them. ;^)
>
> https://github.com/dvyukov/relacy
>
>

There is no shame in using a race detector. If you want me to debug your
work, well, its not going to be for free. Believe it or not its not
exactly trivial. You already had to make corrections to your own code.

> while( ret == -1 && errno == EAGAIN );

Keep EINTR in mind.

Re: A Java- / .NET-like monitor

<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2504&group=comp.lang.c%2B%2B#2504

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.niel.me!news.gegeweb.eu!gegeweb.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 18:16:47 +0100
Organization: A noiseless patient Spider
Lines: 287
Message-ID: <uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 8 Nov 2023 17:16:47 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="8087a75e9a50340a40d90ff08d823f7b";
logging-data="1792803"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+30XgrwwkjZt4CCYsfUEz/d6Nus0dF4CU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:gY8eVsUi4WcFv+ku8AFNB7wrvT0=
In-Reply-To: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
Content-Language: de-DE
 by: Bonita Montero - Wed, 8 Nov 2023 17:16 UTC

Am 08.11.2023 um 15:56 schrieb Bonita Montero:
> Java and .NET have monitor objects instead of a combination of mutexes
> and condition variables. The advantage of a monitor object is that when
> you wait for a monitor to be signalled you can wait for that and for the
> mutexe's semaphore to be unlocked in _one_ step. With a condition varia-
> ble you have first to wait for the notify()-semaphore to be signalled
> and for the mutexe's lock in two steps.
> The below code simply has a 32 or 64 bit atomic (depening on if the 64
> bit variant is lock-free or not) and the lower half is the number of
> threads waiting to enter the mutex part and the upper half is the num-
> ber of threads waiting to be notified. As all threads wanting to be
> notified are also waiting for the mutex the lower half is always >=
> the upper half, which I check for in several asserts.
> On Windows waiting to be notified and waiting to lock the mutex part
> to be unlocked is done by WaitForMultipleObjects(). On Linux there's
> no way to wait for mutliple kernel handles to be signalled, but there
> are SystemV semaphore sets which may consist of several semaphores and
> you may have multiple operations to proceed atomically on this set.
> The drawback of combining the mutex and condition-variable parts is
> that you can't have multiple conditions associated with the same mutex.
>
>
> // monitor.h
>
> #pragma once
> #if defined(_WIN32)
>     #define NOMINMAX
>     #include <Windows.h>
> #elif defined(__unix__)
>     #include <sys/types.h>
>     #include <sys/sem.h>
>     #include <sys/stat.h>
> #else
>     #error unsupported platform
> #endif
> #include <atomic>
> #include <semaphore>
> #include <type_traits>
> #if defined(_WIN32)
>     #include "xhandle.h"
> #endif
>
> struct monitor
> {
>     monitor();
>     ~monitor();
>     void lock();
>     void unlock();
>     void wait();
>     void notify();
>     void notify_all();
> private:
>     inline static thread_local char t_dummy;
>     static constexpr bool USE64 =
> std::atomic_int64_t::is_always_lock_free;
>     using atomic_word_t = std::conditional_t<USE64, uint64_t, uint32_t>;
>     static constexpr unsigned BITS = USE64 ? 32 : 16;
>     static constexpr atomic_word_t
>         ENTER_VALUE = 1,
>         SIGNAL_VALUE = USE64 ? 1ull << 32 : 1,
>         ENTER_MASK = USE64 ? (uint32_t)-1 : (uint16_t)-1,
>         SIGNAL_MASK = USE64 ? (uint64_t)(uint32_t)-1 << 32 :
> (uint32_t)(uint16_t)-1 << 16;
>     std::atomic<atomic_word_t> m_atomic;
>     std::atomic<char *> m_threadId;
>     std::atomic_uint32_t m_recCount;
> #if defined(_WIN32)
>     static constexpr uint32_t SEM_MAX = std::numeric_limits<LONG>::max();
>     XHANDLE
>         m_xhEnterEvt,
>         m_xhSignalSem;
> #elif defined(__unix__)
>     static constexpr uint32_t SEM_MAX = std::numeric_limits<short>::max();
>     int m_sems;
>     int semop( std::initializer_list<sembuf> sems );
> #endif
> };
>
> // monitor.cpp
>
> #include <iostream>
> #include <limits>
> #include <system_error>
> #include <cassert>
> #include "monitor.h"
>
> using namespace std;
>
> monitor::monitor() :
>     m_atomic( 0 ),
>     m_threadId( nullptr )
> #if defined(_WIN32)
>     , m_xhEnterEvt( CreateEventA( nullptr, FALSE, FALSE, nullptr ) ),
>     m_xhSignalSem( CreateSemaphoreA( nullptr, 0, SEM_MAX, nullptr ) )
> #elif defined(__unix__)
>     , m_sems( semget( IPC_PRIVATE, 2, S_IRUSR | S_IWUSR ) )
> #endif
> {
> #if defined(_WIN32)
>     if( !m_xhEnterEvt.get() || !m_xhSignalSem.get() )
>         throw system_error( GetLastError(), system_category(), "can't
> initialize monitor object" );
> #elif defined(__unix__)
>     union semun { int val; void *p; } su;
>     su.val = 0;
>     #if defined(__linux__)
>     if( m_sems == -1 )
>     #else
>     if( m_sems == -1 || semctl( m_sems, 0, SETVAL, su ) == -1 ||
> semctl( m_sems, 1, SETVAL, su ) == -1 )
>     #endif
>         throw system_error( errno, system_category(), "can't initialize
> monitor object" );
> #endif
> }
>
> monitor::~monitor()
> {
> #if defined(__unix__)
>     int ret = semctl( m_sems, 0, IPC_RMID );
>     assert(ret != -1);
> #endif
> }
>
> void monitor::lock()
> {
>     if( m_threadId.load( memory_order_relaxed ) == &t_dummy )
>     {
>         uint32_t oldRecCount = m_recCount.load( memory_order_relaxed );
>         if( oldRecCount == (uint32_t)-1 )
>             throw system_error( (int)errc::result_out_of_range,
> generic_category(), "montor's recursion count saturated" );
>         m_recCount.store( oldRecCount + 1, memory_order_relaxed );
>         return;
>     }
>     atomic_word_t ref = m_atomic.load( memory_order_relaxed );
>     do
>     {
>         if( (ref & ENTER_MASK) == ENTER_MASK )
>             throw system_error( (int)errc::result_out_of_range,
> generic_category(), "montor's locker count saturated" );
>         assert((ref & ENTER_MASK) >= ref >> BITS);
>     } while( !m_atomic.compare_exchange_strong( ref, ref + 1,
> memory_order_acquire, memory_order_relaxed ) );
>     auto initThread = [&]()
>     {
>         m_threadId.store( &t_dummy, memory_order_relaxed );
>         m_recCount.store( 0, memory_order_relaxed );
>     };
>     if( (ref & ENTER_MASK) == ref >> BITS ) [[likely]]
>         return initThread();
> #if defined(_WIN32)
>     if( WaitForSingleObject( m_xhEnterEvt.get(), INFINITE ) !=
> WAIT_OBJECT_0 )
>         terminate();
> #elif defined(__unix__)
>     if( semop( { { 0, -1, 0 } } ) )
>         terminate();
> #endif
>     initThread();
> }
>
> void monitor::unlock()
> {
>     if( uint32_t rc; m_threadId.load( memory_order_relaxed ) ==
> &t_dummy && (rc = m_recCount.load( memory_order_relaxed )) )
>     {
>         m_recCount.store( rc - 1, memory_order_relaxed );
>         return;
>     }
>     atomic_word_t ref = m_atomic.load( memory_order_relaxed );
>     assert((ref & ENTER_MASK) && m_threadId == &t_dummy);
>     m_threadId.store( nullptr, memory_order_relaxed );
>     do
>         assert((ref & ENTER_MASK) >= ref >> BITS);
>     while( !m_atomic.compare_exchange_strong( ref, ref - 1,
> memory_order_release, memory_order_relaxed ) );
>     if( (ref & ENTER_MASK) == 1 ) [[likely]]
>         return;
> #if defined(_WIN32)
>     if( !SetEvent( m_xhEnterEvt.get() ) )
>         terminate();
> #elif defined(__unix__)
>     if( semop( { { 0, 1, IPC_NOWAIT } } ) )
>         terminate();
> #endif
> }
>
> void monitor::wait()
> {
>     assert(m_threadId == &t_dummy && !m_recCount);
>     m_threadId.store( nullptr, memory_order_relaxed );
>     atomic_word_t ref = m_atomic.load( memory_order_relaxed );
>     do
>         assert((ref & ENTER_MASK) > ref >> BITS);
>     while( !m_atomic.compare_exchange_strong( ref, ref + SIGNAL_VALUE,
> memory_order_release, memory_order_relaxed ) );
>     if( (ref & ENTER_MASK) - (ref >> BITS) > 1 )
>     {
> #if defined(_WIN32)
>         if( !SetEvent( m_xhEnterEvt.get() ) )
>             terminate();
> #elif defined(__unix__)
>         if( semop( { { 0, 1, IPC_NOWAIT } } ) )
>             terminate();
> #endif
>     }
> #if defined(_WIN32)
>     HANDLE waitFor[2] { m_xhEnterEvt.get(), m_xhSignalSem.get() };
>     if( WaitForMultipleObjects( 2, waitFor, TRUE, INFINITE ) !=
> WAIT_OBJECT_0 )
>         terminate();
> #elif defined(__unix__)
>     if( semop( { { 0, -1, 0 }, { 1, -1, 0 } } ) )
>         terminate();
> #endif
>     m_threadId.store( &t_dummy, memory_order_relaxed );
>     m_recCount.store( 0, memory_order_relaxed );
> }
>
> void monitor::notify()
> {
>     atomic_word_t ref = m_atomic.load( memory_order_relaxed );
>     assert((ref & ENTER_MASK) > ref >> BITS && m_threadId == &t_dummy);
>     uint32_t n;
>     while( (n = (uint32_t)(ref >> BITS)) &&
> !m_atomic.compare_exchange_strong( ref, ref - SIGNAL_VALUE,
> memory_order_relaxed, memory_order_relaxed ) );
>     if( !(ref >> BITS) )
>         return;
> #if defined(_WIN32)
>     if( !ReleaseSemaphore( m_xhSignalSem.get(), 1, nullptr ) )
>         terminate();
> #elif defined(__unix__)
>     int ret;
>     do
>         ret = semop( { { 1, 1, IPC_NOWAIT } } );
>     while( ret == EAGAIN );
while( ret == -1 && errno == EAGAIN );
>     if( ret )
>         terminate();
> #endif
> }
>
> void monitor::notify_all()
> {
>     atomic_word_t ref = m_atomic.load( memory_order_relaxed );
>     assert((ref & ENTER_MASK) > ref >> BITS && m_threadId == &t_dummy);
>     uint32_t n;
>     while( (n = (uint32_t)(ref >> BITS)) &&
> !m_atomic.compare_exchange_strong( ref, ref & ENTER_MASK,
> memory_order_relaxed, memory_order_relaxed ) );
>     while( n )
>     {
>         uint32_t nRelease = n <= SEM_MAX ? n : SEM_MAX;
> #if defined(_WIN32)
>         BOOL succ;
>         for( ; !(succ = ReleaseSemaphore( m_xhSignalSem.get(),
> nRelease, nullptr )) && GetLastError() == ERROR_TOO_MANY_POSTS;
>             nRelease = nRelease > 1 ? nRelease / 2 : nRelease );
>         if( !succ )
>             terminate();
> #elif defined(__unix__)
>         int ret;
>         for( ; (ret = semop( { { 1, (short)nRelease, IPC_NOWAIT } } ))
> == EAGAIN;
for( ; (ret = semop( { { 1, (short)nRelease, IPC_NOWAIT } } )) == -1 &&
errno == EAGAIN;


Click here to read the complete article
Re: A Java- / .NET-like monitor

<20231108100351.347@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2505&group=comp.lang.c%2B%2B#2505

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 18:16:36 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 60
Message-ID: <20231108100351.347@kylheku.com>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
Injection-Date: Wed, 8 Nov 2023 18:16:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="33bc43c0ef23c1905bc74e9bbff4021e";
logging-data="1819642"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MgxArAN0tr+rB1YF/CMNa8lJLxxCZoS8="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:HupEgXJnOBLQK9jZrDTy7kea7Ec=
 by: Kaz Kylheku - Wed, 8 Nov 2023 18:16 UTC

On 2023-11-08, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Java and .NET have monitor objects instead of a combination of mutexes
> and condition variables. The advantage of a monitor object is that when

POSIX-style mutexes and condition variables are actually Mesa-style
monitors.

Monitors were invented by C. A. R. Hoare ("Quicksort Guy") and another
collaborator whose name escapes me, in the context of some concurrent
Pascal experiment.

Hoare monitors make some ordering guarantees that the "Mesa semantics"
monitors do not. Something like that when you signal a condition
variable, a waiting thread gets in, and when it releases the mutex, the
original thread get back in (no competititon).

The paradigm is that there is one monitor and multiple conditions.

> you wait for a monitor to be signalled you can wait for that and for the
> mutexe's semaphore to be unlocked in _one_ step. With a condition varia-
> ble you have first to wait for the notify()-semaphore to be signalled
> and for the mutexe's lock in two steps.

That's an internal detail. In the POSIX API, you have pthread_cond_wait,
which looks like one operation to the caller.

It is not required to be implemented with semaphores.

> struct monitor
> {
> monitor();
> ~monitor();
> void lock();
> void unlock();
> void wait();
> void notify();
> void notify_all();

The problem is that you often want multiple condition variables with
one monitor. So this is a nonstarter.

I suggest you make an API where the wait operation has an "int cond_index"
parameter to select a condition variable.

The monitor object can be told at construction time how large a vector
of condition variables is required.

That still doesn't provide all the flexibility, but fits the common use
cases where you have a monitor plus a fixed number of condition
variables.

It doesn't work where you have a dynamic number of condition variables;
e.g. a dynamic set data structure has one monitor, plus a condition on
each node it contains.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: A Java- / .NET-like monitor

<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2506&group=comp.lang.c%2B%2B#2506

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 19:31:20 +0100
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Nov 2023 18:31:20 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="8087a75e9a50340a40d90ff08d823f7b";
logging-data="1825438"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+/CcLwD61rem4StOdZ8DAoAGgbrl/83eA="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:4Tac9KI8EUyQhfsVwnH64l9r45g=
In-Reply-To: <20231108100351.347@kylheku.com>
Content-Language: de-DE
 by: Bonita Montero - Wed, 8 Nov 2023 18:31 UTC

Am 08.11.2023 um 19:16 schrieb Kaz Kylheku:

> POSIX-style mutexes and condition variables are actually Mesa-style
> monitors.

A monitor is different because the mutex and condition variable
is joined in a monitor which allows the shown optimization while
waiting to be notified.

> That's an internal detail. In the POSIX API, you have pthread_cond_wait,
> which looks like one operation to the caller.

The mentioned optimization isn't possible if you don't join the
mutex with the condition variable as I've shown; or more precisely
there's a limit on the number of conditions as explained below.

> The problem is that you often want multiple condition variables with
> one monitor. So this is a nonstarter.

If that would be often Java and .net would provide that.

> I suggest you make an API where the wait operation has an "int cond_index"
> parameter to select a condition variable.

I've got a value which is usually 64 bit where the lower half is the
numer if theads which want to have exclusive access to the mutex. The
upper half is the number of threads that want to be notified. I thought
I could split the 64 bit value in three parts, one for the first threads
and two for the latter two types of threads. But from my eperience with
Java and .net I thought that it doesn't happen often that you need two
separate monitors to have twoo conditions, so I dropped this idea.

> The monitor object can be told at construction time how large a vector
> of condition variables is required.

Then I'd had to make my atomic even more split and the number of
threads being able to register in the bitfields of the atomic would
be too limited. My code is to take advantage of the one-step approach
while waiting to be notified and if you need more conditions beyond
that you'd have to stick with the two kernel calls used with a normal
mutex and condition variable.

Re: A Java- / .NET-like monitor

<uigv9g$1q2i4$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2507&group=comp.lang.c%2B%2B#2507

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.furie.org.uk!usenet.goja.nl.eu.org!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 13:41:04 -0800
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <uigv9g$1q2i4$1@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Nov 2023 21:41:04 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="19f344dc7af9d1b20308a5f94e05a2cd";
logging-data="1903172"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/540oBIuA6dfzPoUim2/X+TaIXDtPxP7I="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:bu8zxmX4LLPNXg7PesUYN1E77+c=
Content-Language: en-US
In-Reply-To: <uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Wed, 8 Nov 2023 21:41 UTC

On 11/8/2023 9:16 AM, Bonita Montero wrote:
> Am 08.11.2023 um 15:56 schrieb Bonita Montero:
>> Java and .NET have monitor objects instead of a combination of mutexes
>> and condition variables. The advantage of a monitor object is that when
>> you wait for a monitor to be signalled you can wait for that and for the
>> mutexe's semaphore to be unlocked in _one_ step. With a condition varia-
>> ble you have first to wait for the notify()-semaphore to be signalled
>> and for the mutexe's lock in two steps.
>> The below code simply has a 32 or 64 bit atomic (depening on if the 64
>> bit variant is lock-free or not) and the lower half is the number of
>> threads waiting to enter the mutex part and the upper half is the num-
>> ber of threads waiting to be notified. As all threads wanting to be
>> notified are also waiting for the mutex the lower half is always >=
>> the upper half, which I check for in several asserts.
>> On Windows waiting to be notified and waiting to lock the mutex part
>> to be unlocked is done by WaitForMultipleObjects(). On Linux there's
>> no way to wait for mutliple kernel handles to be signalled, but there
>> are SystemV semaphore sets which may consist of several semaphores and
>> you may have multiple operations to proceed atomically on this set.
>> The drawback of combining the mutex and condition-variable parts is
>> that you can't have multiple conditions associated with the same mutex.

[snip code]

Model it through Relacy Race Detector first, if you get any issues, we
can work through them. ;^)

https://github.com/dvyukov/relacy

Re: A Java- / .NET-like monitor

<20231108154733.223@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2509&group=comp.lang.c%2B%2B#2509

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 23:51:21 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20231108154733.223@kylheku.com>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<uih3s2$1r33v$1@dont-email.me>
Injection-Date: Wed, 8 Nov 2023 23:51:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e33510ae532c387a4eca237a5d32ef21";
logging-data="1952591"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19l4YyTJxqu0uS40FJvcRmHQ3CFD16RDCE="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:dEaW1fM9ei2N4Od9AyuyFtqWxFg=
 by: Kaz Kylheku - Wed, 8 Nov 2023 23:51 UTC

On 2023-11-08, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
> On 11/8/2023 11:56 AM, Bonita Montero wrote:
>> I've implemented a monitor without spurious wakeups.
>
> Yawn.

Funnier:

Amine Moulay Ramdane has written seven, likewise dead in the water.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: A Java- / .NET-like monitor

<20231108152102.495@kylheku.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2510&group=comp.lang.c%2B%2B#2510

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-4973@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 23:25:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <20231108152102.495@kylheku.com>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
Injection-Date: Wed, 8 Nov 2023 23:25:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="e33510ae532c387a4eca237a5d32ef21";
logging-data="1944712"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+tdNwqDsuHXWnQ/LAzUaGkYi/rBFcMhW0="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:OqpT2ZT02g6V9U0BjmziW1rFXHc=
 by: Kaz Kylheku - Wed, 8 Nov 2023 23:25 UTC

On 2023-11-08, Bonita Montero <Bonita.Montero@gmail.com> wrote:
> Am 08.11.2023 um 20:49 schrieb Kaz Kylheku:
>
>> No, the "monitor" idea you're proposing is different in this
>> way.
>
> That's not true. That spurious wakesups may happen with a mutex and
> a condition variable are constituted in that both are separate enti-
> ties.

That doesn't follow. Hoare's original monitor implementation had
separate condition variables, yet no spurious wakeups.

In fact, the condition wait did not require loops! Just:

if (!whatever_condition)
monitor.wait(whatever_cond_var);

The thread waiting on the condition was guaranteed to get into
the monitor with the condition still true!

The reason we accept spurious wakeups is that the guarantee is not
efficient on systems with multiple processors, not because
we don't know how to code up the guarantee.

Spurious wakesup are part of the "Mesa semantics" of monitors
and condition variables, in contrast to the "Hoare semantics".

> I've implemented a monitor without spurious wakeups.

It doesn't look like a monitor, if it doesn't have multiple condition
variables. Maybe your approach can support that.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: A Java- / .NET-like monitor

<uih5r1$1rd03$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2511&group=comp.lang.c%2B%2B#2511

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 15:32:49 -0800
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <uih5r1$1rd03$1@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 8 Nov 2023 23:32:50 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="1946627"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZDbtWaQsbnJSeeWj+rP4nyCePHURSa90="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:lngRcaRAuTav9ziUU0/60VcPBao=
Content-Language: en-US
In-Reply-To: <uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Wed, 8 Nov 2023 23:32 UTC

On 11/8/2023 11:56 AM, Bonita Montero wrote:
> Am 08.11.2023 um 20:49 schrieb Kaz Kylheku:
>
>> No, the "monitor" idea you're proposing is different in this
>> way.
>
> That's not true. That spurious wakesups may happen with a mutex and
> a condition variable are constituted in that both are separate enti-
> ties. Spuriuos wakesups never happen with my implementation, but
> stolen wakeups are still possible.
>
>> Monitors as they are understood in computer science (first described by
>> C. A. R. Hoare) do not combine the monitor and condition variables into
>> one object; they are distinct entities: one monitor, zero to many
>> conditions.

mutex and condition variables happen to be intimately interconnected.
Look up wait morphing...

>
> The way monitors work does not suggest an implementation, or they can
> be based internally on a mutex and a condition variable, but if you
> have a monitor that never has spurious wakeups, it is implemented
> like mine.
>
>> To avoid muddying the debate with nonstandard terminology, you might
>> want to call your cockamamie idea "bonitor".
>
> I've implemented a monitor without spurious wakeups.
>

Re: A Java- / .NET-like monitor

<uihnei$21tq7$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2512&group=comp.lang.c%2B%2B#2512

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Thu, 9 Nov 2023 05:33:24 +0100
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <uihnei$21tq7$1@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<20231108152102.495@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:33:22 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="a9604e3708dee71e445818bf67ac3a30";
logging-data="2160455"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+iRcutsu4gvhVlL/nHaZg3yvAebXT3ykE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:pbd68X00hu6COXfdKzcXiPejcK4=
In-Reply-To: <20231108152102.495@kylheku.com>
Content-Language: de-DE
 by: Bonita Montero - Thu, 9 Nov 2023 04:33 UTC

Am 09.11.2023 um 00:25 schrieb Kaz Kylheku:

> Spurious wakesup are part of the "Mesa semantics" of monitors
> and condition variables, in contrast to the "Hoare semantics".

Hoare monitors suck since they are less efficient.

Re: A Java- / .NET-like monitor

<uihnhu$21tq7$2@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2514&group=comp.lang.c%2B%2B#2514

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Thu, 9 Nov 2023 05:35:12 +0100
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <uihnhu$21tq7$2@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<uih5r1$1rd03$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:35:10 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="a9604e3708dee71e445818bf67ac3a30";
logging-data="2160455"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4BImAGVL8y8rPFDuTCG7dqTQUh4Qwlzg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:8ZyGOjVY+f+gdf5qXDOrZRSjYY8=
In-Reply-To: <uih5r1$1rd03$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 9 Nov 2023 04:35 UTC

Am 09.11.2023 um 00:32 schrieb Chris M. Thomasson:

> mutex and condition variables happen to be intimately interconnected.
> Look up wait morphing...

With my implementation registering as a thread wanting to enter the
mutex and waiting to be notified is one atomic step. That's only
possible if they're one part.

Re: A Java- / .NET-like monitor

<uihnkd$21rae$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2515&group=comp.lang.c%2B%2B#2515

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!news.hispagatos.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 20:36:29 -0800
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <uihnkd$21rae$1@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<20231108152102.495@kylheku.com>
<uihnei$21tq7$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:36:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="2157902"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ITnWCeSHbiS57w+j+qI5JWM/JTtsB1oI="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Es1lFr/5UNNvUjwIhPIDXxTG0Cs=
In-Reply-To: <uihnei$21tq7$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 9 Nov 2023 04:36 UTC

On 11/8/2023 8:33 PM, Bonita Montero wrote:
> Am 09.11.2023 um 00:25 schrieb Kaz Kylheku:
>
>> Spurious wakesup are part of the "Mesa semantics" of monitors
>> and condition variables, in contrast to the "Hoare semantics".
>
> Hoare monitors suck since they are less efficient.

Humm... Are you okay Bonita? Anything wrong with you?

Re: A Java- / .NET-like monitor

<uihnkl$21tq7$3@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2516&group=comp.lang.c%2B%2B#2516

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Thu, 9 Nov 2023 05:36:39 +0100
Organization: A noiseless patient Spider
Lines: 9
Message-ID: <uihnkl$21tq7$3@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
<uigv9g$1q2i4$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:36:37 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="a9604e3708dee71e445818bf67ac3a30";
logging-data="2160455"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/jSxUK7XTXt29qlTiNtNkVM+paDEUMRpk="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:cTJBukUIxoHDFOFQM/VShrw3/QI=
Content-Language: de-DE
In-Reply-To: <uigv9g$1q2i4$1@dont-email.me>
 by: Bonita Montero - Thu, 9 Nov 2023 04:36 UTC

Am 08.11.2023 um 22:41 schrieb Chris M. Thomasson:

> Model it through Relacy Race Detector first, if you get any issues, we
> can work through them. ;^)
> https://github.com/dvyukov/relacy

You'd suggest Relacy for a hello world.

Re: A Java- / .NET-like monitor

<uihnm3$21tq7$4@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2517&group=comp.lang.c%2B%2B#2517

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Thu, 9 Nov 2023 05:37:24 +0100
Organization: A noiseless patient Spider
Lines: 6
Message-ID: <uihnm3$21tq7$4@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
<uigv9g$1q2i4$1@dont-email.me> <uigvpj$1q2i4$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:37:23 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="a9604e3708dee71e445818bf67ac3a30";
logging-data="2160455"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185uXVcXpgKbtIq1go0Xe9Xx+OqgXVCd5U="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:B0Aivagf2ShjNTZsviwlXmKWyMg=
In-Reply-To: <uigvpj$1q2i4$2@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 9 Nov 2023 04:37 UTC

Am 08.11.2023 um 22:49 schrieb Chris M. Thomasson:

> Keep EINTR in mind.

EINTR is handled if you inspect my own semop overload function.

Re: A Java- / .NET-like monitor

<uihnnm$21tq7$5@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2518&group=comp.lang.c%2B%2B#2518

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Thu, 9 Nov 2023 05:38:16 +0100
Organization: A noiseless patient Spider
Lines: 7
Message-ID: <uihnnm$21tq7$5@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<20231108152102.495@kylheku.com>
<uihnei$21tq7$1@raubtier-asyl.eternal-september.org>
<uihnkd$21rae$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:38:14 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="a9604e3708dee71e445818bf67ac3a30";
logging-data="2160455"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1915Cgpm/qI58Aw/MJVN/JM9nuF+9FNBZk="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:8L6mMo/MdqkOfmzCPPUg2r9eMtY=
In-Reply-To: <uihnkd$21rae$1@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 9 Nov 2023 04:38 UTC

Am 09.11.2023 um 05:36 schrieb Chris M. Thomasson:

> Humm... Are you okay Bonita? Anything wrong with you?

Hoare monitors relase a waiting thread immediately after a notify()
and that's less efficient.

Re: A Java- / .NET-like monitor

<uihnql$21rae$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2519&group=comp.lang.c%2B%2B#2519

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 20:39:49 -0800
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <uihnql$21rae$2@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
<uigv9g$1q2i4$1@dont-email.me> <uigvpj$1q2i4$2@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:39:49 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="2157902"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/coXWXDyvxuXtWk6rzkUKA2WgcM8K7zy4="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:hCteDQ383IVNRqe+4zFlxTHG1CY=
Content-Language: en-US
In-Reply-To: <uigvpj$1q2i4$2@dont-email.me>
 by: Chris M. Thomasson - Thu, 9 Nov 2023 04:39 UTC

On 11/8/2023 1:49 PM, Chris M. Thomasson wrote:
> On 11/8/2023 1:41 PM, Chris M. Thomasson wrote:
>> On 11/8/2023 9:16 AM, Bonita Montero wrote:
>>> Am 08.11.2023 um 15:56 schrieb Bonita Montero:
>>>> Java and .NET have monitor objects instead of a combination of mutexes
>>>> and condition variables. The advantage of a monitor object is that when
>>>> you wait for a monitor to be signalled you can wait for that and for
>>>> the
>>>> mutexe's semaphore to be unlocked in _one_ step. With a condition
>>>> varia-
>>>> ble you have first to wait for the notify()-semaphore to be signalled
>>>> and for the mutexe's lock in two steps.
>>>> The below code simply has a 32 or 64 bit atomic (depening on if the 64
>>>> bit variant is lock-free or not) and the lower half is the number of
>>>> threads waiting to enter the mutex part and the upper half is the num-
>>>> ber of threads waiting to be notified. As all threads wanting to be
>>>> notified are also waiting for the mutex the lower half is always >=
>>>> the upper half, which I check for in several asserts.
>>>> On Windows waiting to be notified and waiting to lock the mutex part
>>>> to be unlocked is done by WaitForMultipleObjects(). On Linux there's
>>>> no way to wait for mutliple kernel handles to be signalled, but there
>>>> are SystemV semaphore sets which may consist of several semaphores and
>>>> you may have multiple operations to proceed atomically on this set.
>>>> The drawback of combining the mutex and condition-variable parts is
>>>> that you can't have multiple conditions associated with the same mutex.
>>
>> [snip code]
>>
>> Model it through Relacy Race Detector first, if you get any issues, we
>> can work through them. ;^)
>>
>> https://github.com/dvyukov/relacy
>>
>>
>
> There is no shame in using a race detector. If you want me to debug your
> work, well, its not going to be for free. Believe it or not its not
> exactly trivial. You already had to make corrections to your own code.
>
> > while( ret == -1 && errno == EAGAIN );
>
> Keep EINTR in mind.

Wrt your code:

https://youtu.be/0R6WIbx8ysE

Re: A Java- / .NET-like monitor

<uihnsr$21rab$2@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2520&group=comp.lang.c%2B%2B#2520

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 20:40:59 -0800
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <uihnsr$21rab$2@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
<uigv9g$1q2i4$1@dont-email.me> <uigvpj$1q2i4$2@dont-email.me>
<uihnm3$21tq7$4@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:41:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="2157899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18bR3Qix994IR10wVDlizf9xl0NMNTmZRU="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Y3cyIEvsX4cwES5axSiKdDQ940I=
Content-Language: en-US
In-Reply-To: <uihnm3$21tq7$4@raubtier-asyl.eternal-september.org>
 by: Chris M. Thomasson - Thu, 9 Nov 2023 04:40 UTC

On 11/8/2023 8:37 PM, Bonita Montero wrote:
> Am 08.11.2023 um 22:49 schrieb Chris M. Thomasson:
>
>> Keep EINTR in mind.
>
> EINTR is handled if you inspect my own semop overload function.
>

I actually might have some free time to blow on it later on tonight.
Humm... You are not exactly a fun person to work for.

Re: A Java- / .NET-like monitor

<uihnu3$21rab$3@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2521&group=comp.lang.c%2B%2B#2521

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 20:41:39 -0800
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <uihnu3$21rab$3@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<uigfpu$1mmp3$1@raubtier-asyl.eternal-september.org>
<uigv9g$1q2i4$1@dont-email.me>
<uihnkl$21tq7$3@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:41:40 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="2157899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+dAEGCiYgZKc2zZiRL6zQnpGiXVbJRBbw="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:iiPp9cNkJUI7cxM9DhLk7MmsuoQ=
In-Reply-To: <uihnkl$21tq7$3@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 9 Nov 2023 04:41 UTC

On 11/8/2023 8:36 PM, Bonita Montero wrote:
> Am 08.11.2023 um 22:41 schrieb Chris M. Thomasson:
>
>> Model it through Relacy Race Detector first, if you get any issues, we
>> can work through them. ;^)
>> https://github.com/dvyukov/relacy
>
> You'd suggest Relacy for a hello world.
>
>

:^D

Hello world! Try to get it passing a Relacy test, if you are having
trouble, I can help you.

Re: A Java- / .NET-like monitor

<uihnup$21rab$4@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2522&group=comp.lang.c%2B%2B#2522

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 20:42:00 -0800
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <uihnup$21rab$4@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<20231108152102.495@kylheku.com>
<uihnei$21tq7$1@raubtier-asyl.eternal-september.org>
<uihnkd$21rae$1@dont-email.me>
<uihnnm$21tq7$5@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 04:42:01 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="2157899"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+mB8nGEyJmVm5y1cbuWhvF7YbIn3RU9EE="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:BIzTvnph9M7FxW06WQIIgwX8px0=
In-Reply-To: <uihnnm$21tq7$5@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 9 Nov 2023 04:42 UTC

On 11/8/2023 8:38 PM, Bonita Montero wrote:
> Am 09.11.2023 um 05:36 schrieb Chris M. Thomasson:
>
>> Humm... Are you okay Bonita? Anything wrong with you?
>
> Hoare monitors relase a waiting thread immediately after a notify()
> and that's less efficient.
>

Yawn.

Re: A Java- / .NET-like monitor

<uihpgl$227kv$1@raubtier-asyl.eternal-september.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2531&group=comp.lang.c%2B%2B#2531

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail
From: Bonita.Montero@gmail.com (Bonita Montero)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Thu, 9 Nov 2023 06:08:39 +0100
Organization: A noiseless patient Spider
Lines: 13
Message-ID: <uihpgl$227kv$1@raubtier-asyl.eternal-september.org>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<20231108152102.495@kylheku.com>
<uihnei$21tq7$1@raubtier-asyl.eternal-september.org>
<uihnkd$21rae$1@dont-email.me>
<uihnnm$21tq7$5@raubtier-asyl.eternal-september.org>
<uihnup$21rab$4@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 05:08:38 -0000 (UTC)
Injection-Info: raubtier-asyl.eternal-september.org; posting-host="a9604e3708dee71e445818bf67ac3a30";
logging-data="2170527"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Mv2fmWnNfSc3UvIJFESgyPlT8wcLxJ9Y="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:ohu7LIYu04LwAD/Ljpmc4Vb6wSU=
In-Reply-To: <uihnup$21rab$4@dont-email.me>
Content-Language: de-DE
 by: Bonita Montero - Thu, 9 Nov 2023 05:08 UTC

Am 09.11.2023 um 05:42 schrieb Chris M. Thomasson:
> On 11/8/2023 8:38 PM, Bonita Montero wrote:
>> Am 09.11.2023 um 05:36 schrieb Chris M. Thomasson:
>>
>>> Humm... Are you okay Bonita? Anything wrong with you?
>>
>> Hoare monitors relase a waiting thread immediately after a notify()
>> and that's less efficient.

> Yawn.

Re-acquiring the mutex part of a monitor after notify()
is an superfluous extra part that takes CPU time.

Re: A Java- / .NET-like monitor

<uihpkh$2262f$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2532&group=comp.lang.c%2B%2B#2532

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 21:10:41 -0800
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <uihpkh$2262f$1@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<uih3s2$1r33v$1@dont-email.me> <20231108154733.223@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 05:10:41 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="2168911"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18kQmCpBZKazHot2FbmcaWHyhqQs6mm0oY="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:1qniDckEJVVZywuHQLQwy5rY4fw=
Content-Language: en-US
In-Reply-To: <20231108154733.223@kylheku.com>
 by: Chris M. Thomasson - Thu, 9 Nov 2023 05:10 UTC

On 11/8/2023 3:51 PM, Kaz Kylheku wrote:
> On 2023-11-08, Chris M. Thomasson <chris.m.thomasson.1@gmail.com> wrote:
>> On 11/8/2023 11:56 AM, Bonita Montero wrote:
>>> I've implemented a monitor without spurious wakeups.
>>
>> Yawn.
>
> Funnier:
>
> Amine Moulay Ramdane has written seven, likewise dead in the water.
>

Actually, Amine had a couple of interesting ideas from years ago.
Therefore, I think that Amine just might be "smarter" than Bonita?

Still, wrt Anime, not sure if the ideas are original or from reading a
white paper.

Re: A Java- / .NET-like monitor

<uihpmf$228mv$1@dont-email.me>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=2533&group=comp.lang.c%2B%2B#2533

  copy link   Newsgroups: comp.lang.c++
Path: i2pn2.org!i2pn.org!eternal-september.org!feeder2.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: chris.m.thomasson.1@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c++
Subject: Re: A Java- / .NET-like monitor
Date: Wed, 8 Nov 2023 21:11:43 -0800
Organization: A noiseless patient Spider
Lines: 16
Message-ID: <uihpmf$228mv$1@dont-email.me>
References: <uig7jl$1l4v0$1@raubtier-asyl.eternal-september.org>
<20231108100351.347@kylheku.com>
<uigk5o$1nmku$1@raubtier-asyl.eternal-september.org>
<20231108114723.256@kylheku.com>
<uigp4n$1onpc$1@raubtier-asyl.eternal-september.org>
<20231108152102.495@kylheku.com>
<uihnei$21tq7$1@raubtier-asyl.eternal-september.org>
<uihnkd$21rae$1@dont-email.me>
<uihnnm$21tq7$5@raubtier-asyl.eternal-september.org>
<uihnup$21rab$4@dont-email.me>
<uihpgl$227kv$1@raubtier-asyl.eternal-september.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 9 Nov 2023 05:11:43 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="c15d53f2796d981aaf0ad9fa13a4e8bd";
logging-data="2171615"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/+at7NPuZukSp+SM7nx4r+Pn7WloxTdoc="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:L0VrjUpF0jrIrWGWxh7+ZEwBMGY=
In-Reply-To: <uihpgl$227kv$1@raubtier-asyl.eternal-september.org>
Content-Language: en-US
 by: Chris M. Thomasson - Thu, 9 Nov 2023 05:11 UTC

On 11/8/2023 9:08 PM, Bonita Montero wrote:
> Am 09.11.2023 um 05:42 schrieb Chris M. Thomasson:
>> On 11/8/2023 8:38 PM, Bonita Montero wrote:
>>> Am 09.11.2023 um 05:36 schrieb Chris M. Thomasson:
>>>
>>>> Humm... Are you okay Bonita? Anything wrong with you?
>>>
>>> Hoare monitors relase a waiting thread immediately after a notify()
>>> and that's less efficient.
>
>> Yawn.
>
> Re-acquiring the mutex part of a monitor after notify()
> is an superfluous extra part that takes CPU time.

Look up wait morphing.

Pages:1234567891011
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor