Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callbacks from C to ocaml need nested functions where it shouldn't need to #7340

Closed
vicuna opened this issue Aug 30, 2016 · 7 comments
Closed

Comments

@vicuna
Copy link

vicuna commented Aug 30, 2016

Original bug ID: 7340
Reporter: goswin
Status: feedback (set by @mshinwell on 2016-12-08T07:42:36Z)
Resolution: open
Priority: normal
Severity: feature
Target version: undecided
Category: runtime system and C interface

Bug description

When you have a callback from C to ocaml and support blocking sections then 2 functions are needed over and over again in the same pattern:

    void outer(const char *name) {
        caml_leave_blocking_section();
        inner(name);
        caml_enter_blocking_section();
    }

    void inner(const char *name) {
        CAMLparam0();
        CAMLlocal1(_name);
        _name = caml_copy_string(name);
        caml_callback1(_func, _name);
        CAMLreturn0;
    }

There should be a macro CAMLenter() that combines caml_leave_blocking_section(); with CAMLparam0();
and macros CAMLleave0() and CAMLleaveT(type, arg) that combine caml_enter_blocking_section() with the respective CAMLreturn* macros.

Note: The inner function can never return a value so the equivalent of CAMLreturn(value) makes no sense. Should there be a CAMLleave() that doesn't create a local copy of the return value instead?

@vicuna
Copy link
Author

vicuna commented Aug 31, 2016

Comment author: @ygrek

There is CAMLdrop now, see #4891

@vicuna
Copy link
Author

vicuna commented Sep 8, 2016

Comment author: goswin

Yes, but that's only have the solution imho.

@vicuna
Copy link
Author

vicuna commented Sep 26, 2016

Comment author: @ygrek

You mean "half of the solution"?
I think it is adequate solution and nothing more to fix here.

@vicuna
Copy link
Author

vicuna commented Dec 8, 2016

Comment author: @mshinwell

@Goswin Please explain why you think CAMLdrop is insufficient.

@yallop
Copy link
Member

yallop commented Apr 10, 2019

I think Goswin's concern may be that the call to CAMLlocal1 needs to be preceded by a call to CAMLparam0, and the documentation for CAMLparam0 says:

   The function body must start with one of the [CAMLparam] macros.

so it appears that the body of inner can't be inlined into outer, since that would place CAMLparam0 after the call to caml_leave_blocking_section (i.e. not at the beginning of the function).

I think CAMLparam0 can probably be safely be placed after the call to caml_leave_blocking_section, and the documentation is a bit too restrictive. If that's true then we can fix this issue by clarifying the documentation.

@stedolan
Copy link
Contributor

If compiling with a C90 compiler, the CAMLparam0 must be at the start of a block but that need not be at the start of a function. So the following works:

void foo(const char *name) {
    caml_leave_blocking_section();
    {
        CAMLparam0();
        CAMLlocal1(_name);
        _name = caml_copy_string(name);
        caml_callback1(_func, _name);
        CAMLdrop;
    }
    caml_enter_blocking_section();
}

@github-actions
Copy link

github-actions bot commented May 9, 2020

This issue has been open one year with no activity. Consequently, it is being marked with the "stale" label. What this means is that the issue will be automatically closed in 30 days unless more comments are added or the "stale" label is removed. Comments that provide new information on the issue are especially welcome: is it still reproducible? did it appear in other contexts? how critical is it? etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants