<commit_before>#[doc="
A process-wide libuv event loop for library use.
"];

import ll = uv_ll;
import hl = uv_hl;
import get_gl = get;

export get, get_monitor_task_gl;

native mod rustrt {
    fn rust_uv_get_kernel_global_chan_ptr() -> *libc::uintptr_t;
}

#[doc ="
Race-free helper to get access to a global task where a libuv
loop is running.

Use `uv::hl::interact` to do operations against the global
loop that this function returns.

# Return

* A `hl::high_level_loop` that encapsulates communication with the global
loop.
"]
fn get() -> hl::high_level_loop {
    ret get_monitor_task_gl();
}

#[doc(hidden)]
fn get_monitor_task_gl() -> hl::high_level_loop {
    let monitor_loop_chan_ptr =
        rustrt::rust_uv_get_kernel_global_chan_ptr();
    log(debug, #fmt("ENTERING global_loop::get() loop chan: %?",
       monitor_loop_chan_ptr));
    let builder_fn = {||
        let builder = task::builder();
        let opts = {
            supervise: false,
            notify_chan: none,
            sched:
                some({mode: task::manual_threads(1u),
                      native_stack_size: none })
        };
        task::set_opts(builder, opts);
        builder
    };
    unsafe {
        log(debug, "before priv::chan_from_global_ptr");
        type hl_loop_req_ch = comm::chan<hl::high_level_loop>;
        let msg_ch = priv::chan_from_global_ptr::<hl_loop_req_ch>(
            monitor_loop_chan_ptr,
            builder_fn) {|msg_po|
            log(debug, "global monitor task starting");
            priv::weaken_task() {|weak_exit_po|
                log(debug, "global monitor task is now weak");
                let hl_loop_data = spawn_libuv_weak_task();
                let hl_loop = alt hl_loop_data {
                  (async, msg_ch) {
                    hl::high_level_loop({async_handle:async, op_chan:msg_ch})
                  }
                };
                loop {
                    log(debug, "in outer_loop...");
                    let continue = either::either(
                        {|weak_exit|
                            // all normal tasks have ended, tell the
                            // libuv loop to tear_down, then exit
                            log(debug, #fmt("weak_exit_po recv'd msg: %?",
                                           weak_exit));
                            let ( a, loop_msg_ch )= hl_loop_data;
                            comm::send(loop_msg_ch, hl::teardown_loop);
                            ll::async_send(a);
                            false
                        }, {|fetch_ch|
                            log(debug, #fmt("hl_loop req recv'd: %?",
                                           fetch_ch));
                            comm::send(fetch_ch, copy(hl_loop));
                            true
                        }, comm::select2(weak_exit_po, msg_po));
                    if !continue { break; }
                }
                log(debug, "global monitor task is leaving weakend state");
            };
            log(debug, "global monitor task exiting");
        };
        // once we have a chan to the monitor loop, we ask it for
        // the libuv loop's async handle
        let fetch_po = comm::port::<hl::high_level_loop>();
        let fetch_ch = comm::chan(fetch_po);
        comm::send(msg_ch, fetch_ch);
        comm::recv(fetch_po)
    }
}

