From: Robert Eachus <rieachus@comcast.net>
Subject: Re: Read-write mutex sometimes fails on deadlock
Date: Mon, 6 Nov 2017 10:24:30 -0800 (PST)
Date: 2017-11-06T10:24:30-08:00 [thread overview]
Message-ID: <14ca8f28-a689-4431-9122-81a1c504569c@googlegroups.com> (raw)
In-Reply-To: <4502cd60-b843-4ace-99da-d716fc455635@googlegroups.com>
On Saturday, October 28, 2017 at 4:02:33 PM UTC-4, pascal....@gmail.com wrote:
> Hi,
>
> [long post because it includes the full sources and output, sorry]
>
> I have a package that implements several kinds of mutexes, including a
> read-write mutex. Here is the full source (spec and body) with only the
> read-write part.
You got started down the wrong road and never turned around. The right abstraction for this in Ada is a protected object. A typical mutex looks like:
protected type Mutex is
entry Write(D: in Data):
function Read return Data;
private
The_Data: Data := No_Data;
end Mutex;
protected body Mutex is
entry Write (D: in Data) is
begin
The_Data := D;
Some_Data := True;
Print(D);
delay 0.1;
end Write;
function Read return Data is
return The_Data;
end Read;
end Mutex;
The choice of type Data, the value No_Data and the procedure Print is left to the reader. If you want to insure that calls to Print(D) are sequentialized, you can wrap it in its own protected object:
protected Writer is
procedure Write (D: in Data);
end Writer;
protected body Writer is
procedure Write(D: in Data) is
begin
-- do some output
end Write;
end Writer;
Now you can use Write to do some formatting of your output and be sure even if it is called by your tasks outside the Mutex, for example your tasks could print the data read.
There are thousands of useful patterns in Ada using protected objects or protected values. For example, you could allow exactly one reader after a Write, but allow two successive Writes. Or you could stack up reads before the first write, etc.
next prev parent reply other threads:[~2017-11-06 18:24 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-10-28 20:02 Read-write mutex sometimes fails on deadlock pascal.malaise
2017-11-06 18:24 ` Robert Eachus [this message]
2017-11-06 18:31 ` Simon Wright
2017-11-12 4:33 ` Robert Eachus
2017-11-12 5:21 ` J-P. Rosen
2017-11-15 22:57 ` Randy Brukardt
2017-11-15 23:05 ` Randy Brukardt
2017-11-06 21:01 ` Dmitry A. Kazakov
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox