From 7ab33f49185d5a416198f1e0bf5a2ca9f347bbac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 10 Dec 2009 10:21:55 +0100 Subject: Change the expected return value for on_load functions An on_load function is supposed to return 'true' to indicate that the module should be loaded, and 'false' if it should be unloaded. But returning any other term, as well as causing an exception, will also unload the module. Since we don't like boolean values mixed with other values, change the expected return value as follows: * If 'ok' is returned, the module will remain loaded and become callable. * If any other value is returned (or an exception is generated), the module will be unloaded. Also, if the returned value is not an atom, send a warning message to the error_logger (using error_logger:warning_msg/2). The new interpretation of the return value means that an on_load function can now directly return the return value from erlang:load_nif/2. --- system/doc/reference_manual/code_loading.xml | 34 +++++++++++++++++++--------- 1 file changed, 23 insertions(+), 11 deletions(-) (limited to 'system/doc') diff --git a/system/doc/reference_manual/code_loading.xml b/system/doc/reference_manual/code_loading.xml index 0e148298b1..f56e1ff408 100644 --- a/system/doc/reference_manual/code_loading.xml +++ b/system/doc/reference_manual/code_loading.xml @@ -121,8 +121,10 @@ loop() -> Running a function when a module is loaded -

This section describes an experimental feature introduced in R13B03. - There may be backward-incompatible changes in the feature in future releases.

+

This section describes an experimental feature that was + introduced in R13B03, and changed in a backwards-incompatible + way in R13B04. There may be more backward-incompatible changes + in future releases.

The -on_load() directive names a function that should @@ -133,25 +135,35 @@ loop() ->

It is not necessary to export the function. It will be called in a freshly spawned process (which will be terminated as soon as the function - returns). The function must return true if the module is to - be remained loaded and be callable, or false if the module - is to be unloaded. Returning any other value or generating an exception - will also cause the module to be unloaded.

+ returns). The function must return ok if the module is to + be remained loaded and become callable, or any other value if the module + is to be unloaded. Generating an exception will also cause the + module to be unloaded. If the return value is not an atom, + a warning error report will be sent to the error logger.

A process that calls any function in a module whose on_load function has not yet returned will be suspended until the on_load function has returned.

+

In embedded mode, all modules will be loaded first and then + will all on_load functions be called. The system will be + terminated unless all of the on_load functions return + ok

. +

Example:

 -module(m).
--on_load(run_me/0).
+-on_load(load_my_nifs/0).
+
+load_my_nifs() ->
+    NifPath = ...,    %Set up the path to the NIF library.
+    Info = ...,       %Initialize the Info term
+    erlang:load_nif(NifPath, Info).
-run_me() -> - %% Do something with side effects here, for instance load a library - %% containing native-implemented functions. - true. +

If the call to erlang:load_nif/2 fails, the module + will be unloaded and there will be warning report sent to + the error loader.

-- cgit v1.2.3