comp.lang.ada
 help / color / mirror / Atom feed
* Task safe containers? Help needed.
@ 2017-10-08  4:27 reinert
  2017-10-08  5:42 ` Niklas Holsti
  2017-10-08  6:57 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 9+ messages in thread
From: reinert @ 2017-10-08  4:27 UTC (permalink / raw)


Hi,

if I want to access a container (Vector, Set, Map) by several tasks (updating and reading) - (hopefully) without loosing functionality of the container.

What should I do?

reinert


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-08  4:27 Task safe containers? Help needed reinert
@ 2017-10-08  5:42 ` Niklas Holsti
  2017-10-08  6:57 ` Dmitry A. Kazakov
  1 sibling, 0 replies; 9+ messages in thread
From: Niklas Holsti @ 2017-10-08  5:42 UTC (permalink / raw)


On 17-10-08 07:27 , reinert wrote:
> Hi,
>
> if I want to access a container (Vector, Set, Map) by several tasks
> (updating and reading) - (hopefully) without loosing functionality of
> the container.
>
> What should I do?
>
> reinert

Wrap the container in a protected object so that all container
operations are executed by one task at a time.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-08  4:27 Task safe containers? Help needed reinert
  2017-10-08  5:42 ` Niklas Holsti
@ 2017-10-08  6:57 ` Dmitry A. Kazakov
  2017-10-08 15:10   ` reinert
  2017-10-09 21:54   ` Randy Brukardt
  1 sibling, 2 replies; 9+ messages in thread
From: Dmitry A. Kazakov @ 2017-10-08  6:57 UTC (permalink / raw)


On 2017-10-08 06:27, reinert wrote:

> if I want to access a container (Vector, Set, Map) by several tasks
> (updating and reading) - (hopefully) without loosing functionality of
> the container.
> 
> What should I do?

1. If operations are short and non-blocking put the container in a 
protected object and route operations trough object's ones.

2. If operations are long, unbounded, blocking (most of the cases):

2.a Use a mutex taken at the beginning of each operation and released at 
the end.

2.b Use monitor task owning the container. All operations are performed 
by the task from task's entries.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-08  6:57 ` Dmitry A. Kazakov
@ 2017-10-08 15:10   ` reinert
  2017-10-08 16:05     ` Dmitry A. Kazakov
  2017-10-09 21:54   ` Randy Brukardt
  1 sibling, 1 reply; 9+ messages in thread
From: reinert @ 2017-10-08 15:10 UTC (permalink / raw)


About your first option: Putting the containers in a protected object makes the container functions "private" and not directly available outside the protected object. Right? I like these container functions.

But your other options also somehow complicates :-)

A compromise seems to be necessary :-) Maybe the first option anyway facilitates cleanest code? There must be a reason for the invention of protected types.

reinert

On Sunday, October 8, 2017 at 8:57:18 AM UTC+2, Dmitry A. Kazakov wrote:
> On 2017-10-08 06:27, reinert wrote:
> 
> > if I want to access a container (Vector, Set, Map) by several tasks
> > (updating and reading) - (hopefully) without loosing functionality of
> > the container.
> > 
> > What should I do?
> 
> 1. If operations are short and non-blocking put the container in a 
> protected object and route operations trough object's ones.
> 
> 2. If operations are long, unbounded, blocking (most of the cases):
> 
> 2.a Use a mutex taken at the beginning of each operation and released at 
> the end.
> 
> 2.b Use monitor task owning the container. All operations are performed 
> by the task from task's entries.
> 
> -- 
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-08 15:10   ` reinert
@ 2017-10-08 16:05     ` Dmitry A. Kazakov
  2017-10-09  9:18       ` reinert
  0 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2017-10-08 16:05 UTC (permalink / raw)


On 2017-10-08 17:10, reinert wrote:

> About your first option: Putting the containers in a protected
> object  makes the container functions "private" and not directly available
> outside the protected object. Right?

Which is good, since they are unsafe.

> But your other options also somehow complicates :-)

Not at all.

> A compromise seems to be necessary :-)

There is no compromise, each method has its use.

> Maybe the first option anyway
> facilitates cleanest code?

