caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Indeterminate Initialization: Is it a Bug?
@ 2004-04-01  9:04 Richard Cole
  2004-04-01 13:37 ` Xavier Leroy
  0 siblings, 1 reply; 2+ messages in thread
From: Richard Cole @ 2004-04-01  9:04 UTC (permalink / raw)
  To: caml-list

Hi,

There seems to be a difference in initialization behaviour using the 
native compiler depending on whether a module contains non "external" 
calls. Let me give an example:

#!/bin/bash
rm -f test_funky.ml funky.ml ml_funky.c *funky*.cm* *funky*.a *funky*.so

echo '
  Printf.fprintf stderr "Test.Init\n"; flush stderr ;;
  open Funky ;;
  funky 4 ;;
' > test_funky.ml

echo '
  #include <caml/mlvalues.h>
  #include <caml/memory.h>
  #include <stdio.h>

  value ml_funky(value v)
  {
    CAMLparam1(v);
    int i = Int_val(v) + 1;
    fprintf(stderr, "ML_FUNKY!\n"); fflush(stderr);
    CAMLreturn(Val_int(i));
  };
' > ml_funky.c

echo '
  external funky : int -> int = "ml_funky" ;;
  Printf.fprintf stderr "Funky.Init.\n"; flush stderr ;;
' > funky.ml

gcc -Wall -c ml_funky.c -I/usr/local/godi/lib/ocaml/std-lib/
ocamlopt -c funky.ml
ocamlopt -c test_funky.ml
ocamlmklib -o funky ml_funky.o funky.cmx
ocamlopt -o test_funky.exe funky.cmxa test_funky.cmx -I .
./test_funky.exe

The output doesn't contain "Funky.Init". The problem is that the 
initialization code in the funky module didn't get called. If I wrap the 
functions as in:

#!/bin/bash
echo '
  external ml_funky: int -> int = "ml_funky" ;;
  let funky x = ml_funky x ;;
  Printf.fprintf stderr "Init.\n"; flush stderr ;;
' > funky.ml

ocamlopt -c funky.ml
ocamlmklib -o funky ml_funky.o funky.cmx
ocamlopt -o test_funky.exe funky.cmxa test_funky.ml -I .
./test_funky.exe

Then the initialization code, namely the printf statement in flunky.ml 
is called.

So I have the following question: Is there some way to guarrentee that 
modules are initialized exactly once? Does the development team see the 
current situation, whereby intialization depends on wheher there is a 
non external method in the module that is called, as a problem? Can it 
be fixed?

I should mention that I'm using Godi with godi-ocaml version 3.07pl2 and 
fairly vanilla Redhat 9. Is this problem replicatable on other platforms?

Why do I want to have intialization code always called in my modules. 
Mostly because I want the following initialization code to work without 
having to add a layer of indirection to my wrapper modules.

exception NotFound ;;
let _ = Callback.register "Funky.NotFound" NotFound ;;

best regards,

Richard.


-------------------
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] 2+ messages in thread

* Re: [Caml-list] Indeterminate Initialization: Is it a Bug?
  2004-04-01  9:04 [Caml-list] Indeterminate Initialization: Is it a Bug? Richard Cole
@ 2004-04-01 13:37 ` Xavier Leroy
  0 siblings, 0 replies; 2+ messages in thread
From: Xavier Leroy @ 2004-04-01 13:37 UTC (permalink / raw)
  To: Richard Cole; +Cc: caml-list

> There seems to be a difference in initialization behaviour using the 
> native compiler depending on whether a module contains non "external" 
> calls. [...]

> The output doesn't contain "Funky.Init". The problem is that the 
> initialization code in the funky module didn't get called. If I wrap the 
> functions as in:
> 
> #!/bin/bash
> echo '
>   external ml_funky: int -> int = "ml_funky" ;;
>   let funky x = ml_funky x ;;
>   Printf.fprintf stderr "Init.\n"; flush stderr ;;
> ' > funky.ml

A simpler solution is to define "funky" as an external in the .ml file
but declare it as a regular value in the .mli file:

.ml:  external funky: int -> int = "ml_funky"
.mli: val funky: int -> int

> So I have the following question: Is there some way to guarrentee that 
> modules are initialized exactly once? Does the development team see the 
> current situation, whereby intialization depends on wheher there is a 
> non external method in the module that is called, as a problem? Can it 
> be fixed?

The behavior you observe is a consequence of the link-time elimination
of unreferenced .cma or .cmxa members.  An "external" declaration in a
module interface is like a type declaration: using it elsewhere
doesn't create a reference to the implementation of the defining module.

You can turn off this link-time elimination using the -linkall flag,
either at library creation time (ocamlmklib), or at link-time (ocamlopt).
Then, all modules will be initialized exactly once and in the order
given, like you expect.

- Xavier Leroy

-------------------
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] 2+ messages in thread

end of thread, other threads:[~2004-04-01 13:39 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-04-01  9:04 [Caml-list] Indeterminate Initialization: Is it a Bug? Richard Cole
2004-04-01 13:37 ` Xavier Leroy

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).