comp.lang.ada
 help / color / mirror / Atom feed
* Using the "Terminated" aspect for a task passed to a generic
@ 2018-04-23 14:20 NiGHTS
  2018-04-23 15:40 ` AdaMagica
                   ` (4 more replies)
  0 siblings, 5 replies; 8+ messages in thread
From: NiGHTS @ 2018-04-23 14:20 UTC (permalink / raw)


Having looked carefully through 2 Ada reference books, one dedicated to tasking, and using Google to no end, I am stumped on the following Ada dilemma. 

generic

        type Managed_Task_Type is limited private;

package WD is

        task WD_Thread;

end WD;

package body WD is

        task body WD_Thread is
        
                M : Managed_Task_Type;
        begin
        
                loop
                        if M'Terminated then
                        
                                exit;
                        end if;
                        
                end loop;
        
        end WD_Thread;
        
end WD; 

The above is an overly simplified case study of the problem I am having. Don't mind the busy wait loop as it would not be used in practice.

Basically I am passing a Task of an unknown structure to a generic package that itself runs a task whose sole job is to monitor the provided task. Apparently since the Generic doesn't know that this is a task, the compiler will not allow me to use the Terminated aspect to learn when it has completed.

Is there a way to hint to the compiler that this is indeed a task object? Or is there an alternative method to determine if the task has terminated?

I suppose the worst case scenario is to pass a protected boolean to the M thread that will set the flag upon any sort of termination. Problem with that strategy is that I don't trust the programmer of the task to do this consistently. So then i'd have to write a task interface, elaborate each of my task types, and pass that to the generic... all because my terminated aspect doesn't work in this context.

Any help is appreciated, thanks!

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

* Re: Using the "Terminated" aspect for a task passed to a generic
  2018-04-23 14:20 Using the "Terminated" aspect for a task passed to a generic NiGHTS
@ 2018-04-23 15:40 ` AdaMagica
  2018-04-23 16:23 ` Jeffrey R. Carter
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: AdaMagica @ 2018-04-23 15:40 UTC (permalink / raw)


Am Montag, 23. April 2018 16:20:33 UTC+2 schrieb NiGHTS:
> Having looked carefully through 2 Ada reference books, one dedicated to tasking, and using Google to no end, I am stumped on the following Ada dilemma. 
> 
> generic
> 
>         type Managed_Task_Type is limited private;
> 
> package WD is

Why not

    generic
      with function is_Terminated return Boolean;
    package WD is

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

* Re: Using the "Terminated" aspect for a task passed to a generic
  2018-04-23 14:20 Using the "Terminated" aspect for a task passed to a generic NiGHTS
  2018-04-23 15:40 ` AdaMagica
@ 2018-04-23 16:23 ` Jeffrey R. Carter
  2018-04-23 16:29 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 8+ messages in thread
From: Jeffrey R. Carter @ 2018-04-23 16:23 UTC (permalink / raw)


On 04/23/2018 04:20 PM, NiGHTS wrote:
> 
> generic
> 
>          type Managed_Task_Type is limited private;
> 
> package WD is
> 
>          task WD_Thread;
> 
> end WD;

Ada doesn't have a generic formal task type. Within WD, the only operations you 
have on the type are 'Address, 'Size, "in", and "not in". (I thinks that's it. 
If I've forgotten some, they're similar in utility.) If you want to apply any 
other operation, you have to require it as a generic formal subprogram:

generic
    type Managed_Task is limited private;
    with function Terminated (T : Managed_Task) return Boolean;
package WD is ...

-- 
Jeff Carter
"When danger reared its ugly head, he bravely
turned his tail and fled."
Monty Python and the Holy Grail
60


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

* Re: Using the "Terminated" aspect for a task passed to a generic
  2018-04-23 14:20 Using the "Terminated" aspect for a task passed to a generic NiGHTS
  2018-04-23 15:40 ` AdaMagica
  2018-04-23 16:23 ` Jeffrey R. Carter
@ 2018-04-23 16:29 ` Dmitry A. Kazakov
  2018-04-23 16:35 ` Dan'l Miller
  2018-04-23 17:18 ` NiGHTS
  4 siblings, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2018-04-23 16:29 UTC (permalink / raw)


On 2018-04-23 16:20, NiGHTS wrote:
> Having looked carefully through 2 Ada reference books, one dedicated to tasking, and using Google to no end, I am stumped on the following Ada dilemma.
> 
> generic
> 
>          type Managed_Task_Type is limited private;

I am not sure if that would work, but you could try your luck with

    type Managed_Task_Type is
       new Task_Type and task interface with private;

or

    type Managed_Task_Type is
       synchronized new Task_Type with private;

A better way, almost always, is to put the task into a tagged object and 
pass that down:

    task type Task_Type is ...
    type Task_Object is
       new Ada.Finalization.Limited_Controlled with
    record
       The_Task : Task_Type;
    end record;
    function Is_Terminated (Object : Task_Object) return Boolean is
    begin
       return Object.The_Task'Terminated;
    end Is_Terminated;

generic
    type Managed_Task_Type is new Task_Object with private;
package WD is
     ...

    task body WD_Thread is
       M : Managed_Task_Type;
    begin
       while not M.Is_Terminated loop
          ...
       end loop;

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

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

* Re: Using the "Terminated" aspect for a task passed to a generic
  2018-04-23 14:20 Using the "Terminated" aspect for a task passed to a generic NiGHTS
                   ` (2 preceding siblings ...)
  2018-04-23 16:29 ` Dmitry A. Kazakov
@ 2018-04-23 16:35 ` Dan'l Miller
  2018-04-23 16:41   ` Dmitry A. Kazakov
  2018-04-23 17:04   ` Dan'l Miller
  2018-04-23 17:18 ` NiGHTS
  4 siblings, 2 replies; 8+ messages in thread
From: Dan'l Miller @ 2018-04-23 16:35 UTC (permalink / raw)


On Monday, April 23, 2018 at 9:20:33 AM UTC-5, NiGHTS wrote:
> Having looked carefully through 2 Ada reference books, one dedicated to tasking, and using Google to no end, I am stumped on the following Ada dilemma. 
> 
> generic
> 
>         type Managed_Task_Type is limited private;

Wait.  Why can't we write:

generic
  type Managed_Task_Type is limited task interface;
package WD is
...

as per 12.5.5's AI95-00345 example in the _AARM:2012TC1_ page 527 (given there without the “limited”)?

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

* Re: Using the "Terminated" aspect for a task passed to a generic
  2018-04-23 16:35 ` Dan'l Miller
