comp.lang.ada
 help / color / mirror / Atom feed
* Array of tasks
@ 1997-12-09  0:00 Larry Coon
  1997-12-09  0:00 ` Stephen Leake
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Larry Coon @ 1997-12-09  0:00 UTC (permalink / raw)



I posted a similar question from home a few days ago,
and didn't see a response.  I checked from work and
didn't see my original question, so I'm posting again
just in case something happened to my original post.
My apologies if you've already seen this.

I have an array of tasks and an array of semaphores.
The size of both arrays are set at runtime (based on
user input).  I can't figure out a way to make the
array of semaphores visible to the array of tasks.

Here's some contrived code to illustrate what I'm
trying to do:

procedure main is
  protected type semaphore is
    entry seize;
    procedure release;
  private
    available:	boolean := true;
  end semaphore;

  -- The usual implementation of a semaphore.
  protected body semaphore is separate;

  task type t;
  task body t is separate;

  task_count: positive;
begin -- main
  put ("Enter number of tasks: ");
  get (task_count);
  declare
    subtype task_range is integer range 1..task_count;
    semaphore_array: array (task_range) of semaphore;
    task_array: array (task_range) of t;
  begin
    null;
  end;
end;

Within the body of task T I want to access a semaphore in
semaphore_array.  It won't compile, saying the semapore
array is not visible.

How do I declare things so the tasks can see the semaphores?
I've tried nested blocks, and I've tried passing pointers
to the semaphores, but couldn't get anything to work right.
Thanks in advance for any help.

Larry Coon
University of California
larry@assist.org
and lmcoon@home.com




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

* Re: Array of tasks
  1997-12-09  0:00 Array of tasks Larry Coon
@ 1997-12-09  0:00 ` Stephen Leake
  1997-12-09  0:00   ` Larry Coon
  1997-12-10  0:00 ` Anonymous
  1997-12-10  0:00 ` Jean-Pierre Rosen
  2 siblings, 1 reply; 6+ messages in thread
From: Stephen Leake @ 1997-12-09  0:00 UTC (permalink / raw)



Larry Coon wrote:
> 
> I have an array of tasks and an array of semaphores.
> The size of both arrays are set at runtime (based on
> user input).  I can't figure out a way to make the
> array of semaphores visible to the array of tasks.
> 
> Here's some contrived code to illustrate what I'm
> trying to do:
>
>   <fixed code below>
>
> Within the body of task T I want to access a semaphore in
> semaphore_array.  It won't compile, saying the semapore
> array is not visible.
> 
> How do I declare things so the tasks can see the semaphores?
> I've tried nested blocks, and I've tried passing pointers
> to the semaphores, but couldn't get anything to work right.
> Thanks in advance for any help.
> 

I was hoping an access discriminant would do the trick, but I ended up
with dynamically allocating the semaphores. Maybe you can fiddle some
more. I added the bodies so others can compile if they want.

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO;
procedure Test is
  protected type semaphore is
    entry seize;
    procedure release;
  private
    available:  boolean := true;
  end semaphore;

  -- The usual implementation of a semaphore.
  protected body semaphore is separate;

  type Semaphore_Array_Type is array (Integer range <> ) of semaphore;
  type Semaphore_Array_Access_Type is access all Semaphore_Array_Type;
  task type T (Semaphore_Array : Semaphore_Array_Access_Type);
  task body t is separate;

  task_count: positive;
begin -- main
  put ("Enter number of tasks: ");
  Ada.Integer_Text_IO.get (task_count);
  declare
    subtype task_range is integer range 1..task_count;
    Semaphore_Array : Semaphore_Array_Access_Type (task_range) :=
new        Semaphore_Array_Type (Task_Range);
    subtype Specific_Task is T (Semaphore_Array);
    task_array: array (task_range) of Specific_Task;
  begin
    null;
  end;
end Test;

separate (Test)
protected body semaphore is
   entry Seize when Available is
   begin
      Available := False;
   end Seize;

   procedure Release
   is begin
      Available := True;
   end Release;
end semaphore;

separate (Test)
task body T is
begin
   Semaphore_Array (1).Seize;
end T;

> Larry Coon
> University of California
> larry@assist.org
> and lmcoon@home.com

-- 
- Stephe




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

* Re: Array of tasks
  1997-12-09  0:00 ` Stephen Leake
