comp.lang.ada
 help / color / mirror / Atom feed
* Re: Passing Ada Proc as a C Function Pointer
@ 2010-08-05 14:31 Warren
  2010-08-05 15:37 ` Warren
  0 siblings, 1 reply; 14+ messages in thread
From: Warren @ 2010-08-05 14:31 UTC (permalink / raw)


On Aug 5, 3:33 am, Rolf <rolf.ebert_nosp...@gmx.net> wrote:
> On 5 Aug., 02:45, Warren <ve3...@gmail.com> wrote:
>
> > On Aug 4, 3:46 pm, Simon Wright <si...@pushface.org> wrote:
>
> > > I would be a bit worried about Thread_Proc's *Ada* environment. There's
> > > this thing called the Secondary Stack, used for - for example - string
> > > catenation; and it's not set up here.
>
> > Yikes, that could be a problem. I vaguely remember something about
> > a "secondary stack".  I'm going to need to research this.
>
> The current version of AVR-Ada does not support the secondary stack.
> It is required for returning unconstrained objects from functions.
> Using unconstrained arrays already consumes so much of the valuable
> RAM that I never felt the need to support returning these objects from
> functions.

Thanks Rolf!

If I hard code the call to the Ada proc from the start thread in the
C code, the threaded code works great now (in Ada!)  So I just got to
work out this business of the C function pointer.

The other issue I have is to sort out the declaration of the Context
object in Ada. If I declare the context in C, things go well.

Attempting to get the C_Context_Type declared correctly is proving
to be a challenge. Here is what I have:

--  typedef enum {
--      ats_normal,
--      ats_wait,
--      ats_clear = 0x40,
--      ats_tick = 0x80
--  } __attribute__((packed)) avr_thread_state;
--
--  typedef struct avr_thread_context {
--      volatile avr_thread_state state;
--      uint8_t* stack_ptr;
--      volatile struct avr_thread_context* next;
--      volatile int16_t timeout;
--      volatile void* waiting_for;
--      volatile struct avr_thread_context* next_waiting;
--      volatile struct avr_thread_context* prev_waiting;
--  } avr_thread_context;

    type C_Context_Type is
        record
            State :         Unsigned_16;
            Stack_Pointer : System.Address;
            Next :          System.Address;
            Timeout :       Unsigned_16;
            Waiting_For :   System.Address;
            Next_Waiting :  System.Address;
            Prev_Waiting :  System.Address;
        end record;

    pragma Convention(C,C_Context_Type);
    pragma Pack(C_Context_Type);
    for C_Context_Type'Size use 16;

Keep in mind I am NOT interested in using the members of
C_Context_Type, but simply trying to set aside storage
that would match the C declaration (in bytes). I've
also tried declaring arrays, but I get a similar
experience (dope vectors etc. balloon the space).

When I compile the above, I get the complaint:

avr_threads.ads:58:33: size for "C_Context_Type" too small, minimum
allowed is 112

112 bytes - yikers!  How do I get this right?

Warren




^ permalink raw reply	[flat|nested] 14+ messages in thread
* Passing Ada Proc as a C Function Pointer
@ 2010-08-04 16:40 Warren
  2010-08-04 17:14 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Warren @ 2010-08-04 16:40 UTC (permalink / raw)


I some types defined as:

  type Thread_Context is private;
  type Stack_Area is new String;
  type Thread_Proc is access procedure;

and an Ada API function:

    procedure Thread_Start(Context : out Thread_Context; Proc :
Thread_Proc; Stack : out Stack_Area) is
        type C_Func_Ptr is access procedure;
        pragma Convention(Convention => C, Entity => C_Func_Ptr);

        procedure avr_thread_start(Context : System.Address;
                    Proc : C_Func_Ptr;
                    Stack : System.Address;
                    Size : Unsigned_16);
        pragma import(C,avr_thread_start,"avr_thread_start");

        Func : C_Func_Ptr := Proc.all'Access;
    begin

 
avr_thread_start(Context'Address,Func,Stack(Stack'First)'Address,Stack'Length);

    end Thread_Start;

Because this part of the Ada code is not working, I'm suspicious that
the passed function pointer is incorrect (I have an all-C thread
example
working).

Is it vital that the Thread_Proc procedure also be declared as

pragma import(C,Proc) ?

I'd prefer not to, if possible.

There are no arguments passed-- I only need to launch the passed
Ada procedure from the C side (in a thread).

The avr_thread_start C routine is declared as:

void avr_thread_start(
  avr_thread_context* context,
  void (*fn)(void),
  uint8_t* stack,
  uint16_t stack_size
);

Warren



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

end of thread, other threads:[~2010-08-05 20:50 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
  -- 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

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