@ 2018-04-23 16:41   ` Dmitry A. Kazakov
  2018-04-23 17:04   ` Dan'l Miller
  1 sibling, 0 replies; 8+ messages in thread
From: Dmitry A. Kazakov @ 2018-04-23 16:41 UTC (permalink / raw)


On 2018-04-23 18:35, Dan'l Miller wrote:
> On Monday, April 23, 2018 at 9:20:33 AM UTC-5, NiGHTS wrote:
>> Having looked carefully through 2 Ada reference books, one dedicated to tasking, and using Google to no end, I am stumped on the following Ada dilemma.
>>
>> generic
>>
>>          type Managed_Task_Type is limited private;
> 
> Wait.  Why can't we write:
> 
> generic
>    type Managed_Task_Type is limited task interface;
> package WD is
> ...
> 
> as per 12.5.5's AI95-00345 example in the _AARM:2012TC1_ page 527 (given there without the “limited”)?

Because you could not declare an object out of the interface, which 
seems to be a requirement.

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

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

* Re: Using the "Terminated" aspect for a task passed to a generic
  2018-04-23 16:35 ` Dan'l Miller
  2018-04-23 16:41   ` Dmitry A. Kazakov
@ 2018-04-23 17:04   ` Dan'l Miller
  1 sibling, 0 replies; 8+ messages in thread
From: Dan'l Miller @ 2018-04-23 17:04 UTC (permalink / raw)


On Monday, April 23, 2018 at 11:35:11 AM UTC-5, Dan'l Miller wrote:
> On Monday, April 23, 2018 at 9:20:33 AM UTC-5, NiGHTS wrote:
> > generic
> > 
> >         type Managed_Task_Type is limited private;
> 
> Wait.  Why can't we write:
> 
> generic
>   type Managed_Task_Type is limited task interface;
> package WD is
> ...
> 
> as per 12.5.5's AI95-00345 example in the _AARM:2012TC1_ page 527 (given there without the
> “limited”)?

Oh, as per _AARM:2012TC1_'s 3.9.4's Syntax paragraph 2/2, the syntax doesn't support the limited there because of the subsequent 5/2's “… all task and protected interfaces are synchronized interfaces, and all
synchronized interfaces are limited interfaces”.  So amended, it would revert to aforementioned example:

Wait.  Why can't OP modify the design to write:

generic
  type Managed_Task_Type is task interface;
package WD is
... // then the OP's design adjusted to pass in the task-interfaced type instead of having the package
    // contain a freshly-declared task.

as per 12.5.5's AI95-00345 example in the _AARM:2012TC1_ page 527?

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

* Re: Using the "Terminated" aspect for a task passed to a generic
  2018-04-23 14:20 Using the "Terminated" aspect for a task passed to a generic NiGHTS
                   ` (3 preceding siblings ...)
  2018-04-23 16:35 ` Dan'l Miller
@ 2018-04-23 17:18 ` NiGHTS
  4 siblings, 0 replies; 8+ messages in thread
From: NiGHTS @ 2018-04-23 17:18 UTC (permalink / raw)


Thank you all for your help. These are some really interesting ideas. 

I was thinking about my problem out-of-the-box and felt it would be an effective implementation to make a package including a task interface rather than a generic package passing a task. I realized that the task probably should not be defined separately from the WD package and should instead be part of the same inherited package.

To simplify code organization is in itself a bug fix.

I appreciate your quick responses and I have learned a few things as well. Thank you again!

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

end of thread, other threads:[~2018-04-23 17:18 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-04-23 14:20 Using the "Terminated" aspect for a task passed to a generic NiGHTS
2018-04-23 15:40 ` AdaMagica
2018-04-23 16:23 ` Jeffrey R. Carter
2018-04-23 16:29 ` Dmitry A. Kazakov
2018-04-23 16:35 ` Dan'l Miller
2018-04-23 16:41   ` Dmitry A. Kazakov
2018-04-23 17:04   ` Dan'l Miller
2018-04-23 17:18 ` NiGHTS

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