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: backlog4.nntp.dca3.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!goblin3!goblin1!goblin.stu.neva.ru!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Jeffrey Carter Newsgroups: comp.lang.ada Subject: Re: Safety of unprotected concurrent operations on constant objects Date: Sun, 04 May 2014 11:55:49 -0700 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <7403d130-8b42-43cd-a0f1-53ba34b46141@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 4 May 2014 18:55:51 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="42ea65963a295dd28559459f9f96c6a5"; logging-data="26472"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/NRU+Xct+mhI4kG8JhDAbRMBN90DlhDV8=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.5.0 In-Reply-To: Cancel-Lock: sha1:EnRQ7DfjO7Rqe4gRMlHUG2UT5Ys= Xref: number.nntp.dca.giganews.com comp.lang.ada:186213 Date: 2014-05-04T11:55:49-07:00 List-Id: On 05/04/2014 12:46 AM, Natasha Kerensikova wrote: > > I have some shared resources indexed by a string, described in a > file, and considered as constant throughout the lifetime of the program > (with the standard scheme "restart for changes to take effect"). > > When I have whatever indexed by a string, I immediately think Maps, and > usually go for Ordered_Maps because they are easier to understand. > > Since the map is semantically constant, I use the magic word constant, > with a function to load from file at elaboration, and everything seems > to work fine. So, you have M : constant Map := F; and you're concerned about concurrent calls to M.Element (Key) The reserved word constant means that you can't assign to the object or pass it as an [in] out parameter. It certainly says nothing about what can happen to things designated by an access component of the object, and unbounded containers should be expected to have access components. type AI is access Integer; C : constant AI := new Integer'(1); V : AI := new Integer'(2); C := V; -- illegal C.all := 42; -- No problem As you've noted, the ARM says nothing about task safety for containers in general, maps in general, or ordered maps in specific, so you can't rely on this being task safe. And since you have the source to GNAT's ordered map package, you can look at it and see that this specific implementation is not task safe. As you note, the standard allows for simultaneous calls to protected functions, so in general putting the map in a protected object and allowing access through a protected function doesn't gain you anything. Accessing it through a protected procedure, however, does guarantee non-concurrent access. Since you have access to the source of GNAT, you can see that it locks a PO even for function calls, so with GNAT a protected function shouldn't be a problem. I've worked on a project that used GNAT and AWS and had many tasks accessing hashed maps in protected objects without problem. If you're interested in a solution that is not GNAT-specific, the skip-list implementation in the PragmAda Reusable Components has a Search operation that is task safe. It's easy enough to use a skip list as an ordered map. You'd have to use a[n] [Un]Bounded_String for the key, but that shouldn't be too much of a problem. The PragmARCs are available from http://pragmada.x10hosting.com/pragmarc.htm -- Jeff Carter "Brave Sir Robin ran away." Monty Python and the Holy Grail 59