Clean up startup/shutdown hook code

db4
Slava Pestov 2009-11-20 00:55:16 -06:00
parent 8cbe676062
commit 24c3ddbbde
11 changed files with 85 additions and 83 deletions

View File

@ -1,19 +1,17 @@
USING: init command-line debugger system continuations USING: init command-line debugger system continuations
namespaces eval kernel vocabs.loader io destructors ; namespaces eval kernel vocabs.loader io ;
[ [
boot boot
do-startup-hooks
[ [
do-startup-hooks (command-line) parse-command-line
[ load-vocab-roots
(command-line) parse-command-line run-user-init
load-vocab-roots "e" get [ eval( -- ) ] when*
run-user-init ignore-cli-args? not script get and
"e" get [ eval( -- ) ] when* [ run-script ] [ "run" get run ] if*
ignore-cli-args? not script get and output-stream get [ stream-flush ] when*
[ run-script ] [ "run" get run ] if* 0 exit
output-stream get [ stream-flush ] when* ] [ print-error 1 exit ] recover
0 ] set-startup-quot
] [ print-error 1 ] recover
] with-destructors exit
] set-boot-quot

View File

@ -1,12 +1,10 @@
USING: destructors init command-line system namespaces kernel USING: init command-line system namespaces kernel vocabs.loader io ;
vocabs.loader io ;
[ [
boot boot
[ do-startup-hooks
do-startup-hooks (command-line) parse-command-line
(command-line) parse-command-line "run" get run
"run" get run output-stream get [ stream-flush ] when*
output-stream get [ stream-flush ] when* 0 exit
] with-destructors 0 exit ] set-startup-quot
] set-boot-quot

View File

@ -145,7 +145,7 @@ SYMBOL: architecture
RESET RESET
! Boot quotation, set in stage1.factor ! Boot quotation, set in stage1.factor
USERENV: bootstrap-boot-quot 20 USERENV: bootstrap-startup-quot 20
! Bootstrap global namesapce ! Bootstrap global namesapce
USERENV: bootstrap-global 21 USERENV: bootstrap-global 21

View File

@ -446,7 +446,7 @@ SYMBOL: deploy-vocab
: [print-error] ( -- word ) "print-error" "debugger" lookup ; : [print-error] ( -- word ) "print-error" "debugger" lookup ;
: deploy-boot-quot ( word -- ) : deploy-startup-quot ( word -- )
[ [
[ boot ] % [ boot ] %
startup-hooks get values concat % startup-hooks get values concat %
@ -466,7 +466,7 @@ SYMBOL: deploy-vocab
strip-io? [ [ flush ] % ] unless strip-io? [ [ flush ] % ] unless
[ 0 exit ] % [ 0 exit ] %
] [ ] make ] [ ] make
set-boot-quot ; set-startup-quot ;
: startup-stripper ( -- ) : startup-stripper ( -- )
t "quiet" set-global t "quiet" set-global
@ -521,7 +521,7 @@ SYMBOL: deploy-vocab
strip-default-methods strip-default-methods
strip-compiler-classes strip-compiler-classes
f 5 setenv ! we can't use the Factor debugger or Factor I/O anymore f 5 setenv ! we can't use the Factor debugger or Factor I/O anymore
deploy-vocab get vocab-main deploy-boot-quot deploy-vocab get vocab-main deploy-startup-quot
find-megamorphic-caches find-megamorphic-caches
stripped-word-props stripped-word-props
stripped-globals strip-globals stripped-globals strip-globals

View File

@ -37,7 +37,7 @@ load-help? off
[ [
"resource:basis/bootstrap/stage2.factor" "resource:basis/bootstrap/stage2.factor"
dup exists? [ dup exists? [
[ run-file ] with-destructors run-file
] [ ] [
"Cannot find " write write "." print "Cannot find " write write "." print
"Please move " write image write " to the same directory as the Factor sources," print "Please move " write image write " to the same directory as the Factor sources," print
@ -46,4 +46,4 @@ load-help? off
] if ] if
] % ] %
] [ ] make ] [ ] make
bootstrap.image.private:bootstrap-boot-quot set bootstrap.image.private:bootstrap-startup-quot set

View File

@ -6,8 +6,6 @@ IN: destructors
SYMBOL: disposables SYMBOL: disposables
[ H{ } clone disposables set-global ] "destructors" add-startup-hook
ERROR: already-unregistered disposable ; ERROR: already-unregistered disposable ;
SYMBOL: debug-leaks? SYMBOL: debug-leaks?
@ -89,6 +87,12 @@ PRIVATE>
] with-scope ; inline ] with-scope ; inline
[ [
always-destructors get-global H{ } clone disposables set-global
error-destructors get-global append dispose-each V{ } clone always-destructors set-global
] "destructors.global" add-shutdown-hook V{ } clone error-destructors set-global
] "destructors" add-startup-hook
[
do-always-destructors
do-error-destructors
] "destructors" add-shutdown-hook

View File

@ -4,13 +4,13 @@ IN: init
HELP: boot HELP: boot
{ $description "Called on startup as part of the boot quotation to initialize the runtime and prepare it for running user code." } ; { $description "Called on startup as part of the boot quotation to initialize the runtime and prepare it for running user code." } ;
{ boot boot-quot set-boot-quot } related-words { boot startup-quot set-startup-quot } related-words
HELP: boot-quot HELP: startup-quot
{ $values { "quot" quotation } } { $values { "quot" quotation } }
{ $description "Outputs the initial quotation called by the VM on startup." } ; { $description "Outputs the initial quotation called by the VM on startup." } ;
HELP: set-boot-quot HELP: set-startup-quot
{ $values { "quot" quotation } } { $values { "quot" quotation } }
{ $description "Sets the initial quotation called by the VM on startup. This quotation must begin with a call to " { $link boot } ". The image must be saved for changes to the boot quotation to take effect." } { $description "Sets the initial quotation called by the VM on startup. This quotation must begin with a call to " { $link boot } ". The image must be saved for changes to the boot quotation to take effect." }
{ $notes "The " { $link "tools.deploy" } " tool uses this word." } ; { $notes "The " { $link "tools.deploy" } " tool uses this word." } ;
@ -44,8 +44,8 @@ ARTICLE: "init" "Initialization and startup"
{ $subsections add-shutdown-hook } { $subsections add-shutdown-hook }
"The boot quotation can be changed:" "The boot quotation can be changed:"
{ $subsections { $subsections
boot-quot startup-quot
set-boot-quot set-startup-quot
} }
"When quitting Factor, shutdown hooks are called:" "When quitting Factor, shutdown hooks are called:"
{ $subsection do-shutdown-hooks } ; { $subsection do-shutdown-hooks } ;

View File

@ -27,12 +27,12 @@ shutdown-hooks global [ drop V{ } clone ] cache drop
: boot ( -- ) init-namespaces init-catchstack init-error-handler ; : boot ( -- ) init-namespaces init-catchstack init-error-handler ;
: boot-quot ( -- quot ) 20 getenv ; : startup-quot ( -- quot ) 20 getenv ;
: set-boot-quot ( quot -- ) 20 setenv ; : set-startup-quot ( quot -- ) 20 setenv ;
: shutdown-quot ( -- quot ) 67 getenv ; : shutdown-quot ( -- quot ) 22 getenv ;
: set-shutdown-quot ( quot -- ) 67 setenv ; : set-shutdown-quot ( quot -- ) 22 setenv ;
[ do-shutdown-hooks ] set-shutdown-quot [ do-shutdown-hooks ] set-shutdown-quot

View File

