From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.107.161.77 with SMTP id k74mr11996873ioe.66.1509992671354; Mon, 06 Nov 2017 10:24:31 -0800 (PST) X-Received: by 10.157.92.195 with SMTP id r3mr343351oti.5.1509992671261; Mon, 06 Nov 2017 10:24:31 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.unit0.net!peer01.am4!peer.am4.highwinds-media.com!peer02.ams1!peer.ams1.xlned.com!news.xlned.com!peer03.fr7!futter-mich.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!l196no1221752itl.0!news-out.google.com!x87ni30ita.0!nntp.google.com!l196no1221740itl.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 6 Nov 2017 10:24:30 -0800 (PST) In-Reply-To: <4502cd60-b843-4ace-99da-d716fc455635@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:191:8303:2100:5985:2c17:9409:aa9c; posting-account=fdRd8woAAADTIlxCu9FgvDrUK4wPzvy3 NNTP-Posting-Host: 2601:191:8303:2100:5985:2c17:9409:aa9c References: <4502cd60-b843-4ace-99da-d716fc455635@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <14ca8f28-a689-4431-9122-81a1c504569c@googlegroups.com> Subject: Re: Read-write mutex sometimes fails on deadlock From: Robert Eachus Injection-Date: Mon, 06 Nov 2017 18:24:31 +0000 Content-Type: text/plain; charset="UTF-8" X-Received-Body-CRC: 1043527586 X-Received-Bytes: 3088 Xref: news.eternal-september.org comp.lang.ada:48737 Date: 2017-11-06T10:24:30-08:00 List-Id: 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.