Example #1
0
void Tox_Dispatcher::start()
{
    if (run.load()) {
        LOG(INFO) << "Tox dispatcher is already running";
    }

    LOG(INFO) << "Starting Tox dispatcher";
    try {
        init_tox();
        init_callbacks();
        bootstrap();
    } catch (const std::exception& e) {
        LOG(ERROR) << e.what();
        exit(-1);
    }
    dispatcher_started.Emit(this);

    LOG(INFO) << "Tox Id: " << get_self_id();

    /* run tox main loop */
    run.store(true);
    while (run.load()) {
        lock.lock();
            tox_iterate(tox);
            uint32_t millis = tox_iteration_interval(tox);
        lock.unlock();
        std::this_thread::sleep_for(std::chrono::milliseconds(millis));
    }

    tox_kill(tox);
    tox = nullptr;
    LOG(INFO) << "Tox dispatcher stopped";
}
Example #2
0
    thread_state_enum at_timer(SchedulingPolicy& scheduler,
        boost::chrono::steady_clock::time_point& abs_time,
        thread_id_type const& thrd, thread_state_enum newstate,
        thread_state_ex_enum newstate_ex, thread_priority priority)
    {
        if (HPX_UNLIKELY(!thrd)) {
            HPX_THROW_EXCEPTION(null_thread_id,
                "threads::detail::at_timer",
                "NULL thread id encountered");
            return terminated;
        }

        // create a new thread in suspended state, which will execute the
        // requested set_state when timer fires and will re-awaken this thread,
        // allowing the deadline_timer to go out of scope gracefully
        thread_id_type self_id = get_self_id();

        boost::shared_ptr<boost::atomic<bool> > triggered(
            boost::make_shared<boost::atomic<bool> >(false));

        thread_init_data data(
            boost::bind(&wake_timer_thread,
                thrd, newstate, newstate_ex, priority,
                self_id, triggered),
            "wake_timer", 0, priority);

        thread_id_type wake_id = invalid_thread_id;
        create_thread(&scheduler, data, wake_id, suspended);

        // create timer firing in correspondence with given time
        typedef boost::asio::basic_deadline_timer<
            boost::chrono::steady_clock
          , util::chrono_traits<boost::chrono::steady_clock>
        > deadline_timer;

        deadline_timer t (
            get_thread_pool("timer-pool")->get_io_service(), abs_time);

        // let the timer invoke the set_state on the new (suspended) thread
        t.async_wait(boost::bind(&detail::set_thread_state,
            wake_id, pending, wait_timeout, priority,
            std::size_t(-1), boost::ref(throws)));

        // this waits for the thread to be reactivated when the timer fired
        // if it returns signaled the timer has been canceled, otherwise
        // the timer fired and the wake_timer_thread above has been executed
        bool oldvalue = false;
        thread_state_ex_enum statex = get_self().yield(suspended);

        if (wait_timeout != statex &&
            triggered->compare_exchange_strong(oldvalue, true)) //-V601
        {
            // wake_timer_thread has not been executed yet, cancel timer
            t.cancel();
        }

        return terminated;
    }