@ 1997-12-09  0:00   ` Larry Coon
  1997-12-10  0:00     ` Mats Weber
  0 siblings, 1 reply; 6+ messages in thread
From: Larry Coon @ 1997-12-09  0:00 UTC (permalink / raw)



Stephen Leake wrote:

> I was hoping an access discriminant would do the trick, but I ended up
> with dynamically allocating the semaphores. Maybe you can fiddle some
> more. I added the bodies so others can compile if they want.

[code snipped]

Stephen,

Thanks for the reply.  The discriminants were something I hadn't thought
of.  The only problem is that I'm already using default discriminants
for
something else, and I'm not sure how to make them work together.  Sorry
I
wasn't complete with my contrived example -- I was trying to reduce the
problem to essentials by eliminating code that wasn't part of the
problem,
and I failed to include code that turned out to be relevant to your
solution.

Okay, to be a little more complete -- My tasks are each assigned a task
number, and I use a discriminant with a default to do it.  Here's my
code again, this time with the default stuff implemented:

procedure main is
  protected type semaphore is
    entry seize;
    procedure release;
  private
    available:  boolean := true;
  end semaphore;

  protected body semaphore is separate;

  protected counter is
    function get_next return integer;
  private
    data:   integer := 1;
  end counter;

  protected body counter is
    function get_next return integer is
      return_val: integer;
    begin
      return_val	:= data;
      data		:= data + 1;
      return return_val;
    end get_next;
  end counter;

  task type t (task_no: integer := counter.get_next);
  task body t is separate;

  task_count: positive;
begin -- main
  put ("Enter number of tasks: ");
  get (task_count);
  declare
    subtype task_range is integer range 1..task_count;
    semaphore_array: array (task_range) of semaphore;
    task_array: array (task_range) of t;
  begin
    null;
  end;
end;

-- And here are the implementation for the "separate" things:
separate (main)
protected body semaphore is
   entry Seize when Available is
   begin
      Available := False;
   end Seize;

   procedure Release
   is begin
      Available := True;
   end Release;
end semaphore;

separate (main)
task body T is
begin
   Semaphore_Array (1).Seize;
end T;


Larry Coon
University of California
larry@assist.org
and lmcoon@home.com




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

* Re: Array of tasks
  1997-12-09  0:00   ` Larry Coon
@ 1997-12-10  0:00     ` Mats Weber
  0 siblings, 0 replies; 6+ messages in thread
From: Mats Weber @ 1997-12-10  0:00 UTC (permalink / raw)



Larry Coon wrote:

> Okay, to be a little more complete -- My tasks are each assigned a task
> number, and I use a discriminant with a default to do it.  Here's my
> code again, this time with the default stuff implemented:

Here is how I would do it, see comments in the code for explanations:

--
with text_io;

use text_io;

procedure try_task_and_semaphore is

  package int_text_io is new text_io.integer_io(integer);

  -- Use a function to get the number of tasks, so you don't need
  -- a nested declare bock.
   
  function get_int (prompt : string) return integer is
    result : integer;
  begin
    put(prompt);
    int_text_io.get(result);
    return result;
  end;

  task_count: positive := get_int(prompt => "Enter number of tasks: ");

  subtype task_range is integer range 1..task_count;

  protected type semaphore is
    entry seize;
    procedure release;
  private
    available:  boolean := true;
  end semaphore;

  protected body semaphore is separate;

  semaphore_array: array (task_range) of semaphore;

  -- Counter does not need (and cannot, because the function get_next is 
  -- not allowed to modify data) be a protected type. See why in the 
  -- Ada 95 Rationale section 9.6. I changed it to a package.

  package counter is
    function get_next return integer;
  private
    data:   integer := 1;
  end counter;

  package body counter is
    function get_next return integer is
      return_val: integer;
    begin
      return_val        := data;
      data              := data + 1;
      return return_val;
    end get_next;
  end counter;

  task type t (task_no: integer := counter.get_next);
  task body t is separate;
  
  task_array: array (task_range) of t;

  -- With everything declared at the same level, the code is simpler
  -- (except for the function to get an integer) and you don't need
  -- access discriminants or other complicated stuff.

  -- Of course if you really need your tasks type to be parameterizable,
  -- then use access discriminants, but I wouldn't use them for visibility
  -- control.

begin
  null;
end;

-- separate bodies not changed.




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

* Re: Array of tasks
  1997-12-09  0:00 Array of tasks Larry Coon
  1997-12-09  0:00 ` Stephen Leake
@ 1997-12-10  0:00 ` Anonymous
  1997-12-10  0:00 ` Jean-Pierre Rosen
  2 siblings, 0 replies; 6+ messages in thread
From: Anonymous @ 1997-12-10  0:00 UTC (permalink / raw)



On Tue, 09 Dec 1997 08:56:18 -0800, Larry Coon <larry@assist.org> wrote:

> I posted a similar question from home a few days ago,
> and didn't see a response.  I checked from work and
> didn't see my original question, so I'm posting again
> just in case something happened to my original post.
> My apologies if you've already seen this.
> 
> I have an array of tasks and an array of semaphores.
> The size of both arrays are set at runtime (based on
> user input).  I can't figure out a way to make the
> array of semaphores visible to the array of tasks.
> 
> Here's some contrived code to illustrate what I'm
> trying to do:
> 
> procedure main is
>   protected type semaphore is
>     entry seize;
>     procedure release;
>   private
>     available:	boolean := true;
>   end semaphore;
> 
>   -- The usual implementation of a semaphore.
>   protected body semaphore is separate;
> 
>   task type t;
>   task body t is separate;
> 
>   task_count: positive;
> begin -- main
>   put ("Enter number of tasks: ");
>   get (task_count);
>   declare
>     subtype task_range is integer range 1..task_count;
>     semaphore_array: array (task_range) of semaphore;
>     task_array: array (task_range) of t;
>   begin
>     null;
>   end;
> end;
> 
> Within the body of task T I want to access a semaphore in
> semaphore_array.  It won't compile, saying the semapore
> array is not visible.
> 
> How do I declare things so the tasks can see the semaphores?
> I've tried nested blocks, and I've tried passing pointers
> to the semaphores, but couldn't get anything to work right.
> Thanks in advance for any help.
> 

The body of type T comes before the declaration of the array of
Semaphore_Array, so it should not be surprising that Semaphore_Array is
not visible in the body of T.

The simple solution is to move the definition of T into the block, so
the body comes after the declaration of Semaphore_Array. Of course, this
means the body of T cannot be separate.

A more complicated solution gives T an access discriminant to type
Semaphore. Then the body of T refers to the semaphore designated by this
access value, not to the array. The fun part is giving each task its own
semaphore access value.

Jeff Carter  PGP:1024/440FBE21
My real e-mail address: ( carter @ innocon . com )
"We call your door-opening request a silly thing."
Monty Python & the Holy Grail

Posted with Spam Hater - see
http://www.compulink.co.uk/~net-services/spam/































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

* Re: Array of tasks
  1997-12-09  0:00 Array of tasks Larry Coon
  1997-12-09  0:00 ` Stephen Leake
  1997-12-10  0:00 ` Anonymous
@ 1997-12-10  0:00 ` Jean-Pierre Rosen
  2 siblings, 0 replies; 6+ messages in thread
From: Jean-Pierre Rosen @ 1997-12-10  0:00 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 987 bytes --]


Larry Coon a �crit dans le message <348D7832.27A4@assist.org>...
>Here's some contrived code to illustrate what I'm
>trying to do:
>
>procedure main is
>  protected type semaphore is
>    entry seize;
>    procedure release;
>  private
>    available: boolean := true;
>  end semaphore;
>
>  -- The usual implementation of a semaphore.
>  protected body semaphore is separate;
>
>  task type t;
>  task body t is separate;
>
>  task_count: positive;
>begin -- main
>  put ("Enter number of tasks: ");
>  get (task_count);
>  declare
>    subtype task_range is integer range 1..task_count;
>    semaphore_array: array (task_range) of semaphore;

You could declare your task type HERE, it will have visibility on the
semaphores. The only thing you'll loose is the ability to have the task body
separate, but if you use a subprogram rather than a nested block statement,
you would get this feature back.

>    task_array: array (task_range) of t;
>  begin
>    null;
>  end;
>end;







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

end of thread, other threads:[~1997-12-10  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-12-09  0:00 Array of tasks Larry Coon
1997-12-09  0:00 ` Stephen Leake
1997-12-09  0:00   ` Larry Coon
1997-12-10  0:00     ` Mats Weber
1997-12-10  0:00 ` Anonymous
1997-12-10  0:00 ` Jean-Pierre Rosen

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