No. The cleanest code would be per delegation. Ada has none. Then there 
are issues with interface functions while most relevant synchronization 
mechanisms considered mutating. The Rosen's trick is used to work that 
around. A problem for monitor (#2.b) is to have an unconstrained result, 
since task entries cannot return such.

> There must be a reason for the invention of
> protected types.

The reason was to add an OO view on synchronization as a complement to 
the procedural view tasks offer.

The implementation provided by protected objects was chosen to be most 
efficient, powerful, safe, verifiable and universal. Which comes with 
certain limitations.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-08 16:05     ` Dmitry A. Kazakov
@ 2017-10-09  9:18       ` reinert
  0 siblings, 0 replies; 9+ messages in thread
From: reinert @ 2017-10-09  9:18 UTC (permalink / raw)


Thanks, will test out options...

reinert

On Sunday, October 8, 2017 at 6:05:04 PM UTC+2, Dmitry A. Kazakov wrote:
> On 2017-10-08 17:10, reinert wrote:
> 
> > About your first option: Putting the containers in a protected
> > object  makes the container functions "private" and not directly available
> > outside the protected object. Right?
> 
> Which is good, since they are unsafe.
> 
> > But your other options also somehow complicates :-)
> 
> Not at all.
> 
> > A compromise seems to be necessary :-)
> 
> There is no compromise, each method has its use.
> 
> > Maybe the first option anyway
> > facilitates cleanest code?
> 
> No. The cleanest code would be per delegation. Ada has none. Then there 
> are issues with interface functions while most relevant synchronization 
> mechanisms considered mutating. The Rosen's trick is used to work that 
> around. A problem for monitor (#2.b) is to have an unconstrained result, 
> since task entries cannot return such.
> 
> > There must be a reason for the invention of
> > protected types.
> 
> The reason was to add an OO view on synchronization as a complement to 
> the procedural view tasks offer.
> 
> The implementation provided by protected objects was chosen to be most 
> efficient, powerful, safe, verifiable and universal. Which comes with 
> certain limitations.
> 
> -- 
> Regards,
> Dmitry A. Kazakov
> http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-08  6:57 ` Dmitry A. Kazakov
  2017-10-08 15:10   ` reinert
@ 2017-10-09 21:54   ` Randy Brukardt
  2017-10-10  7:25     ` Dmitry A. Kazakov
  1 sibling, 1 reply; 9+ messages in thread
From: Randy Brukardt @ 2017-10-09 21:54 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:orci8c$mpl$1@gioia.aioe.org...
> On 2017-10-08 06:27, reinert wrote:
>
>> if I want to access a container (Vector, Set, Map) by several tasks
>> (updating and reading) - (hopefully) without loosing functionality of
>> the container.
>>
>> What should I do?
>
> 1. If operations are short and non-blocking put the container in a 
> protected object and route operations trough object's ones.

If some of the operations are functions, and you have an up-to-date 
compiler, then use aspect Exclusive_Functions so that the mutual exclusion 
extends to the functions as well as the procedures in the protected object. 
Otherwise, functions aren't *officially* exclusive, so you have to use all 
procedures for proper exclusion. (Note: I'm not aware of any Ada compiler 
that actually allows non-exclusive functions, but the possibility exists 
unless you use the aspect.)

                                       Randy.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-09 21:54   ` Randy Brukardt
@ 2017-10-10  7:25     ` Dmitry A. Kazakov
  2017-10-10 19:48       ` Randy Brukardt
  0 siblings, 1 reply; 9+ messages in thread
From: Dmitry A. Kazakov @ 2017-10-10  7:25 UTC (permalink / raw)


On 09/10/2017 23:54, Randy Brukardt wrote:

> If some of the operations are functions, and you have an up-to-date
> compiler, then use aspect Exclusive_Functions so that the mutual exclusion
> extends to the functions as well as the procedures in the protected object.

Interesting.

BTW, if protected types were declared as a normal types without ugly 
C++-ish brackets, then we could simply have it passed in-out mode. 
Functions are now allowed to have this. Or it could be passed as a 
non-constant anonymous access. Both would naturally imply 
"update-exclusion", no aspect ever needed.

> Otherwise, functions aren't *officially* exclusive, so you have to use all
> procedures for proper exclusion.

I see it as an advantage, actually. A mutex-based solution could deploy 
read-write mutex to achieve the same effect. Functions would take mutext 
in the read-only mode. Procedures would use the full-access mode.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Task safe containers? Help needed.
  2017-10-10  7:25     ` Dmitry A. Kazakov
@ 2017-10-10 19:48       ` Randy Brukardt
  0 siblings, 0 replies; 9+ messages in thread
From: Randy Brukardt @ 2017-10-10 19:48 UTC (permalink / raw)


"Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de> wrote in message 
news:orhslu$pji$1@gioia.aioe.org...
> On 09/10/2017 23:54, Randy Brukardt wrote:
>
>> If some of the operations are functions, and you have an up-to-date
>> compiler, then use aspect Exclusive_Functions so that the mutual 
>> exclusion
>> extends to the functions as well as the procedures in the protected 
>> object.
>
> Interesting.
>
> BTW, if protected types were declared as a normal types without ugly 
> C++-ish brackets, then we could simply have it passed in-out mode. 
> Functions are now allowed to have this. Or it could be passed as a 
> non-constant anonymous access. Both would naturally imply 
> "update-exclusion", no aspect ever needed.

I agree that the function/procedure definition was a bad idea: only 2 of the 
4 possibilities are supported, and all of the can make sense. Too late to 
fix, though.

Also note that you might need exclusion for read-only access (that's 
definitely true in Ada 2005/2012 where there are no guarantee of task safety 
even for reading of a container -- Ada 2020 will change this slightly).

                               Randy.



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-10-10 19:48 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-08  4:27 Task safe containers? Help needed reinert
2017-10-08  5:42 ` Niklas Holsti
2017-10-08  6:57 ` Dmitry A. Kazakov
2017-10-08 15:10   ` reinert
2017-10-08 16:05     ` Dmitry A. Kazakov
2017-10-09  9:18       ` reinert
2017-10-09 21:54   ` Randy Brukardt
2017-10-10  7:25     ` Dmitry A. Kazakov
2017-10-10 19:48       ` Randy Brukardt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox