Esempio n. 1
0
static void jl_shutdown_uv_cb(uv_shutdown_t* req, int status)
{
    //if (status == 0)
        jl_close_uv((uv_handle_t*)req->handle);
    free(req);
}
Esempio n. 2
0
DLLEXPORT void uv_atexit_hook()
{
#if defined(JL_GC_MARKSWEEP) && defined(GC_FINAL_STATS)
    jl_print_gc_stats(JL_STDERR);
#endif
    if (jl_base_module) {
        jl_value_t *f = jl_get_global(jl_base_module, jl_symbol("_atexit"));
        if (f!=NULL && jl_is_function(f)) {
            jl_apply((jl_function_t*)f, NULL, 0);
        }
    }
    uv_loop_t* loop = jl_global_event_loop();
    struct uv_shutdown_queue queue = {NULL, NULL};
    uv_walk(loop, jl_uv_exitcleanup_walk, &queue);
    // close stdout and stderr last, since we like being
    // able to show stuff (incl. printf's)
    jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stdout, &queue);
    jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stderr, &queue);
    struct uv_shutdown_queue_item *item = queue.first;
    while (item) {
        uv_handle_t *handle = item->h;
        if (uv_is_closing(handle)) {
            item = item->next;
            continue;
        }
        switch(handle->type) {
        case UV_TTY:
        case UV_UDP:
//#ifndef __WIN32__ // unix only supports shutdown on TCP and NAMED_PIPE
// but uv_shutdown doesn't seem to be particularly reliable, so we'll avoid it in general
            jl_close_uv(handle);
            break;
//#endif
        case UV_TCP:
        case UV_NAMED_PIPE:
            if (uv_is_writable((uv_stream_t*)handle)) { // uv_shutdown returns an error if not writable
                uv_shutdown_t *req = malloc(sizeof(uv_shutdown_t));
                int err = uv_shutdown(req, (uv_stream_t*)handle, jl_shutdown_uv_cb);
                if (err != 0) {
                    printf("shutdown err: %s\n", uv_strerror(uv_last_error(jl_global_event_loop())));
                    jl_close_uv(handle);
                }
            }
            else {
                jl_close_uv(handle);
            }
            break;
        case UV_POLL:
        case UV_TIMER:
        case UV_PREPARE:
        case UV_CHECK:
        case UV_IDLE:
        case UV_ASYNC:
        case UV_SIGNAL:
        case UV_PROCESS:
        case UV_FS_EVENT:
        case UV_FS_POLL:
            jl_close_uv(handle);
            break;
        case UV_HANDLE:
        case UV_STREAM:
        case UV_UNKNOWN_HANDLE:
        case UV_HANDLE_TYPE_MAX:
        case UV_RAW_FD:
        case UV_RAW_HANDLE:
        default:
            assert(0);
        }
        item = item->next;
    }
    uv_run(loop,UV_RUN_DEFAULT); //let libuv spin until everything has finished closing
}
Esempio n. 3
0
DLLEXPORT void uv_atexit_hook()
{
#if defined(JL_GC_MARKSWEEP) && defined(GC_FINAL_STATS)
    jl_print_gc_stats(JL_STDERR);
#endif
    if (jl_base_module) {
        jl_value_t *f = jl_get_global(jl_base_module, jl_symbol("_atexit"));
        if (f!=NULL && jl_is_function(f)) {
            jl_apply((jl_function_t*)f, NULL, 0);
        }
    }
    uv_loop_t* loop = jl_global_event_loop();
    struct uv_shutdown_queue queue = {NULL, NULL};
    uv_walk(loop, jl_uv_exitcleanup_walk, &queue);
    // close stdout and stderr last, since we like being
    // able to show stuff (incl. printf's)
    jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stdout, &queue);
    jl_uv_exitcleanup_add((uv_handle_t*)jl_uv_stderr, &queue);
    struct uv_shutdown_queue_item *item = queue.first;
    while (item) {
        uv_handle_t *handle = item->h;
        if (uv_is_closing(handle)) {
            item = item->next;
            continue;
        }
        switch(handle->type) {
        case UV_TTY:
        case UV_UDP:
//#ifndef _OS_WINDOWS_ // unix only supports shutdown on TCP and NAMED_PIPE
// but uv_shutdown doesn't seem to be particularly reliable, so we'll avoid it in general
            jl_close_uv(handle);
            break;
//#endif
        case UV_TCP:
        case UV_NAMED_PIPE:
            // These will be shut down in jl_close_uv.
            jl_close_uv(handle);
            break;
        //Don't close these directly, but rather let the GC take care of it
        case UV_POLL:
            uv_poll_stop((uv_poll_t*)handle);
            handle->data = NULL;
            uv_unref(handle);
            break;
        case UV_TIMER:
            uv_timer_stop((uv_timer_t*)handle);
            handle->data = NULL;
            uv_unref(handle);
            break;
        case UV_IDLE:
            uv_idle_stop((uv_idle_t*)handle);
        case UV_ASYNC:
            handle->data = NULL;
            uv_unref(handle);
            break;
        case UV_FS_EVENT:
            handle->data = NULL;
            uv_unref(handle);
            break;
        case UV_FS_POLL:
            uv_fs_poll_stop((uv_fs_poll_t*)handle);
            handle->data = NULL;
            uv_unref(handle);
            break;
        case UV_PREPARE:
        case UV_CHECK:
        case UV_SIGNAL:
        case UV_PROCESS:
            jl_close_uv(handle);
            break;
        case UV_HANDLE:
        case UV_STREAM:
        case UV_UNKNOWN_HANDLE:
        case UV_HANDLE_TYPE_MAX:
        case UV_RAW_FD:
        case UV_RAW_HANDLE:
        default:
            assert(0);
        }
        item = item->next;
    }
    uv_run(loop,UV_RUN_DEFAULT); //let libuv spin until everything has finished closing
}