comp.lang.ada
 help / color / mirror / Atom feed
From: gary@telesoft.UUCP@ndmce.uucp (Gary Dismukes @lizard)
Subject: Re: Need to see certain data inside a task type.
Date: Fri, 17-Oct-86 17:06:06 EDT	[thread overview]
Date: Fri Oct 17 17:06:06 1986
Message-ID: <326@ndmce.uucp> (raw)


In response to Len Armstrong's question regarding tasks and discriminants:


Since the access that a task has to data outside of its body is essentially
limited to visible objects declared prior to the task body and to data passed
in as entry parameters, there is no way for a task to directly access a
discriminant of a record type containing the task as a component.  At one
point during the language design process there was some discussion of allowing
discriminants to be declared for task types.  However, this was not long before
the standardization of Ada and it was felt that not enough time was available
to give the necessary attention to integrating such a feature so it was not
added.  There is, however, a way to achieve the basic functionality that you
are looking for.  One approach to solving the problem would be to require an
explicit call to an initialization entry after the declaration of the record
object.  Using the given example we would have:

  declare
    X: TR( SOME_VALUE_OF_SOME_TYPE );
  begin
    X.TASK_FIELD.INITIALIZE( X.PARAM_FIELD );
    ...
  end;

This is clearly undesirable because of the separation of the declaration
and initialization of the object.  We would prefer that the user of the
type TR need not be concerned with the initialization of the task.

An alternative approach is to introduce an access type and initialization
function as shown below:


  package PKG is
  
    task type T is
       entry INITIALIZE ( INIT_PARAM: SOME_TYPE );
       entry E1;
       entry E2;
    end T;
   
    type T_ACCESS is access T;
   
    function T_INIT ( INIT_PARAM: SOME_TYPE ) return T_ACCESS;
   
    type TR ( PARAM_FIELD: SOME_TYPE ) is
      record
        TASK_FIELD : T_ACCESS := T_INIT( PARAM_FIELD );
      end record;
   
  end PKG;

  package body PKG is
   
    task body T is
      PARAMETER: SOME_TYPE;
    begin
      accept INITIALIZE ( INIT_PARAM: SOME_TYPE ) do
        PARAMETER:= INIT_PARAM;
      end INITIALIZE;
        ...
    end T;

    function T_INIT ( INIT_PARAM: SOME_TYPE ) return T_ACCESS is
      NEW_T: T_ACCESS := new T;
    begin
      NEW_T.INITIALIZE( INIT_PARAM );
      return NEW_T;
    end T_INIT;

  end PKG;


It then suffices to declare an object of type PKG.TR with the appropriate
discriminant constraint:
  
    XX : PKG.TR( SOME_VALUE_OF_SOME_TYPE );

This solution encapsulates the creation and initialization of the task object
within a function and relies on the ability to use a discriminant in the
default value expression for a record component.  The replacement of the
task component with an access component is largely transparent to the user
because of the implicit dereferencing for entry calls (e.g., XX.TASK_FIELD.E1).
The main disadvantage is that, if deallocation of the task object is an
important concern, an explicit unchecked deallocation will be necessary
unless the implementation supports garbage collection.

There is also a fundamental limitation in using discriminants to parameterize
tasks because the type of a discriminant is restricted by the language to being
a discrete type, but this may not matter for the application considered.


  Gary Dismukes
  TeleSoft

             reply	other threads:[~1986-10-17 21:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1986-10-17 21:06 gary [this message]
  -- strict thread matches above, loose matches on Subject: below --
1986-10-17 21:10 Need to see certain data inside a task type Bryan
1986-10-14 21:39 Gary Dismukes @lizard
1986-10-14 20:52 Doug Bryan
1986-10-13 14:09 "LEN ARMSTRONG ", SYSTEMS LAB
replies disabled

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