* [Caml-list] "noalloc" + enter/leave blocking section - safe?
@ 2004-08-02 10:43 Markus Mottl
2004-08-02 15:15 ` Markus Mottl
0 siblings, 1 reply; 7+ messages in thread
From: Markus Mottl @ 2004-08-02 10:43 UTC (permalink / raw)
To: OCaml
Hi,
one important question: is it safe to use "noalloc" with
C-functions that contain calls to "caml_enter_blocking_section" and
"caml_leave_blocking_section" to allow other POSIX-treads?
I have the following external function:
(** Synchronize all filesystem buffers with disk. *)
external sync : unit -> unit = "unix_sync" "noalloc"
It is implemented as follows:
CAMLprim value unix_sync()
{
caml_enter_blocking_section();
sync();
caml_leave_blocking_section();
return Val_unit;
}
When removing the "noalloc" attribute, I cannot reproduce the problem
(crash) reported in bug report #3019 anymore, but I'm not sure whether
this is just a coincidence.
Of course, functions with this attribute shouldn't allocate anything on
the OCaml-heap or throw exceptions. But I thought that context-switches
would be safe?
Best regards,
Markus
--
Markus Mottl http://www.oefai.at/~markus markus@oefai.at
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] "noalloc" + enter/leave blocking section - safe?
2004-08-02 10:43 [Caml-list] "noalloc" + enter/leave blocking section - safe? Markus Mottl
@ 2004-08-02 15:15 ` Markus Mottl
2004-08-02 15:34 ` Jere Sanisalo
0 siblings, 1 reply; 7+ messages in thread
From: Markus Mottl @ 2004-08-02 15:15 UTC (permalink / raw)
To: OCaml
On Mon, 02 Aug 2004, Markus Mottl wrote:
> one important question: is it safe to use "noalloc" with
> C-functions that contain calls to "caml_enter_blocking_section" and
> "caml_leave_blocking_section" to allow other POSIX-treads?
Ok, after having done some experiments on my own now, it seems that this
is _not_ safe. Here is a small example that demonstrates this:
file: foo.ml
---------------------------------------------------------------------------
external foo : 'a -> unit = "foo_stub" "noalloc"
let thread () =
let ar = Array.init 1 (fun _ -> "") in
foo ();
let ar = Array.init 1 (fun _ -> "") in
()
let () =
let n = 10 in
let ar = Array.create n () in
let tids = Array.map (Thread.create thread) ar in
Array.iter Thread.join tids
---------------------------------------------------------------------------
file: foo_stubs.c
---------------------------------------------------------------------------
#include <caml/mlvalues.h>
#include <caml/signals.h>
CAMLprim value foo_stub(value v) {
caml_enter_blocking_section();
int i;
for (i = 0; i < 100000000; i++);
caml_leave_blocking_section();
return Val_unit;
}
---------------------------------------------------------------------------
Compiling and linking the above example to native code (i.e. with
POSIX-threads) and running it will almost immediately produce a segfault
on my machine. Removing "noalloc" solves this problem.
Could the documentation on the C-interface please be updated with a
small section on the correct use of "noalloc"? It's not documented at
all right now! (This also closes bug report #3019, I guess).
Regards,
Markus
--
Markus Mottl http://www.oefai.at/~markus markus@oefai.at
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] "noalloc" + enter/leave blocking section - safe?
2004-08-02 15:15 ` Markus Mottl
@ 2004-08-02 15:34 ` Jere Sanisalo
2004-08-02 15:49 ` Falk Hueffner
0 siblings, 1 reply; 7+ messages in thread
From: Jere Sanisalo @ 2004-08-02 15:34 UTC (permalink / raw)
To: OCaml
On Mon, Aug 02, 2004 at 05:15:02PM +0200, Markus Mottl wrote:
>---------------------------------------------------------------------------
>external foo : 'a -> unit = "foo_stub" "noalloc"
Hmm, I wasn't aware that there was a "noalloc" directive. What does it
specifically declare? Does it make ocaml call the natives in a more
efficient manner (and of course the function must not allocate anything)?
Are there any other directives I should be aware of?
And according to the documents the "noalloc" should really be the name of
the native function? Am I really missing something here?
--
Jere Sanisalo [xm@xmunkki.org] - http://www.xmunkki.org/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] "noalloc" + enter/leave blocking section - safe?
2004-08-02 15:34 ` Jere Sanisalo
@ 2004-08-02 15:49 ` Falk Hueffner
2004-08-02 16:02 ` Richard Jones
2004-08-02 16:32 ` Markus Mottl
0 siblings, 2 replies; 7+ messages in thread
From: Falk Hueffner @ 2004-08-02 15:49 UTC (permalink / raw)
To: xm; +Cc: OCaml
Jere Sanisalo <xm@xmunkki.org> writes:
> On Mon, Aug 02, 2004 at 05:15:02PM +0200, Markus Mottl wrote:
>>---------------------------------------------------------------------------
>>external foo : 'a -> unit = "foo_stub" "noalloc"
>
> Hmm, I wasn't aware that there was a "noalloc" directive. What does
> it specifically declare? Does it make ocaml call the natives in a
> more efficient manner (and of course the function must not allocate
> anything)?
It calls the C function directly, with zero overhead. Otherwise, the
call goes through "caml_c_call", which puts the GC state into global
variables for C to pick up, and restores it later from there.
> Are there any other directives I should be aware of?
There's "float", which means the function works on unboxed floats.
--
Falk
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] "noalloc" + enter/leave blocking section - safe?
2004-08-02 15:49 ` Falk Hueffner
@ 2004-08-02 16:02 ` Richard Jones
2004-08-02 16:32 ` Markus Mottl
1 sibling, 0 replies; 7+ messages in thread
From: Richard Jones @ 2004-08-02 16:02 UTC (permalink / raw)
Cc: OCaml
On Mon, Aug 02, 2004 at 05:49:53PM +0200, Falk Hueffner wrote:
> Jere Sanisalo <xm@xmunkki.org> writes:
>
> > On Mon, Aug 02, 2004 at 05:15:02PM +0200, Markus Mottl wrote:
> >>---------------------------------------------------------------------------
> >>external foo : 'a -> unit = "foo_stub" "noalloc"
> >
> > Hmm, I wasn't aware that there was a "noalloc" directive. What does
> > it specifically declare? Does it make ocaml call the natives in a
> > more efficient manner (and of course the function must not allocate
> > anything)?
>
> It calls the C function directly, with zero overhead. Otherwise, the
> call goes through "caml_c_call", which puts the GC state into global
> variables for C to pick up, and restores it later from there.
>
> > Are there any other directives I should be aware of?
>
> There's "float", which means the function works on unboxed floats.
Cool features!
Rich.
--
Richard Jones. http://www.annexia.org/ http://www.j-london.com/
Merjis Ltd. http://www.merjis.com/ - improving website return on investment
PTHRLIB is a library for writing small, efficient and fast servers in C.
HTTP, CGI, DBI, lightweight threads: http://www.annexia.org/freeware/pthrlib/
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [Caml-list] "noalloc" + enter/leave blocking section - safe?
2004-08-02 15:49 ` Falk Hueffner
2004-08-02 16:02 ` Richard Jones
@ 2004-08-02 16:32 ` Markus Mottl
2004-08-02 16:39 ` Falk Hueffner
1 sibling, 1 reply; 7+ messages in thread
From: Markus Mottl @ 2004-08-02 16:32 UTC (permalink / raw)
To: Falk Hueffner; +Cc: xm, OCaml
Falk Hueffner schrieb am Montag, den 02. August 2004:
> > Are there any other directives I should be aware of?
>
> There's "float", which means the function works on unboxed floats.
If I understand asmcomp/cmmgen.ml correctly, this means that the function
arguments and also the return value must all be (unboxed) floats?
Best regards,
Markus
--
Markus Mottl http://www.oefai.at/~markus markus@oefai.at
-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2004-08-02 16:39 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-08-02 10:43 [Caml-list] "noalloc" + enter/leave blocking section - safe? Markus Mottl
2004-08-02 15:15 ` Markus Mottl
2004-08-02 15:34 ` Jere Sanisalo
2004-08-02 15:49 ` Falk Hueffner
2004-08-02 16:02 ` Richard Jones
2004-08-02 16:32 ` Markus Mottl
2004-08-02 16:39 ` Falk Hueffner
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox