comp.lang.ada
 help / color / mirror / Atom feed
From: Warren <ve3wwg@gmail.com>
Subject: Re: Passing Ada Proc as a C Function Pointer
Date: Thu, 5 Aug 2010 09:51:43 -0700 (PDT)
Date: 2010-08-05T09:51:43-07:00	[thread overview]
Message-ID: <9a27fbef-37ca-4fdd-ab20-9c30c91645ab@5g2000yqz.googlegroups.com> (raw)
In-Reply-To: 45573613-9738-4606-95f5-0103670a34e9@h28g2000yqd.googlegroups.com

On Aug 5, 11:37 am, Warren <ve3...@gmail.com> wrote:
> Arg!!!!  Arg!!!!
>
> I keep thinking in terms of bytes - not BITS!!  No wonder!
>
> Warren

At long last-- it works!!!!

I ended up creating a C routine for the launch:

void
avr_thread_start_ada(
  avr_thread_context* context,
  uint8_t* stack,
  uint16_t stack_size) {
        extern void thread_thunk(void);  /* Ada in C convention */

        avr_thread_start(context,thread_thunk,stack,stack_size);
}

which is called by Start_Thread in Ada. This then invokes the usual
avr_thread_start(), which then launches the Ada (in C convention)
routine Thread_Thunk:

    procedure Thread_Thunk;
    pragma export(C,Thread_Thunk,"thread_thunk");

    procedure Thread_Thunk is
        Context : Thread_Context_Ptr := Get_Context;
    begin
        Context.Ada_Proc.all;  -- Start Ada code in new thread
    end Thread_Thunk;

There was an undocumented AVR_Threads routine in the include file
that allowed me to pick up the active context, which is used by
Ada routine binding Get_Context.

The context object turned out to be tricky because the C context
object needed to be "struct aligned", which appears to be 64-bit.
So a little work on that fixed it. I also rolled the stack into
the object as a variant record, for user convenience:

    type Thread_Context(Stack_Size : Unsigned_16) is
        record
            C_Context :     aliased C_Context_Type;
            Ada_Proc :      Thread_Proc;
            Stack :         aliased Stack_Type(1..Stack_Size);
        end record;

    for Thread_Context use
        record
            C_Context       at  0 range 0 .. 16 * 8 -1;     -- Must be
first to be 64-bit aligned
            Stack_Size      at 16 range 0 .. 15;            -- Moved
here - non critical
            Ada_Proc        at 18 range 0 .. 15;            -- Non
critical placement
        --  Stack           at 20 range 0 .. Stack_Size * 8 - 1;
        end record;

Thanks to everyone for their comments and ideas.

Warren



  reply	other threads:[~2010-08-05 16:51 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-08-05 14:31 Passing Ada Proc as a C Function Pointer Warren
2010-08-05 15:37 ` Warren
2010-08-05 16:51   ` Warren [this message]
  -- strict thread matches above, loose matches on Subject: below --
2010-08-04 16:40 Warren
2010-08-04 17:14 ` Dmitry A. Kazakov
2010-08-05  0:24   ` Warren
2010-08-04 18:46 ` Jeffrey Carter
2010-08-04 19:36   ` Dmitry A. Kazakov
2010-08-05  0:42   ` Warren
2010-08-05  0:55     ` Warren
2010-08-04 19:46 ` Simon Wright
2010-08-05  0:45   ` Warren
2010-08-05  7:33     ` Rolf
2010-08-05 20:50       ` Simon Wright
replies disabled

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