From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail1-relais-roc.national.inria.fr (mail1-relais-roc.national.inria.fr [192.134.164.82]) by sympa.inria.fr (Postfix) with ESMTPS id B24727ED7A for ; Mon, 27 Aug 2012 23:07:56 +0200 (CEST) Received-SPF: None (mail1-smtp-roc.national.inria.fr: no sender authenticity information available from domain of andre@digirati.com.br) identity=pra; client-ip=187.73.32.187; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="andre@digirati.com.br"; x-sender="andre@digirati.com.br"; x-conformance=sidf_compatible Received-SPF: Pass (mail1-smtp-roc.national.inria.fr: domain of andre@digirati.com.br designates 187.73.32.187 as permitted sender) identity=mailfrom; client-ip=187.73.32.187; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="andre@digirati.com.br"; x-sender="andre@digirati.com.br"; x-conformance=sidf_compatible; x-record-type="v=spf1" Received-SPF: Pass (mail1-smtp-roc.national.inria.fr: domain of postmaster@mta115.f1.k8.com.br designates 187.73.32.187 as permitted sender) identity=helo; client-ip=187.73.32.187; receiver=mail1-smtp-roc.national.inria.fr; envelope-from="andre@digirati.com.br"; x-sender="postmaster@mta115.f1.k8.com.br"; x-conformance=sidf_compatible; x-record-type="v=spf1" X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AmEBADbhO1C7SSC7nGdsb2JhbABFhgO0eSIBAQEBAQgLCQkUJ4IgAQEFIwQZAQE4DwsYAgImAgIgDQEpEiKHdKcAboNCAQWPQgaBIY0kggqBEps0YIxW X-IronPort-AV: E=Sophos;i="4.80,322,1344204000"; d="scan'208";a="170979915" Received: from mta115.f1.k8.com.br ([187.73.32.187]) by mail1-smtp-roc.national.inria.fr with ESMTP/TLS/ADH-AES256-SHA; 27 Aug 2012 23:07:47 +0200 Received: from localhost (localhost [127.0.0.1]) by smtpz.f1.k8.com.br (Postfix) with ESMTP id 45CA16002D for ; Mon, 27 Aug 2012 21:07:45 +0000 (UTC) X-Virus-Scanned: amavisd-new at k8.com.br Received: from smtpz.f1.k8.com.br ([127.0.0.1]) by localhost (mta115.f1.k8.com.br [127.0.0.1]) (amavisd-new, port 10024) with LMTP id IfwRBJYGv6aF for ; Mon, 27 Aug 2012 21:07:45 +0000 (UTC) Received: from [10.7.5.14] (fosforo.f2.k8.com.br [200.150.156.10]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by smtpz.f1.k8.com.br (Postfix) with ESMTPSA id 0EC486009D for ; Mon, 27 Aug 2012 21:07:44 +0000 (UTC) X-DKIM: OpenDKIM Filter v2.5.2 smtpz.f1.k8.com.br 0EC486009D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=digirati.com.br; s=default; t=1346101665; bh=5+SRCU5D0CpuGCuW+dWDSPxHnT45oxY9G+Fs1QbICKk=; h=Subject:From:To:Date:In-Reply-To:References; b=cmjnU5/l/OrVujL3Xry8JAoRJMFXxIABGk2G1f2aMVJDnCRL2J0Lqy3maYo+8QT03 /9OVMzQBraHYJDRF2kNqG2SyR2jdQKzFC6zYUXsDOyigViH1V+JfTO2qNkzjo3ztkt ovT9RmJZ2eSieW1EXOa2zxddZNXRJcDrbfUIdgIM= Message-ID: <1346101664.4319.8.camel@andre> From: Andre Nathan To: caml-list@inria.fr Date: Mon, 27 Aug 2012 18:07:44 -0300 In-Reply-To: <1346096503.4319.5.camel@andre> References: <1346096503.4319.5.camel@andre> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.2.3-0ubuntu6 Content-Transfer-Encoding: 7bit Mime-Version: 1.0 Subject: Re: [Caml-list] Thread-private data in C stubs 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); }