From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,86f62fb0f98ad93e X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!postnews.google.com!5g2000yqz.googlegroups.com!not-for-mail From: Warren Newsgroups: comp.lang.ada Subject: Re: Passing Ada Proc as a C Function Pointer Date: Thu, 5 Aug 2010 09:51:43 -0700 (PDT) Organization: http://groups.google.com Message-ID: <9a27fbef-37ca-4fdd-ab20-9c30c91645ab@5g2000yqz.googlegroups.com> References: 60b8a494-fed1-4e0f-ba27-a2b7070d5818@k19g2000yqc.googlegroups.com <7002cc94-5207-4b8d-8686-f8b262bfdef0@d17g2000yqb.googlegroups.com> <45573613-9738-4606-95f5-0103670a34e9@h28g2000yqd.googlegroups.com> NNTP-Posting-Host: 216.121.235.102 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1281027103 17258 127.0.0.1 (5 Aug 2010 16:51:43 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 5 Aug 2010 16:51:43 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 5g2000yqz.googlegroups.com; posting-host=216.121.235.102; posting-account=ENgozAkAAACH-stq5yXctoDQeZQP2E6J User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 ( .NET CLR 3.5.30729),gzip(gfe) Xref: g2news1.google.com comp.lang.ada:12883 Date: 2010-08-05T09:51:43-07:00 List-Id: On Aug 5, 11:37=A0am, Warren wrote: > Arg!!!! =A0Arg!!!! > > I keep thinking in terms of bytes - not BITS!! =A0No 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 :=3D 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