unsafe fn spawn_libuv_weak_task() -> (*ll::uv_async_t,
                                      comm::chan<hl::high_level_msg>){
    let exit_po = comm::port::<(*ll::uv_async_t,
                              comm::chan<hl::high_level_msg>)>();
    let exit_ch = comm::chan(exit_po);

    task::spawn_sched(task::manual_threads(1u)) {||
        log(debug, "entering global libuv task");
        let loop_ptr = ll::loop_new();
        priv::weaken_task() {|weak_exit_po|
            log(debug, #fmt("global libuv task is now weak %?",
                            weak_exit_po));
            let loop_msg_po = comm::port::<hl::high_level_msg>();
            let loop_msg_ch = comm::chan(loop_msg_po);
            hl::run_high_level_loop(<commit_msg>std: Rename spawn_libuv_weak_task to spawn_high_level_loop<commit_after>#[doc="
A process-wide libuv event loop for library use.
"];

import ll = uv_ll;
import hl = uv_hl;
import get_gl = get;

export get, get_monitor_task_gl;

native mod rustrt {
    fn rust_uv_get_kernel_global_chan_ptr() -> *libc::uintptr_t;
}

#[doc ="
Race-free helper to get access to a global task where a libuv
loop is running.

Use `uv::hl::interact` to do operations against the global
loop that this function returns.

# Return

* A `hl::high_level_loop` that encapsulates communication with the global
loop.
"]
fn get() -> hl::high_level_loop {
    ret get_monitor_task_gl();
}

#[doc(hidden)]
fn get_monitor_task_gl() -> hl::high_level_loop {
    let monitor_loop_chan_ptr =
        rustrt::rust_uv_get_kernel_global_chan_ptr();
    log(debug, #fmt("ENTERING global_loop::get() loop chan: %?",
       monitor_loop_chan_ptr));
    let builder_fn = {||
        let builder = task::builder();
        let opts = {
            supervise: false,
            notify_chan: none,
            sched:
                some({mode: task::manual_threads(1u),
                      native_stack_size: none })
        };
        task::set_opts(builder, opts);
        builder
    };
    unsafe {
        log(debug, "before priv::chan_from_global_ptr");
        type hl_loop_req_ch = comm::chan<hl::high_level_loop>;
        let msg_ch = priv::chan_from_global_ptr::<hl_loop_req_ch>(
            monitor_loop_chan_ptr,
            builder_fn) {|msg_po|
            log(debug, "global monitor task starting");
            priv::weaken_task() {|weak_exit_po|
                log(debug, "global monitor task is now weak");
                let hl_loop_data = spawn_high_level_loop();
                let hl_loop = alt hl_loop_data {
                  (async, msg_ch) {
                    hl::high_level_loop({async_handle:async, op_chan:msg_ch})
                  }
                };
                loop {
                    log(debug, "in outer_loop...");
                    let continue = either::either(
                        {|weak_exit|
                            // all normal tasks have ended, tell the
                            // libuv loop to tear_down, then exit
                            log(debug, #fmt("weak_exit_po recv'd msg: %?",
                                           weak_exit));
                            let ( a, loop_msg_ch )= hl_loop_data;
                            comm::send(loop_msg_ch, hl::teardown_loop);
                            ll::async_send(a);
                            false
                        }, {|fetch_ch|
                            log(debug, #fmt("hl_loop req recv'd: %?",
                                           fetch_ch));
                            comm::send(fetch_ch, copy(hl_loop));
                            true
                        }, comm::select2(weak_exit_po, msg_po));
                    if !continue { break; }
                }
                log(debug, "global monitor task is leaving weakend state");
            };
            log(debug, "global monitor task exiting");
        };
        // once we have a chan to the monitor loop, we ask it for
        // the libuv loop's async handle
        let fetch_po = comm::port::<hl::high_level_loop>();
        let fetch_ch = comm::chan(fetch_po);
        comm::send(msg_ch, fetch_ch);
        comm::recv(fetch_po)
    }
}

unsafe fn spawn_high_level_loop() -> (*ll::uv_async_t,
                                      comm::chan<hl::high_level_msg>){
    let exit_po = comm::port::<(*ll::uv_async_t,
                              comm::chan<hl::high_level_msg>)>();
    let exit_ch = comm::chan(exit_po);

    task::spawn_sched(task::manual_threads(1u)) {||
        log(debug, "entering global libuv task");
        let loop_ptr = ll::loop_new();
        priv::weaken_task() {|weak_exit_po|
            log(debug, #fmt("global libuv task is now weak %?",
                            weak_exit_po));
            let loop_msg_po = comm::port::<hl::high_level_msg>();
            let loop_msg_ch = comm::chan(loop_msg_po);
            hl::run_high_level_loop(
{'fraction_non_alphanumeric': 0.11205125105765744, 'fraction_numerical': 0.0007252508159071679, 'mean_word_length': 1.717241379310345, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6411092', 'n_tokens_mistral': 2468, 'n_tokens_neox': 2398, 'n_words': 537}