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 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Read-write mutex sometimes fails on deadlock Date: Mon, 6 Nov 2017 22:01:38 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <4502cd60-b843-4ace-99da-d716fc455635@googlegroups.com> <14ca8f28-a689-4431-9122-81a1c504569c@googlegroups.com> NNTP-Posting-Host: MajGvm9MbNtGBKE7r8NgYA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 X-Notice: Filtered by postfilter v. 0.8.2 Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:48742 Date: 2017-11-06T22:01:38+01:00 List-Id: On 2017-11-06 19:24, Robert Eachus wrote: > On Saturday, October 28, 2017 at 4:02:33 PM UTC-4, pascal....@gmail.com wrote: >> >> 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; This is not mutex. Mutex is a semaphore with k=1. > protected body Mutex is > entry Write (D: in Data) is > begin > The_Data := D; > Some_Data := True; > Print(D); This is illegal, because Print is potentially blocking. With a proper mutex printing is done outside any protected action. Here is a proper (not re-entrant, not read-write) mutex is: protected type Mutex is entry Seize; procedure Release; private Owned : Boolean := False; end Mutex; protected body Mutex is entry Seize when not Owned is begin Owned := True; end Seize; procedure Release is begin if Owned then Owned := False; else raise Use_Error with "Releasing not owned mutex"; end if; end Release; end Mutex; Printing goes as follows (an elaborated example): Lock : Mutex; ... select Lock.Seize; or delay Default_Timeout; raise Time_Error with "Printer mutex deadlocked"; end select; begin Print (Something); Lock.Release; exception when others => Lock.Release; raise; end; P.S. It is advisable to have a controlled holder object with initialization seizing and finalization releasing mutex. Then printing goes Like: declare Exclusion : Holder (Lock'Access); -- Seize Lock begin Print (Something); end; -- Release Lock P.P.S. Re-entrant mutex is one the same task may seize consequently. P.P.P.S. Read-write mutex is one with two levels of access. Read access is allowed to several tasks. Write access is to only one. Read-write mutexes have issues with pro-/demotion operations, that is when a task owning the mutext changes the access level without releasing it. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de