Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Andre Nathan <andre@digirati.com.br>
To: caml-list@inria.fr
Subject: Re: [Caml-list] Thread-private data in C stubs
Date: Mon, 27 Aug 2012 18:07:44 -0300	[thread overview]
Message-ID: <1346101664.4319.8.camel@andre> (raw)
In-Reply-To: <1346096503.4319.5.camel@andre>

On Mon, 2012-08-27 at 16:41 -0300, Andre Nathan wrote:
> Hello
> 
> I'm writing stubs to a C library uses pthreads and exposes two functions
> to get and set thread-private data, getpriv(ctx) and setpriv(ctx, p)
> where ctx is some opaque context object, and p is a void* pointer.
> 
> What's the best way to deal with this from the stub code, ie., how do I
> avoid the thread-private data to be garbage collected when the OCaml
> variable goes out of scope?
> 
> Since the library in question uses threads internally, this needs to be
> a thread-safe solution.

For the record, with help from #ocaml, I got it to work like this:

struct priv {
  value v;
};

CAMLprim value
caml_setpriv(value ctx_val, value priv_val)
{
    CAMLparam2(ctx_val, priv_val);
    context *ctx = (context *)ctx_val;
    struct priv *p;
   
    p = getpriv(ctx);
    if (p != NULL) {
        caml_remove_global_root(&(p->v));
        caml_stat_free(p);
    }

    p = caml_stat_alloc(sizeof(*p));
    p->v = priv_val;
    ret = setpriv(ctx, p);
    caml_register_global_root(&(p->v));

    CAMLreturn(Val_unit);
}


  reply	other threads:[~2012-08-27 21:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-27 19:41 Andre Nathan
2012-08-27 21:07 ` Andre Nathan [this message]
     [not found] <fa.P6AfawLTOdz6VcNoVpOatntnvpI@ifi.uio.no>
     [not found] ` <fa.rchhrYogU61pR6TqMyzDz8fSIoA@ifi.uio.no>
2012-08-28 11:48   ` jonathan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1346101664.4319.8.camel@andre \
    --to=andre@digirati.com.br \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox