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!mx02.eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Assignment with Adjust and Task Safety Date: Tue, 22 Mar 2016 19:50:32 +0100 Organization: Aioe.org NNTP Server Message-ID: References: <2c50ad2f-e6d5-4dc9-b4d1-905409311a97@googlegroups.com> NNTP-Posting-Host: LMk7+sG0YqgPmReI4fVkAA.user.gioia.aioe.org Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: abuse@aioe.org User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.6.0 X-Notice: Filtered by postfilter v. 0.8.2 Xref: news.eternal-september.org comp.lang.ada:29847 Date: 2016-03-22T19:50:32+01:00 List-Id: On 2016-03-22 15:36, Jeremiah wrote: > I've been playing around with shared/safe/smart pointer > representations in Ada and have been trying to wrap my head around how > task safety works with Adjustment. I definitely get the concept of > Atomic increment/decrement in order to allow to shared pointers in two > separate task to work safely, but when two separate tasks work on the > same shared pointer, I am having trouble using atomic > increment/decrement to prevent the following issue: > > Initial conditions: > 1. Task_A and Task_B both have access to Shared_Pointer_A > 2. Shared_Pointer_A currently is the only reference to Object_A (count = 1) > 3. Shared_Pointer_B currently points to a different Object_B (count > = 1 or whatever). Additionally, Shared_Pointer_B is only used by Task_B. > 4. Shared_Ponter_C is currently null and only used by Task_A > > If I start an assignment in Task_A of Shared_Pointer_C := > Shared_Pointer_A, but get interrupted by Task_B in between when the > contents of Shared_Pointer_A are copied into Shared_Pointer_C and when > Shared_Pointer_C is adjusted, if Task_B then does the assignment > Shared_Pointer_A := Shared_Pointer_B, how do I prevent Shared_Pointer_C >from working with a reference that is finalized by Task_B's assignment > process? You cannot share mutable smart pointer. There is no way to keep it consistent. > I don't think atomic increment/decrement will fix this as the task > switch happens outside of both Adjust and Finalize. Is there some other > way to prevent this? I know at the higher level I can wrap > Shared_Pointer_A into a protected type to ensure the assignments and > reads from it are protected, but that is something normally out of the > scope of the Shared_Pointer_Type. You add a mutex, global or of a narrower scope, and take it in Adjust and Finalize before doing anything else with the pointer. I don't do this in my implementation of smart pointers because I consider it not worth the overhead of two protected actions for being protected against concurrent access to the pointer. [*] The rationale is this. Smart pointers are unlikely shared. Normally they are kept in containers that are not shared or else are protected by a mutex, which eliminates the problem. Yes, it is a potentially a very dangerous problem. It would be great if a tool like AdaControl could detect it. I hope J-P will comment on this. ------------------- * You cannot wrap a protected object by the smart pointer because that would mean calling target object Finalization on the context of a protected operation, which is highly undesirable. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de