Just to tag onto Michael's response: I agree that we want to support this
capability more directly. My thought has been to treat arguments of
function type in the extern procedure declaration context differently,
using a raw function pointer. This seems thematically similar to how we
treat extern array formal arguments like C arrays rather than Chapel array
descriptors.
(For that matter, it'd be nice to optimize pass-function-within-Chapel
cases for which a function pointer would be sufficient to avoid the
rigamarole of the class/method-based approach).
-Brad
On Wed, 15 Apr 2015, Michael Ferguson wrote:
> Hi Brian -
>
> This won't work very well right now since Chapel's mechanism
> for capturing functions is built upon classes and virtual
> method calls - and also since currently the C interoperability
> support doesn't understand function pointers.
>
> I'd like to fix both of those things....
>
> In the mean time, I think that the best you can do is to:
> * declare an extern proc in Chapel
> * create a C function calling the extern proc
> * tell Chapel that the C function is an opaque
>
> Here is an example:
>
> $ cat function_ptr.chpl
>
> export proc proc_one() { writeln("in proc_one"); }
>
> proc_one();
>
> extern var call_proc_one:opaque;
> extern proc call_fn_ptr(x);
>
> call_fn_ptr(call_proc_one);
>
>
> $ cat function_ptr.h
>
> void proc_one(void);
>
>
> static void call_proc_one(void) { proc_one(); }
> static void call_fn_ptr(void (*fn)(void)) { fn(); }
>
>
> $ chpl function_ptr.chpl function_ptr.h
> $ ./a.out
> in proc_one
> in proc_one
>
>
>
>
>
>
>
> Does this workaround solve your problem?
>
> Cheers,
>
> -michael
>
> On 4/15/15, 12:14 PM, "Brian Guarraci" <[email protected]> wrote:
>
>> Hi,
>>
>>
>> What's the best way to declare an external C function that takes a
>> function pointer? I want to pass a Chapel implemented function or an
>> externally implemented function. I've done some research and experiments
>> and haven't found a good solution yet.
>>
>>
>> I originally (and naively) began with something like this in the Chapel
>> module:
>>
>>
>> proc ev_io_init_fn(ref loop: ev_loop_t, ref io: ev_io_t, x: c_int);
>> extern proc ev_io_init(ref io: ev_io_t, ref fn: ev_io_init_fn, fd:
>> ev_fd_t, events: ev_events_t);
>>
>>
>>
>> and then in chpl:
>>
>>
>> export proc accept_cb(ref loop: ev_loop_t, ref watcher: ev_io_t, revents:
>> c_int) {
>> // do work
>> }
>>
>>
>>
>> ev_io_init(w_accept, accept_cb, sd, EV_READ);
>>
>>
>>
>> but this (and similar approaches) will produce a compilation error akin
>> to:
>>
>>
>> chapel/chpl_tcp_server.chpl:38: error: unresolved call
>> 'ev_io_init(c_ptr(ev_io_t),
>> chpl__fcf_type__ref_ev_loop_t__ref_ev_io_t_int32_t_void, int(32),
>> int(32))' [functionResolution.cpp:2448]
>> chapel/LibEv.chpl:75: note: candidates are: ev_io_init(io:
>> c_ptr(ev_io_t), ref fn: ev_io_init_fn, fd: ev_fd_t, events: ev_events_t)
>> [128267] [functionResolution.cpp:2463]
>>
>>
>>
>>
>> Thanks!
>> Brian
>>
>>
>>
>>
>>
>
>
> ------------------------------------------------------------------------------
> BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
> Develop your own process in accordance with the BPMN 2 standard
> Learn Process modeling best practices with Bonita BPM through live exercises
> http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
> source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
> _______________________________________________
> Chapel-developers mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/chapel-developers
>
------------------------------------------------------------------------------
BPM Camp - Free Virtual Workshop May 6th at 10am PDT/1PM EDT
Develop your own process in accordance with the BPMN 2 standard
Learn Process modeling best practices with Bonita BPM through live exercises
http://www.bonitasoft.com/be-part-of-it/events/bpm-camp-virtual- event?utm_
source=Sourceforge_BPM_Camp_5_6_15&utm_medium=email&utm_campaign=VA_SF
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers