diff options
author | Raimo Niskanen <[email protected]> | 2012-03-22 14:29:55 +0100 |
---|---|---|
committer | Raimo Niskanen <[email protected]> | 2012-03-22 15:41:18 +0100 |
commit | f67487b67bfb9ab04b01ef87bbfc39a24f604f11 (patch) | |
tree | 3029c7a42a621670cdf4834a3712bca2f9376b9d /erts | |
parent | 5d7ed57d07118f95cf970ee655ea8eea76cbe6fd (diff) | |
parent | 1e13b92d5c6543c82219610aa1336dbdf1f4dc2d (diff) | |
download | otp-f67487b67bfb9ab04b01ef87bbfc39a24f604f11.tar.gz otp-f67487b67bfb9ab04b01ef87bbfc39a24f604f11.tar.bz2 otp-f67487b67bfb9ab04b01ef87bbfc39a24f604f11.zip |
Merge branch 'maint'
Conflicts:
erts/doc/src/erlang.xml
erts/emulator/beam/erl_process.c
erts/emulator/beam/erl_process.h
erts/emulator/test/bif_SUITE.erl
erts/preloaded/ebin/erlang.beam
erts/preloaded/src/erlang.erl
lib/hipe/cerl/erl_bif_types.erl
Diffstat (limited to 'erts')
-rw-r--r-- | erts/doc/src/erlang.xml | 59 | ||||
-rw-r--r-- | erts/emulator/beam/bif.c | 113 | ||||
-rw-r--r-- | erts/emulator/beam/bif.tab | 4 | ||||
-rw-r--r-- | erts/emulator/beam/erl_init.c | 91 | ||||
-rw-r--r-- | erts/emulator/beam/erl_process.c | 119 | ||||
-rw-r--r-- | erts/emulator/beam/erl_process.h | 6 | ||||
-rw-r--r-- | erts/emulator/beam/global.h | 11 | ||||
-rw-r--r-- | erts/emulator/beam/io.c | 7 | ||||
-rw-r--r-- | erts/emulator/beam/sys.h | 4 | ||||
-rw-r--r-- | erts/emulator/test/bif_SUITE.erl | 56 | ||||
-rw-r--r-- | erts/ntbuild.erl | 332 | ||||
-rw-r--r-- | erts/preloaded/ebin/erlang.beam | bin | 87216 -> 87488 bytes | |||
-rw-r--r-- | erts/preloaded/src/erlang.erl | 13 |
13 files changed, 369 insertions, 446 deletions
diff --git a/erts/doc/src/erlang.xml b/erts/doc/src/erlang.xml index 3664bdf76d..2ead6f511a 100644 --- a/erts/doc/src/erlang.xml +++ b/erts/doc/src/erlang.xml @@ -1236,26 +1236,61 @@ true <name name="halt" arity="0"/> <fsummary>Halt the Erlang runtime system and indicate normal exit to the calling environment</fsummary> <desc> - <p>Halts the Erlang runtime system and indicates normal exit to - the calling environment. Has no return value.</p> + <p>The same as + <seealso marker="#halt/2"><c>halt(0, [])</c></seealso>.</p> <pre> > <input>halt().</input> -os_prompt%</pre> +os_prompt% </pre> </desc> </func> <func> <name name="halt" arity="1"/> <fsummary>Halt the Erlang runtime system</fsummary> <desc> - <p><c><anno>Status</anno></c> must be a non-negative integer, or a string. - Halts the Erlang runtime system. Has no return value. - If <c><anno>Status</anno></c> is an integer, it is returned as an exit - status of Erlang to the calling environment. - If <c><anno>Status</anno></c> is a string, produces an Erlang crash dump - with <c><anno>Status</anno></c> as slogan, and then exits with a non-zero - status code.</p> - <p>Note that on many platforms, only the status codes 0-255 are - supported by the operating system.</p> + <p>The same as + <seealso marker="#halt/2"><c>halt(<anno>Status</anno>, [])</c></seealso>.</p> + <pre> +> <input>halt(17).</input> +os_prompt% <input>echo $?</input> +17 +os_prompt% </pre> + </desc> + </func> + <func> + <name>halt(Status, Options)</name> + <fsummary>Halt the Erlang runtime system</fsummary> + <desc> + <p><c><anno>Status</anno></c> must be a non-negative integer, a string, + or the atom <c>abort</c>. + Halts the Erlang runtime system. Has no return value. + Depending on <c><anno>Status</anno></c>: + </p> + <taglist> + <tag>integer()</tag> + <item>The runtime system exits with the integer value <c><anno>Status</anno></c> + as status code to the calling environment (operating system). + </item> + <tag>string()</tag> + <item>An erlang crash dump is produced with <c><anno>Status</anno></c> as slogan, + and then the runtime system exits with status code <c>1</c>. + </item> + <tag><c>abort</c></tag> + <item> + The runtime system aborts producing a core dump, if that is + enabled in the operating system. + </item> + </taglist> + <p>Note that on many platforms, only the status codes 0-255 are + supported by the operating system. + </p> + <p>For integer <c><anno>Status</anno></c> the Erlang runtime system closes all ports + and allows async threads to finish their operations before exiting. + To exit without such flushing use + <c><anno>Option</anno></c> as <c>{flush,false}</c>. + </p> + <p>For statuses <c>string()</c> and <c>abort</c> the <c>flush</c> + option is ignored and flushing is <em>not</em> done. + </p> </desc> </func> <func> diff --git a/erts/emulator/beam/bif.c b/erts/emulator/beam/bif.c index 9b2394d92f..d39beea1f3 100644 --- a/erts/emulator/beam/bif.c +++ b/erts/emulator/beam/bif.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 1996-2011. All Rights Reserved. + * Copyright Ericsson AB 1996-2012. All Rights Reserved. * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in @@ -3665,43 +3665,122 @@ BIF_RETTYPE display_nl_0(BIF_ALIST_0) /* ARGSUSED */ BIF_RETTYPE halt_0(BIF_ALIST_0) { - VERBOSE(DEBUG_SYSTEM,("System halted by BIF halt/0\n")); - erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN); - erl_exit(0, ""); - return NIL; /* Pedantic (lint does not know about erl_exit) */ + VERBOSE(DEBUG_SYSTEM,("System halted by BIF halt()\n")); + erl_halt(0); + ERTS_BIF_YIELD1(bif_export[BIF_halt_1], BIF_P, am_undefined); } /**********************************************************************/ -#define MSG_SIZE 200 +#define HALT_MSG_SIZE 200 +static char halt_msg[HALT_MSG_SIZE]; /* stop the system with exit code */ /* ARGSUSED */ BIF_RETTYPE halt_1(BIF_ALIST_1) { Sint code; - static char msg[MSG_SIZE]; - int i; if (is_small(BIF_ARG_1) && (code = signed_val(BIF_ARG_1)) >= 0) { - VERBOSE(DEBUG_SYSTEM,("System halted by BIF halt(%d)\n", code)); + VERBOSE(DEBUG_SYSTEM,("System halted by BIF halt(%T)\n", BIF_ARG_1)); + erl_halt((int)(- code)); + ERTS_BIF_YIELD1(bif_export[BIF_halt_1], BIF_P, am_undefined); + } + else if (ERTS_IS_ATOM_STR("abort", BIF_ARG_1)) { + VERBOSE(DEBUG_SYSTEM,("System halted by BIF halt(%T)\n", BIF_ARG_1)); erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN); - erl_exit(-code, ""); - } else if (is_string(BIF_ARG_1) || BIF_ARG_1 == NIL) { - if ((i = intlist_to_buf(BIF_ARG_1, msg, MSG_SIZE-1)) < 0) { + erl_exit(ERTS_ABORT_EXIT, ""); + } + else if (is_string(BIF_ARG_1) || BIF_ARG_1 == NIL) { + int i; + + if ((i = intlist_to_buf(BIF_ARG_1, halt_msg, HALT_MSG_SIZE-1)) < 0) { goto error; } - msg[i] = '\0'; - VERBOSE(DEBUG_SYSTEM,("System halted by BIF halt(%s)\n", msg)); + halt_msg[i] = '\0'; + VERBOSE(DEBUG_SYSTEM,("System halted by BIF halt(%T)\n", BIF_ARG_1)); erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN); - erl_exit(ERTS_DUMP_EXIT, "%s\n", msg); - } else { - error: + erl_exit(ERTS_DUMP_EXIT, "%s\n", halt_msg); + } + else + goto error; + return NIL; /* Pedantic (lint does not know about erl_exit) */ + error: BIF_ERROR(BIF_P, BADARG); +} + +/**********************************************************************/ + +/* stop the system with exit code and flags */ +/* ARGSUSED */ +BIF_RETTYPE halt_2(BIF_ALIST_2) +{ + Sint code; + Eterm optlist = BIF_ARG_2; + int flush = 0; + + for (optlist = BIF_ARG_2; + is_list(optlist); + optlist = CDR(list_val(optlist))) { + Eterm *tp, opt = CAR(list_val(optlist)); + if (is_not_tuple(opt)) + goto error; + tp = tuple_val(opt); + if (tp[0] != make_arityval(2)) + goto error; + if (tp[1] == am_flush) { + if (tp[2] == am_true) + flush = 1; + else if (tp[2] == am_false) + flush = 0; + else + goto error; + } + else + goto error; + } + if (is_not_nil(optlist)) + goto error; + + if (is_small(BIF_ARG_1) && (code = signed_val(BIF_ARG_1)) >= 0) { + VERBOSE(DEBUG_SYSTEM, + ("System halted by BIF halt(%T, %T)\n", BIF_ARG_1, BIF_ARG_2)); + if (flush) { + erl_halt((int)(- code)); + ERTS_BIF_YIELD1(bif_export[BIF_halt_1], BIF_P, am_undefined); + } + else { + erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN); + erl_exit((int)(- code), ""); + } + } + else if (ERTS_IS_ATOM_STR("abort", BIF_ARG_1)) { + VERBOSE(DEBUG_SYSTEM, + ("System halted by BIF halt(%T, %T)\n", BIF_ARG_1, BIF_ARG_2)); + erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN); + erl_exit(ERTS_ABORT_EXIT, ""); } + else if (is_string(BIF_ARG_1) || BIF_ARG_1 == NIL) { + int i; + + if ((i = intlist_to_buf(BIF_ARG_1, halt_msg, HALT_MSG_SIZE-1)) < 0) { + goto error; + } + halt_msg[i] = '\0'; + VERBOSE(DEBUG_SYSTEM, + ("System halted by BIF halt(%T, %T)\n", BIF_ARG_1, BIF_ARG_2)); + erts_smp_proc_unlock(BIF_P, ERTS_PROC_LOCK_MAIN); + erl_exit(ERTS_DUMP_EXIT, "%s\n", halt_msg); + } + else + goto error; return NIL; /* Pedantic (lint does not know about erl_exit) */ + error: + BIF_ERROR(BIF_P, BADARG); } +/**********************************************************************/ + BIF_RETTYPE function_exported_3(BIF_ALIST_3) { if (is_not_atom(BIF_ARG_1) || diff --git a/erts/emulator/beam/bif.tab b/erts/emulator/beam/bif.tab index 7f874a2d9c..1338b90374 100644 --- a/erts/emulator/beam/bif.tab +++ b/erts/emulator/beam/bif.tab @@ -1,7 +1,7 @@ # # %CopyrightBegin% # -# Copyright Ericsson AB 1996-2011. All Rights Reserved. +# Copyright Ericsson AB 1996-2012. All Rights Reserved. # # The contents of this file are subject to the Erlang Public License, # Version 1.1, (the "License"); you may not use this file except in @@ -115,6 +115,8 @@ bif erlang:halt/0 bif 'erl.lang.system':halt/0 ebif_halt_0 bif erlang:halt/1 bif 'erl.lang.system':halt/1 ebif_halt_1 +bif erlang:halt/2 +bif 'erl.lang.system':halt/2 ebif_halt_2 bif erlang:phash/2 bif erlang:phash2/1 bif erlang:phash2/2 diff --git a/erts/emulator/beam/erl_init.c b/erts/emulator/beam/erl_init.c index 4db23a0a18..f56ccac33d 100644 --- a/erts/emulator/beam/erl_init.c +++ b/erts/emulator/beam/erl_init.c @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 1997-2011. All Rights Reserved. + * Copyright Ericsson AB 1997-2012. All Rights Reserved. * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in @@ -1514,7 +1514,7 @@ __decl_noreturn void erts_thr_fatal_error(int err, char *what) #endif static void -system_cleanup(int exit_code) +system_cleanup(int flush_async) { /* * Make sure only one thread exits the runtime system. @@ -1546,7 +1546,7 @@ system_cleanup(int exit_code) * (in threaded non smp case). */ - if (exit_code != 0 + if (!flush_async || !erts_initialized #if defined(USE_THREADS) && !defined(ERTS_SMP) || !erts_equal_tids(main_thread, erts_thr_self()) @@ -1589,21 +1589,12 @@ system_cleanup(int exit_code) erts_exit_flush_async(); } -/* - * Common exit function, all exits from the system go through here. - * n <= 0 -> normal exit with status n; - * n = 127 -> Erlang crash dump produced, exit with status 1; - * other positive n -> Erlang crash dump and core dump produced. - */ - -__decl_noreturn void erl_exit0(char *file, int line, int n, char *fmt,...) +static __decl_noreturn void __noreturn +erl_exit_vv(int n, int flush_async, char *fmt, va_list args1, va_list args2) { unsigned int an; - va_list args; - va_start(args, fmt); - - system_cleanup(n); + system_cleanup(flush_async); save_statistics(); @@ -1613,66 +1604,42 @@ __decl_noreturn void erl_exit0(char *file, int line, int n, char *fmt,...) erts_mtrace_exit((Uint32) an); /* Produce an Erlang core dump if error */ - if (n > 0 && erts_initialized && - (erts_no_crash_dump == 0 || n == ERTS_DUMP_EXIT)) { - erl_crash_dump_v(file, line, fmt, args); + if (((n > 0 && erts_no_crash_dump == 0) || n == ERTS_DUMP_EXIT) + && erts_initialized) { + erl_crash_dump_v((char*) NULL, 0, fmt, args1); } - /* need to reinitialize va_args thing */ - va_end(args); - va_start(args, fmt); - if (fmt != NULL && *fmt != '\0') - erl_error(fmt, args); /* Print error message. */ - va_end(args); + erl_error(fmt, args2); /* Print error message. */ sys_tty_reset(n); if (n == ERTS_INTR_EXIT) exit(0); - else if (n == 127) + else if (n == ERTS_DUMP_EXIT) ERTS_EXIT_AFTER_DUMP(1); else if (n > 0 || n == ERTS_ABORT_EXIT) abort(); exit(an); } -__decl_noreturn void erl_exit(int n, char *fmt,...) +/* Exit without flushing async threads */ +__decl_noreturn void __noreturn erl_exit(int n, char *fmt, ...) { - unsigned int an; - va_list args; - - va_start(args, fmt); - - system_cleanup(n); - - save_statistics(); - - an = abs(n); - - if (erts_mtrace_enabled) - erts_mtrace_exit((Uint32) an); - - /* Produce an Erlang core dump if error */ - if (n > 0 && erts_initialized && - (erts_no_crash_dump == 0 || n == ERTS_DUMP_EXIT)) { - erl_crash_dump_v((char*) NULL, 0, fmt, args); - } - - /* need to reinitialize va_args thing */ - va_end(args); - va_start(args, fmt); - - if (fmt != NULL && *fmt != '\0') - erl_error(fmt, args); /* Print error message. */ - va_end(args); - sys_tty_reset(n); - - if (n == ERTS_INTR_EXIT) - exit(0); - else if (n == ERTS_DUMP_EXIT) - ERTS_EXIT_AFTER_DUMP(1); - else if (n > 0 || n == ERTS_ABORT_EXIT) - abort(); - exit(an); + va_list args1, args2; + va_start(args1, fmt); + va_start(args2, fmt); + erl_exit_vv(n, 0, fmt, args1, args2); + va_end(args2); + va_end(args1); } +/* Exit after flushing async threads */ +__decl_noreturn void __noreturn erl_exit_flush_async(int n, char *fmt, ...) +{ + va_list args1, args2; + va_start(args1, fmt); + va_start(args2, fmt); + erl_exit_vv(n, 1, fmt, args1, args2); + va_end(args2); + va_end(args1); +} diff --git a/erts/emulator/beam/erl_process.c b/erts/emulator/beam/erl_process.c index 6c136571d5..ec85dd487a 100644 --- a/erts/emulator/beam/erl_process.c +++ b/erts/emulator/beam/erl_process.c @@ -104,6 +104,9 @@ do { \ #define ERTS_EMPTY_RUNQ(RQ) \ ((RQ)->len == 0 && (RQ)->misc.start == NULL) +#define ERTS_EMPTY_RUNQ_PORTS(RQ) \ + ((RQ)->ports.info.len == 0 && (RQ)->misc.start == NULL) + extern BeamInstr beam_apply[]; extern BeamInstr beam_exit[]; extern BeamInstr beam_continue_exit[]; @@ -369,6 +372,9 @@ dbg_chk_aux_work_val(erts_aint32_t value) #ifdef ERTS_SMP valid |= ERTS_SSI_AUX_WORK_CODE_IX_ACTIVATION; #endif +#ifdef ERTS_SSI_AUX_WORK_REAP_PORTS + valid |= ERTS_SSI_AUX_WORK_REAP_PORTS; +#endif if (~valid & value) erl_exit(ERTS_ABORT_EXIT, @@ -864,8 +870,6 @@ set_aux_work_flags_wakeup_nob(ErtsSchedulerSleepInfo *ssi, } } -#ifdef ERTS_SMP - static ERTS_INLINE void set_aux_work_flags_wakeup_relb(ErtsSchedulerSleepInfo *ssi, erts_aint32_t flgs) @@ -885,8 +889,6 @@ set_aux_work_flags_wakeup_relb(ErtsSchedulerSleepInfo *ssi, } } -#endif /* ERTS_SMP */ - static ERTS_INLINE erts_aint32_t set_aux_work_flags(ErtsSchedulerSleepInfo *ssi, erts_aint32_t flgs) { @@ -1392,6 +1394,65 @@ handle_check_children(ErtsAuxWorkData *awdp, erts_aint32_t aux_work) #endif +static void +notify_reap_ports_relb(void) +{ + int i; + for (i = 0; i < erts_no_schedulers; i++) { + set_aux_work_flags_wakeup_relb(ERTS_SCHED_SLEEP_INFO_IX(i), + ERTS_SSI_AUX_WORK_REAP_PORTS); + } +} + +erts_smp_atomic32_t erts_halt_progress; +int erts_halt_code; + +static ERTS_INLINE erts_aint32_t +handle_reap_ports(ErtsAuxWorkData *awdp, erts_aint32_t aux_work) +{ + unset_aux_work_flags(awdp->ssi, ERTS_SSI_AUX_WORK_REAP_PORTS); + awdp->esdp->run_queue->halt_in_progress = 1; + if (erts_smp_atomic32_dec_read_acqb(&erts_halt_progress) == 0) { + int i; + erts_smp_atomic32_set_nob(&erts_halt_progress, 1); + for (i = 0; i < erts_max_ports; i++) { + Port *prt = &erts_port[i]; + erts_smp_port_state_lock(prt); + if ((prt->status & (ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP + | ERTS_PORT_SFLG_HALT))) { + erts_smp_port_state_unlock(prt); + continue; + } + /* We need to set the halt flag - get the port lock */ +#ifdef ERTS_SMP + erts_smp_atomic_inc_nob(&prt->refc); +#endif + erts_smp_port_state_unlock(prt); +#ifdef ERTS_SMP + erts_smp_mtx_lock(prt->lock); +#endif + if ((prt->status & (ERTS_PORT_SFLGS_INVALID_DRIVER_LOOKUP + | ERTS_PORT_SFLG_HALT))) { + erts_port_release(prt); + continue; + } + erts_port_status_bor_set(prt, ERTS_PORT_SFLG_HALT); + erts_smp_atomic32_inc_nob(&erts_halt_progress); + if (prt->status & (ERTS_PORT_SFLG_EXITING + | ERTS_PORT_SFLG_CLOSING)) { + erts_port_release(prt); + continue; + } + erts_do_exit_port(prt, prt->id, am_killed); + erts_port_release(prt); + } + if (erts_smp_atomic32_dec_read_nob(&erts_halt_progress) == 0) { + erl_exit_flush_async(erts_halt_code, ""); + } + } + return aux_work & ~ERTS_SSI_AUX_WORK_REAP_PORTS; +} + #if HAVE_ERTS_MSEG static ERTS_INLINE erts_aint32_t @@ -1496,6 +1557,10 @@ handle_aux_work(ErtsAuxWorkData *awdp, erts_aint32_t orig_aux_work) HANDLE_AUX_WORK(ERTS_SSI_AUX_WORK_CODE_IX_ACTIVATION, handle_code_ix_activation); #endif + + HANDLE_AUX_WORK(ERTS_SSI_AUX_WORK_REAP_PORTS, + handle_reap_ports); + ERTS_DBG_CHK_AUX_WORK_VAL(aux_work); return aux_work; @@ -2761,6 +2826,9 @@ try_steal_task_from_victim(ErtsRunQueue *rq, int *rq_lockedp, ErtsRunQueue *vrq) ERTS_SMP_LC_CHK_RUNQ_LOCK(rq, *rq_lockedp); ERTS_SMP_LC_CHK_RUNQ_LOCK(vrq, vrq_locked); + if (rq->halt_in_progress) + goto try_steal_port; + /* * Check for a runnable process to steal... */ @@ -2847,6 +2915,8 @@ try_steal_task_from_victim(ErtsRunQueue *rq, int *rq_lockedp, ErtsRunQueue *vrq) vrq_locked = 1; } + try_steal_port: + ERTS_SMP_LC_CHK_RUNQ_LOCK(rq, *rq_lockedp); ERTS_SMP_LC_CHK_RUNQ_LOCK(vrq, vrq_locked); @@ -2962,7 +3032,8 @@ try_steal_task(ErtsRunQueue *rq) erts_smp_runq_lock(rq); if (!res) - res = !ERTS_EMPTY_RUNQ(rq); + res = rq->halt_in_progress ? + !ERTS_EMPTY_RUNQ_PORTS(rq) : !ERTS_EMPTY_RUNQ(rq); return res; } @@ -3628,6 +3699,7 @@ erts_init_scheduling(int no_schedulers, int no_schedulers_online) rq->len = 0; rq->wakeup_other = 0; rq->wakeup_other_reds = 0; + rq->halt_in_progress = 0; rq->procs.len = 0; rq->procs.pending_exiters = NULL; @@ -3822,6 +3894,9 @@ erts_init_scheduling(int no_schedulers, int no_schedulers_online) ERTS_VERIFY_UNUSED_TEMP_ALLOC(NULL); #endif #endif + + erts_smp_atomic32_init_relb(&erts_halt_progress, -1); + erts_halt_code = 0; } ErtsRunQueue * @@ -6388,7 +6463,9 @@ Process *schedule(Process *p, int calls) ASSERT(rq->len == rq->procs.len + rq->ports.info.len); - if (rq->len == 0 && !rq->misc.start) { + if ((rq->len == 0 && !rq->misc.start) + || (rq->halt_in_progress + && rq->ports.info.len == 0 && !rq->misc.start)) { #ifdef ERTS_SMP @@ -6486,7 +6563,8 @@ Process *schedule(Process *p, int calls) if (rq->ports.info.len) { int have_outstanding_io; have_outstanding_io = erts_port_task_execute(rq, &esdp->current_port); - if (have_outstanding_io && fcalls > 2*input_reductions) { + if ((have_outstanding_io && fcalls > 2*input_reductions) + || rq->halt_in_progress) { /* * If we have performed more than 2*INPUT_REDUCTIONS since * last call to erl_sys_schedule() and we still haven't @@ -9907,3 +9985,30 @@ debug_processes_assert_error(char* expr, char* file, int line) /* *\ * End of the processes/0 BIF implementation. * \* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +/* + * A nice system halt closing all open port goes as follows: + * 1) This function schedules the aux work ERTS_SSI_AUX_WORK_REAP_PORTS + * on all schedulers, then schedules itself out. + * 2) All shedulers detect this and set the flag halt_in_progress + * on their run queue. The last scheduler sets all non-closed ports + * ERTS_PORT_SFLG_HALT. Global atomic erts_halt_progress is used + * as refcount to determine which is last. + * 3) While the run ques has flag halt_in_progress no processes + * will be scheduled, only ports. + * 4) When the last port closes that scheduler calls erlang:halt/1. + * The same global atomic is used as refcount. + * + * A BIF that calls this should make sure to schedule out to never come back: + * erl_halt((int)(- code)); + * ERTS_BIF_YIELD1(bif_export[BIF_erlang_halt_1], BIF_P, NIL); + */ +void erl_halt(int code) +{ + if (-1 == erts_smp_atomic32_cmpxchg_acqb(&erts_halt_progress, + erts_no_schedulers, + -1)) { + erts_halt_code = code; + notify_reap_ports_relb(); + } +} diff --git a/erts/emulator/beam/erl_process.h b/erts/emulator/beam/erl_process.h index 3f19d92fcd..e20cb493f0 100644 --- a/erts/emulator/beam/erl_process.h +++ b/erts/emulator/beam/erl_process.h @@ -265,6 +265,7 @@ typedef enum { #define ERTS_SSI_AUX_WORK_SET_TMO (((erts_aint32_t) 1) << 9) #define ERTS_SSI_AUX_WORK_MSEG_CACHE_CHECK (((erts_aint32_t) 1) << 10) #define ERTS_SSI_AUX_WORK_CODE_IX_ACTIVATION (((erts_aint32_t) 1) << 11) +#define ERTS_SSI_AUX_WORK_REAP_PORTS (((erts_aint32_t) 1) << 12) typedef struct ErtsSchedulerSleepInfo_ ErtsSchedulerSleepInfo; @@ -342,6 +343,7 @@ struct ErtsRunQueue_ { int len; int wakeup_other; int wakeup_other_reds; + int halt_in_progress; struct { int len; @@ -1669,4 +1671,6 @@ erts_sched_poke(ErtsSchedulerSleepInfo *ssi) #endif - +void erl_halt(int code); +extern erts_smp_atomic32_t erts_halt_progress; +extern int erts_halt_code; diff --git a/erts/emulator/beam/global.h b/erts/emulator/beam/global.h index 1431e08610..b259ef3694 100644 --- a/erts/emulator/beam/global.h +++ b/erts/emulator/beam/global.h @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 1996-2011. All Rights Reserved. + * Copyright Ericsson AB 1996-2012. All Rights Reserved. * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in @@ -808,6 +808,8 @@ do { \ /* Port uses port specific locking (opposed to driver specific locking) */ #define ERTS_PORT_SFLG_PORT_SPECIFIC_LOCK ((Uint32) (1 << 13)) #define ERTS_PORT_SFLG_INVALID ((Uint32) (1 << 14)) +/* Last port to terminate halts the emulator */ +#define ERTS_PORT_SFLG_HALT ((Uint32) (1 << 15)) #ifdef DEBUG /* Only debug: make sure all flags aren't cleared unintentionally */ #define ERTS_PORT_SFLG_PORT_DEBUG ((Uint32) (1 << 31)) @@ -912,14 +914,9 @@ void loaded(int, void *); /* config.c */ __decl_noreturn void __noreturn erl_exit(int n, char*, ...); -__decl_noreturn void __noreturn erl_exit0(char *, int, int n, char*, ...); +__decl_noreturn void __noreturn erl_exit_flush_async(int n, char*, ...); void erl_error(char*, va_list); -#define ERL_EXIT0(n,f) erl_exit0(__FILE__, __LINE__, n, f) -#define ERL_EXIT1(n,f,a) erl_exit0(__FILE__, __LINE__, n, f, a) -#define ERL_EXIT2(n,f,a,b) erl_exit0(__FILE__, __LINE__, n, f, a, b) -#define ERL_EXIT3(n,f,a,b,c) erl_exit0(__FILE__, __LINE__, n, f, a, b, c) - /* copy.c */ void init_copy(void); Eterm copy_object(Eterm, Process*); diff --git a/erts/emulator/beam/io.c b/erts/emulator/beam/io.c index 603a6c47aa..7604365ab6 100644 --- a/erts/emulator/beam/io.c +++ b/erts/emulator/beam/io.c @@ -1838,6 +1838,7 @@ terminate_port(Port *prt) Eterm send_closed_port_id; Eterm connected_id = NIL /* Initialize to silence compiler */; erts_driver_t *drv; + int halt; ERTS_SMP_CHK_NO_PROC_LOCKS; ERTS_SMP_LC_ASSERT(erts_lc_is_port_locked(prt)); @@ -1845,6 +1846,8 @@ terminate_port(Port *prt) ASSERT(!prt->nlinks); ASSERT(!prt->monitors); + /* prt->status may be altered by kill_port()below */ + halt = (prt->status & ERTS_PORT_SFLG_HALT) != 0; if (prt->status & ERTS_PORT_SFLG_SEND_CLOSED) { erts_port_status_band_set(prt, ~ERTS_PORT_SFLG_SEND_CLOSED); send_closed_port_id = prt->id; @@ -1896,6 +1899,10 @@ terminate_port(Port *prt) * We don't want to send the closed message until after the * port has been removed from the port table (in kill_port()). */ + if (halt && (erts_smp_atomic32_dec_read_nob(&erts_halt_progress) == 0)) { + erts_smp_port_unlock(prt); /* We will exit and never return */ + erl_exit_flush_async(erts_halt_code, ""); + } if (is_internal_port(send_closed_port_id)) deliver_result(send_closed_port_id, connected_id, am_closed); diff --git a/erts/emulator/beam/sys.h b/erts/emulator/beam/sys.h index eb6f2f8516..7b2bb81f62 100644 --- a/erts/emulator/beam/sys.h +++ b/erts/emulator/beam/sys.h @@ -1,7 +1,7 @@ /* * %CopyrightBegin% * - * Copyright Ericsson AB 1996-2011. All Rights Reserved. + * Copyright Ericsson AB 1996-2012. All Rights Reserved. * * The contents of this file are subject to the Erlang Public License, * Version 1.1, (the "License"); you may not use this file except in @@ -511,7 +511,7 @@ __decl_noreturn void __noreturn erl_exit(int n, char*, ...); /* Some special erl_exit() codes: */ #define ERTS_INTR_EXIT INT_MIN /* called from signal handler */ #define ERTS_ABORT_EXIT (INT_MIN + 1) /* no crash dump; only abort() */ -#define ERTS_DUMP_EXIT (127) /* crash dump; then exit() */ +#define ERTS_DUMP_EXIT (INT_MIN + 2) /* crash dump; then exit() */ Eterm erts_check_io_info(void *p); diff --git a/erts/emulator/test/bif_SUITE.erl b/erts/emulator/test/bif_SUITE.erl index 90c89cc12b..ebbd8b31c6 100644 --- a/erts/emulator/test/bif_SUITE.erl +++ b/erts/emulator/test/bif_SUITE.erl @@ -1,7 +1,7 @@ %% %% %CopyrightBegin% %% -%% Copyright Ericsson AB 2005-2011. All Rights Reserved. +%% Copyright Ericsson AB 2005-2012. All Rights Reserved. %% %% The contents of this file are subject to the Erlang Public License, %% Version 1.1, (the "License"); you may not use this file except in @@ -30,7 +30,7 @@ specs/1,improper_bif_stubs/1,auto_imports/1, t_list_to_existing_atom/1,os_env/1,otp_7526/1, binary_to_atom/1,binary_to_existing_atom/1, - atom_to_binary/1,min_max/1]). + atom_to_binary/1,min_max/1, erlang_halt/1]). suite() -> [{ct_hooks,[ts_install_cth]}]. @@ -40,7 +40,7 @@ all() -> t_list_to_existing_atom, os_env, otp_7526, display, atom_to_binary, binary_to_atom, binary_to_existing_atom, - min_max]. + min_max, erlang_halt]. groups() -> []. @@ -640,6 +640,47 @@ min_max(Config) when is_list(Config) -> ok. + + +erlang_halt(Config) when is_list(Config) -> + try erlang:halt(undefined) of + _-> ?t:fail({erlang,halt,{undefined}}) + catch error:badarg -> ok end, + try halt(undefined) of + _-> ?t:fail({halt,{undefined}}) + catch error:badarg -> ok end, + try erlang:halt(undefined, []) of + _-> ?t:fail({erlang,halt,{undefined,[]}}) + catch error:badarg -> ok end, + try halt(undefined, []) of + _-> ?t:fail({halt,{undefined,[]}}) + catch error:badarg -> ok end, + try halt(0, undefined) of + _-> ?t:fail({halt,{0,undefined}}) + catch error:badarg -> ok end, + try halt(0, [undefined]) of + _-> ?t:fail({halt,{0,[undefined]}}) + catch error:badarg -> ok end, + try halt(0, [{undefined,true}]) of + _-> ?t:fail({halt,{0,[{undefined,true}]}}) + catch error:badarg -> ok end, + try halt(0, [{flush,undefined}]) of + _-> ?t:fail({halt,{0,[{flush,undefined}]}}) + catch error:badarg -> ok end, + try halt(0, [{flush,true,undefined}]) of + _-> ?t:fail({halt,{0,[{flush,true,undefined}]}}) + catch error:badarg -> ok end, + H = hostname(), + {ok,N1} = slave:start(H, halt_node1), + {badrpc,nodedown} = rpc:call(N1, erlang, halt, []), + {ok,N2} = slave:start(H, halt_node2), + {badrpc,nodedown} = rpc:call(N2, erlang, halt, [0]), + {ok,N3} = slave:start(H, halt_node3), + {badrpc,nodedown} = rpc:call(N3, erlang, halt, [0,[]]), + ok. + + + %% Helpers id(I) -> I. @@ -671,3 +712,12 @@ extract_abstract(Mod, Path) -> {ok,{Mod,[{abstract_code,{raw_abstract_v1,Abstr}}]}} = beam_lib:chunks(Beam, [abstract_code]), {Mod,Abstr}. + + +hostname() -> + hostname(atom_to_list(node())). + +hostname([$@ | Hostname]) -> + list_to_atom(Hostname); +hostname([_C | Cs]) -> + hostname(Cs). diff --git a/erts/ntbuild.erl b/erts/ntbuild.erl deleted file mode 100644 index e48be58c17..0000000000 --- a/erts/ntbuild.erl +++ /dev/null @@ -1,332 +0,0 @@ -%% -%% %CopyrightBegin% -%% -%% Copyright Ericsson AB 1997-2009. All Rights Reserved. -%% -%% The contents of this file are subject to the Erlang Public License, -%% Version 1.1, (the "License"); you may not use this file except in -%% compliance with the License. You should have received a copy of the -%% Erlang Public License along with this software. If not, it can be -%% retrieved online at http://www.erlang.org/. -%% -%% Software distributed under the License is distributed on an "AS IS" -%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See -%% the License for the specific language governing rights and limitations -%% under the License. -%% -%% %CopyrightEnd% -%% -%% To be used from makefiles on the unix side executing things on the NT-side --module(ntbuild). - --export([nmake/1, omake/1, waitnode/1, restart/1, - setdir/1, run_tests/1, run_command/1]). --export([serv_nmake/2, serv_omake/2, serv_restart/0, serv_run_tests/2, - serv_run_command/1]). - -waitnode([NtNode]) -> - % First, wait for node to disappear. - case wait_disappear(NtNode, 0) of - ok -> - case wait_appear(NtNode, 0) of - ok -> - halt(0); - fail -> - halt(1) - end; - fail -> - halt(1) - end. - -% Wait for nt node to appear within 5 minutes. -wait_appear(_NtNode, 300) -> - fail; -wait_appear(NtNode, N) -> - receive after 1000 -> ok end, - case nt_node_alive(NtNode, quiet) of - no -> - wait_appear(NtNode, N+1); - yes -> - ok - end. - - - -% Waits for nt node to disappear within 3 minutes. -wait_disappear(NtNode, 300) -> - fail; -wait_disappear(NtNode, N) -> - receive after 1000 -> ok end, - case nt_node_alive(NtNode, quiet) of - yes -> - wait_disappear(NtNode, N+1); - no -> - ok - end. - -restart([NtNode]) -> - case nt_node_alive(NtNode) of - yes -> - case rpc:call(NtNode, ntbuild, serv_restart, []) of - ok -> - io:format("halt(0)~n"), - halt(); - Error -> - io:format("halt(1)~n"), - halt(1) - end; - no -> - halt(1) - end. - - -setdir([NtNode, Dir0]) -> - Dir = atom_to_list(Dir0), - case nt_node_alive(NtNode) of - yes -> - case rpc:call(NtNode, file, set_cwd, [Dir]) of - ok -> - io:format("halt(0)~n"), - halt(); - Error -> - io:format("halt(1) (Error: ~p) (~p not found) ~n", [Error, Dir]), - halt(1) - end; - no -> - halt(1) - end. - -run_tests([NtNode, Vsn0, Logdir]) -> - Vsn = atom_to_list(Vsn0), - case nt_node_alive(NtNode) of - yes -> - case rpc:call(NtNode, ntbuild, serv_run_tests, [Vsn, Logdir]) of - ok -> - io:format("halt(0)~n"), - halt(); - Error -> - io:format("RPC To Windows Node Failed: ~p~n", [Error]), - io:format("halt(1)~n"), - halt(1) - end; - no -> - halt(1) - end. - -run_command([NtNode, Cmd]) -> - case nt_node_alive(NtNode) of - yes -> - case rpc:call(NtNode, ntbuild, serv_run_command, [Cmd]) of - ok -> - io:format("halt(0)~n"), - halt(); - Error -> - io:format("RPC To Windows Node Failed: ~p~n", [Error]), - io:format("halt(1)~n"), - halt(1) - end; - no -> - halt(1) - end. - -nmake([NtNode, Path, Options]) -> -% io:format("nmake2(~w,~w)~n",[Path, Options]), - Dir=atom_to_list(Path), - Opt=atom_to_list(Options), - case nt_node_alive(NtNode) of - yes -> - case rpc:call(NtNode, ntbuild, serv_nmake, [Dir, Opt]) of - ok -> - io:format("halt(0)~n"), - halt(); - Error -> - io:format("Error: ~n", [Error]), - halt(1) - end; - no -> - halt(1) - end. - -omake([NtNode, Path, Options]) -> - Dir=atom_to_list(Path), - Opt=atom_to_list(Options), - case nt_node_alive(NtNode) of - yes -> - case rpc:call(NtNode, ntbuild, serv_omake, [Dir, Opt]) of - ok -> - io:format("halt(0)~n"), - halt(); - Error -> - io:format("RPC To Windows Node Failed: ~p~n", [Error]), - io:format("~p ~p~n", [Dir, Opt]), - io:format("halt(1)~n"), - halt(1) - end; - no -> - halt(1) - end. - - - - - -nt_node_alive(NtNode) -> - case net:ping(NtNode) of - pong -> - yes; - pang -> - io:format("The NT node (~p) is not up. ~n",[NtNode]), - no - end. - -nt_node_alive(NtNode, quiet) -> - case net:ping(NtNode) of - pong -> - yes; - pang -> - no - end. - - - -%%% -%%% The 'serv_' functions. Theese are the routines run on the WinNT node. -%%% - -%%----------------------- -%% serv_run_tests() -%% Runs the tests. -serv_run_tests(Vsn, Logdir) -> - {ok, Cwd}=file:get_cwd(), - io:format("serv_run_tests ~p ~p ~n", [Vsn, Logdir]), - Cmd0= "set central_log_dir=" ++ Logdir, - Erl = "C:/progra~1/erl"++Vsn++"/bin/erl", - Cmd1 = Erl++" -sname a -setcookie a -noshell -noinput -s ts install -s ts run -s ts save -s erlang halt", -%% Dir = "C:/temp/test_suite/test_server", - Cmd= Cmd0 ++ "/r/n" ++ Cmd1, - Dir = "C:/temp/test_server/p7a/test_server", - file:set_cwd(Dir), - Res=run_make_bat(Dir, Cmd), - file:set_cwd(Cwd), - Res. - -%%----------------------- -%% serv_run_command() -%% Runs a command. -serv_run_command(Cmd) -> - {ok, Cwd}=file:get_cwd(), - Res=run_make_bat("", Cmd), - file:set_cwd(Cwd), - Res. - -%%----------------------- -%% serv_restart() -%% Reboots the NT machine. -serv_restart() -> - Exe="\\erts\\install_nt\\reboot.exe", - open_port({spawn, Exe}, [stream, eof, in]), - ok. - - -%%----------------------- -%% serv_nmake(Path, Options) -%% Runs `nmake' in the given directory. -%% Result: ok | error -serv_nmake(Path, Options) -> - {ok, Cwd}=file:get_cwd(), - Command="nmake -e -f Makefile.win32 " ++ Options ++ " 2>&1", - Res=run_make_bat(Path, Command), - file:set_cwd(Cwd), - Res. - -%%----------------------- -%% serv_omake(Path, Options) -%% Runs `omake' in the given directory. -%% Result: ok | error -serv_omake(Path, Options) -> - {ok, Cwd}=file:get_cwd(), - Command="omake -W -E -EN -f Makefile.win32 " ++ Options ++ " 2>&1", - Res=run_make_bat(Path, Command), - file:set_cwd(Cwd), - Res. - - -read_output(Port, SoFar) -> -% io:format("(read_output)~n"), - case get_data_from_port(Port) of - eof -> - io:format("*** eof ***~n"), - io:format("Never reached a real message"), - halt(1); - {ok, Data} -> - case print_line([SoFar|Data]) of - {ok, Rest} -> - read_output(Port, Rest); - {done, Res} -> - Res - end - end. - -print_line(Data) -> - print_line(Data, []). - -print_line([], Acc) -> - {ok, lists:reverse(Acc)}; -print_line([$*,$o,$k,$*|Rest], _Acc) -> - io:format("*ok*~n"), - {done, ok}; -print_line([$*,$e,$r,$r,$o,$r|Rest], _Acc) -> - io:format("*error*~n"), - {done, error}; -print_line([$\r,$\n|Rest], Acc) -> - io:format("~s~n", [lists:reverse(Acc)]), - print_line(Rest, []); -print_line([Chr|Rest], Acc) -> - print_line(Rest, [Chr|Acc]). - -get_data_from_port(Port) -> - receive - {Port, {data, Bytes}} -> - {ok, Bytes}; - {Port, eof} -> - unlink(Port), - exit(Port, die), - eof; - Other -> - io:format("Strange message received: ~p~n", [Other]), - get_data_from_port(Port) - end. - - -run_make_bat(Dir, Make) -> - {Name, Exe, Script}=create_make_script(Dir, Make), - io:format("Exe:~p Cwd:~p Script:~p ~n",[Exe, Dir, Script]), - case file:write_file(Name, Script) of - ok -> - case catch open_port({spawn, Exe}, [stderr_to_stdout, stream, hide, - eof, in]) of - Port when port(Port) -> - read_output(Port, []); - Other -> - io:format("Error, open_port failed: ~p~n", [Other]), - {open_port, Other, Exe} - end; - Error -> - {write_file, Error, Name} - end. - -create_make_script(Dir, Make) when atom(Make) -> - create_make_script(Dir, atom_to_list(Make)); -create_make_script(Dir, Make) -> - {"run_make_bs.bat", - "run_make_bs 2>&1", - ["@echo off\r\n", - "@cd ", Dir, "\r\n", - Make++"\r\n", - "if errorlevel 1 echo *run_make_bs error*\r\n", - "if not errorlevel 1 echo *ok*\r\n"]}. - - - - - diff --git a/erts/preloaded/ebin/erlang.beam b/erts/preloaded/ebin/erlang.beam Binary files differindex 1dce82f611..9b65302b6e 100644 --- a/erts/preloaded/ebin/erlang.beam +++ b/erts/preloaded/ebin/erlang.beam diff --git a/erts/preloaded/src/erlang.erl b/erts/preloaded/src/erlang.erl index 5e451c015b..274764ac74 100644 --- a/erts/preloaded/src/erlang.erl +++ b/erts/preloaded/src/erlang.erl @@ -87,7 +87,7 @@ -export([garbage_collect/0, garbage_collect/1]). -export([garbage_collect_message_area/0, get/0, get/1, get_keys/1]). -export([get_module_info/1, get_stacktrace/0, group_leader/0]). --export([group_leader/2, halt/0, halt/1, hash/2, hibernate/3]). +-export([group_leader/2, halt/0, halt/1, halt/2, hash/2, hibernate/3]). -export([integer_to_list/1, iolist_size/1, iolist_to_binary/1]). -export([is_alive/0, is_builtin/3, is_process_alive/1, length/1, link/1]). -export([list_to_atom/1, list_to_binary/1, list_to_bitstr/1]). @@ -748,10 +748,19 @@ halt() -> %% halt/1 %% Shadowed by erl_bif_types: erlang:halt/1 -spec halt(Status) -> no_return() when - Status :: non_neg_integer() | string(). + Status :: non_neg_integer() | 'abort' | string(). halt(_Status) -> erlang:nif_error(undefined). +%% halt/2 +%% Shadowed by erl_bif_types: erlang:halt/2 +-spec halt(Status, Options) -> no_return() when + Status :: non_neg_integer() | 'abort' | string(), + Options :: [Option], + Option :: {flush, boolean()}. +halt(_Status, _Options) -> + erlang:nif_error(undefined). + %% hash/2 -spec erlang:hash(Term, Range) -> pos_integer() when Term :: term(), |