@ -167,14 +167,14 @@ void factor_vm::start_factor(vm_parameters *p)
if(p->fep) factorbug(); if(p->fep) factorbug();
nest_stacks(NULL); nest_stacks(NULL);
c_to_factor_toplevel(special_objects[OBJ_BOOT]); c_to_factor_toplevel(special_objects[OBJ_STARTUP_QUOT]);
unnest_stacks(); unnest_stacks();
} }
void factor_vm::stop_factor() void factor_vm::stop_factor()
{ {
nest_stacks(NULL); nest_stacks(NULL);
c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN]); c_to_factor_toplevel(special_objects[OBJ_SHUTDOWN_QUOT]);
unnest_stacks(); unnest_stacks();
} }

View File

@ -291,7 +291,7 @@ bool factor_vm::save_image(const vm_char *filename)
h.bignum_neg_one = bignum_neg_one; h.bignum_neg_one = bignum_neg_one;
for(cell i = 0; i < special_object_count; i++) for(cell i = 0; i < special_object_count; i++)
h.special_objects[i] = (save_env_p(i) ? special_objects[i] : false_object); h.special_objects[i] = (save_special_p(i) ? special_objects[i] : false_object);
bool ok = true; bool ok = true;
@ -326,7 +326,7 @@ void factor_vm::primitive_save_image_and_exit()
/* strip out special_objects data which is set on startup anyway */ /* strip out special_objects data which is set on startup anyway */
for(cell i = 0; i < special_object_count; i++) for(cell i = 0; i < special_object_count; i++)
if(!save_env_p(i)) special_objects[i] = false_object; if(!save_special_p(i)) special_objects[i] = false_object;
gc(collect_compact_op, gc(collect_compact_op,
0, /* requested size */ 0, /* requested size */

View File

@ -4,40 +4,40 @@ namespace factor
static const cell special_object_count = 70; static const cell special_object_count = 70;
enum special_object { enum special_object {
OBJ_NAMESTACK, /* used by library only */ OBJ_NAMESTACK, /* used by library only */
OBJ_CATCHSTACK, /* used by library only, per-callback */ OBJ_CATCHSTACK, /* used by library only, per-callback */
OBJ_CURRENT_CALLBACK = 2, /* used by library only, per-callback */ OBJ_CURRENT_CALLBACK = 2, /* used by library only, per-callback */
OBJ_WALKER_HOOK, /* non-local exit hook, used by library only */ OBJ_WALKER_HOOK, /* non-local exit hook, used by library only */
OBJ_CALLCC_1, /* used to pass the value in callcc1 */ OBJ_CALLCC_1, /* used to pass the value in callcc1 */
OBJ_BREAK = 5, /* quotation called by throw primitive */ OBJ_BREAK = 5, /* quotation called by throw primitive */
OBJ_ERROR, /* a marker consed onto kernel errors */ OBJ_ERROR, /* a marker consed onto kernel errors */
OBJ_CELL_SIZE = 7, /* sizeof(cell) */ OBJ_CELL_SIZE = 7, /* sizeof(cell) */
OBJ_CPU, /* CPU architecture */ OBJ_CPU, /* CPU architecture */
OBJ_OS, /* operating system name */ OBJ_OS, /* operating system name */
OBJ_ARGS = 10, /* command line arguments */ OBJ_ARGS = 10, /* command line arguments */
OBJ_STDIN, /* stdin FILE* handle */ OBJ_STDIN, /* stdin FILE* handle */
OBJ_STDOUT, /* stdout FILE* handle */ OBJ_STDOUT, /* stdout FILE* handle */
OBJ_IMAGE = 13, /* image path name */ OBJ_IMAGE = 13, /* image path name */
OBJ_EXECUTABLE, /* runtime executable path name */ OBJ_EXECUTABLE, /* runtime executable path name */
OBJ_EMBEDDED = 15, /* are we embedded in another app? */ OBJ_EMBEDDED = 15, /* are we embedded in another app? */
OBJ_EVAL_CALLBACK, /* used when Factor is embedded in a C app */ OBJ_EVAL_CALLBACK, /* used when Factor is embedded in a C app */
OBJ_YIELD_CALLBACK, /* used when Factor is embedded in a C app */ OBJ_YIELD_CALLBACK, /* used when Factor is embedded in a C app */
OBJ_SLEEP_CALLBACK, /* used when Factor is embedded in a C app */ OBJ_SLEEP_CALLBACK, /* used when Factor is embedded in a C app */
OBJ_COCOA_EXCEPTION = 19, /* Cocoa exception handler quotation */ OBJ_COCOA_EXCEPTION = 19, /* Cocoa exception handler quotation */
OBJ_BOOT = 20, /* boot quotation */ OBJ_STARTUP_QUOT = 20, /* startup quotation */
OBJ_GLOBAL, /* global namespace */ OBJ_GLOBAL, /* global namespace */
OBJ_SHUTDOWN, OBJ_SHUTDOWN_QUOT, /* shutdown quotation */
/* Quotation compilation in quotations.c */ /* Quotation compilation in quotations.c */
JIT_PROLOG = 23, JIT_PROLOG = 23,
JIT_PRIMITIVE_WORD, JIT_PRIMITIVE_WORD,
JIT_PRIMITIVE, JIT_PRIMITIVE,
JIT_WORD_JUMP, JIT_WORD_JUMP,
@ -61,14 +61,14 @@ enum special_object {
JIT_DECLARE_WORD, JIT_DECLARE_WORD,
/* Callback stub generation in callbacks.c */ /* Callback stub generation in callbacks.c */
CALLBACK_STUB = 45, CALLBACK_STUB = 45,
/* Incremented on every modify-code-heap call; invalidates call( inline /* Incremented on every modify-code-heap call; invalidates call( inline
caching */ caching */
REDEFINITION_COUNTER = 46, REDEFINITION_COUNTER = 46,
/* Polymorphic inline cache generation in inline_cache.c */ /* Polymorphic inline cache generation in inline_cache.c */
PIC_LOAD = 47, PIC_LOAD = 47,
PIC_TAG, PIC_TAG,
PIC_TUPLE, PIC_TUPLE,
PIC_CHECK_TAG, PIC_CHECK_TAG,
@ -78,27 +78,29 @@ enum special_object {
PIC_MISS_TAIL_WORD, PIC_MISS_TAIL_WORD,
/* Megamorphic cache generation in dispatch.c */ /* Megamorphic cache generation in dispatch.c */
MEGA_LOOKUP = 57, MEGA_LOOKUP = 57,
MEGA_LOOKUP_WORD, MEGA_LOOKUP_WORD,
MEGA_MISS_WORD, MEGA_MISS_WORD,
OBJ_UNDEFINED = 60, /* default quotation for undefined words */ OBJ_UNDEFINED = 60, /* default quotation for undefined words */
OBJ_STDERR = 61, /* stderr FILE* handle */ OBJ_STDERR = 61, /* stderr FILE* handle */
OBJ_STAGE2 = 62, /* have we bootstrapped? */ OBJ_STAGE2 = 62, /* have we bootstrapped? */
OBJ_CURRENT_THREAD = 63, OBJ_CURRENT_THREAD = 63,
OBJ_THREADS = 64, OBJ_THREADS = 64,
OBJ_RUN_QUEUE = 65, OBJ_RUN_QUEUE = 65,
OBJ_SLEEP_QUEUE = 66, OBJ_SLEEP_QUEUE = 66,
}; };
#define OBJ_FIRST_SAVE OBJ_BOOT /* save-image-and-exit discards special objects that are filled in on startup
anyway, to reduce image size */
#define OBJ_FIRST_SAVE OBJ_STARTUP_QUOT
#define OBJ_LAST_SAVE OBJ_STAGE2 #define OBJ_LAST_SAVE OBJ_STAGE2
inline static bool save_env_p(cell i) inline static bool save_special_p(cell i)
{ {
return (i >= OBJ_FIRST_SAVE && i <= OBJ_LAST_SAVE); return (i >= OBJ_FIRST_SAVE && i <= OBJ_LAST_SAVE);
} }