Post hoc making a type thread safe

This is a discussion on Post hoc making a type thread safe within the ADA forums in Programming Languages category; Hello, I have a rather large object (in terms of # of subprograms) which is standard tagged record. I want to use it in a thread safe manner, and I wonder if there's some better solution than proxying everything with a protected type. I'm not sure if some new 2005 feature can help in this regard. Any gain would be nice; for example, is there some way of using renames to provide the bodies of the protected object using the original object ones? I can't think of a way so I ask in case I'm missing something... Also, I have ...

Go Back   Application Development Forum > Programming Languages > ADA

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 06-10-2008, 02:36 PM
Alex R. Mosteo
Guest
 
Default Post hoc making a type thread safe

Hello,

I have a rather large object (in terms of # of subprograms) which is standard
tagged record. I want to use it in a thread safe manner, and I wonder if
there's some better solution than proxying everything with a protected type.
I'm not sure if some new 2005 feature can help in this regard.

Any gain would be nice; for example, is there some way of using renames to
provide the bodies of the protected object using the original object ones? I
can't think of a way so I ask in case I'm missing something... Also, I have
access to both public, private and body of the original type, in case this is
of use.

In practice I'm looking for something equivalent to:

-- Not Ada
-- protected type Safe is new Unsafe with null record;

Thanks in advance,

Alex.
Reply With Quote
  #2  
Old 06-10-2008, 03:42 PM
Dmitry A. Kazakov
Guest
 
Default Re: Post hoc making a type thread safe

On Tue, 10 Jun 2008 20:36:27 +0200, Alex R. Mosteo wrote:

> I have a rather large object (in terms of # of subprograms) which is standard
> tagged record. I want to use it in a thread safe manner, and I wonder if
> there's some better solution than proxying everything with a protected type.
> I'm not sure if some new 2005 feature can help in this regard.
>
> Any gain would be nice; for example, is there some way of using renames to
> provide the bodies of the protected object using the original object ones? I
> can't think of a way so I ask in case I'm missing something... Also, I have
> access to both public, private and body of the original type, in case this is
> of use.
>
> In practice I'm looking for something equivalent to:
>
> -- Not Ada
> -- protected type Safe is new Unsafe with null record;


I doubt that interfaces would help you in that. Unsafe is a concrete type.
It is just too late. For a protected object it is always too late because
they are "final."

BTW, it would be not that useful, if it were Ada. (Not denying that it
should be Ada.) The reason is that some inherited operations could be
blocking or lengthy. They would be poor candidates for making them
protected. More useful could be multiple inheritance from a task type
converting primitive operations to entries (i.e. making a monitor out of
Unsafe). Even better would be some support for delegation.

Anyway, the pattern I am using for this purpose is admittedly clumsy:

type Safe is new Unsafe with private;
overriding procedure Each_And_Every (X : Safe);
...
private
protected type Lock is
procedure Each_And_Every_Wrapper (X : Unsafe);
...
end Lock;
type Safe is new Unsafe with record
Sequencer : Lock; -- This can be a shared object too
end record;
-------------------------------
protected body Lock is
procedure Each_And_Every_Wrapper (X : Unsafe) is
begin
Each_And_Every (X);
end;
...
end Lock;

procedure Each_And_Every (X : Safe) is
begin
X.Sequencer.Each_And_Every_Wrapper (Unsafe (X));
end Each_And_Every;

--
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de
Reply With Quote
  #3  
Old 06-10-2008, 08:35 PM
tmoran@acm.org
Guest
 
Default Re: Post hoc making a type thread safe

This isn't exactly what you appear to be asking for, but perhaps
it would do what you need.

In Claw.Sockets, it's desirable to avoid one thread trying to do
something with a socket while another thread is in the middle of
some activity with that socket. Our solution was a Controlled
type whose declaration at the beginning of a procedure would
raise an exception if the socket is already busy having something
done to it by another task, or would set a Busy flag if the socket
is not busy. On leaving the routine the Finalize will clear the
Busy state.

Once the mechanism is set up, this just requires a single line declaration
Check_Busy : Check_Busy_Type(Socket'unchecked_access);
to be included at the beginning of each routine to be interlocked.
Reply With Quote
  #4  
Old 06-11-2008, 07:35 AM
Alex R. Mosteo
Guest
 
Default Re: Post hoc making a type thread safe

Dmitry A. Kazakov wrote:

> On Tue, 10 Jun 2008 20:36:27 +0200, Alex R. Mosteo wrote:
>
>> I have a rather large object (in terms of # of subprograms) which is
>> standard tagged record. I want to use it in a thread safe manner, and I
>> wonder if there's some better solution than proxying everything with a
>> protected type. I'm not sure if some new 2005 feature can help in this
>> regard.
>>
>> Any gain would be nice; for example, is there some way of using renames to
>> provide the bodies of the protected object using the original object ones? I
>> can't think of a way so I ask in case I'm missing something... Also, I have
>> access to both public, private and body of the original type, in case this
>> is of use.
>>
>> In practice I'm looking for something equivalent to:
>>
>> -- Not Ada
>> -- protected type Safe is new Unsafe with null record;

>
> I doubt that interfaces would help you in that. Unsafe is a concrete type.
> It is just too late. For a protected object it is always too late because
> they are "final."
>
> BTW, it would be not that useful, if it were Ada. (Not denying that it
> should be Ada.) The reason is that some inherited operations could be
> blocking or lengthy. They would be poor candidates for making them


Yep, I realized there would be problems with such an approach.

> protected. More useful could be multiple inheritance from a task type
> converting primitive operations to entries (i.e. making a monitor out of
> Unsafe). Even better would be some support for delegation.
>
> Anyway, the pattern I am using for this purpose is admittedly clumsy:


Thanks, this is basically what I was planning to do. I was hoping for some
reduction in typing, but I guess there's no way around.

> type Safe is new Unsafe with private;
> overriding procedure Each_And_Every (X : Safe);
> ...
> private
> protected type Lock is
> procedure Each_And_Every_Wrapper (X : Unsafe);
> ...
> end Lock;
> type Safe is new Unsafe with record
> Sequencer : Lock; -- This can be a shared object too
> end record;
> -------------------------------
> protected body Lock is
> procedure Each_And_Every_Wrapper (X : Unsafe) is
> begin
> Each_And_Every (X);
> end;
> ...
> end Lock;
>
> procedure Each_And_Every (X : Safe) is
> begin
> X.Sequencer.Each_And_Every_Wrapper (Unsafe (X));
> end Each_And_Every;
>


Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:32 AM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.