Skip to content
Snippets Groups Projects
Select Git revision
  • 464bdff6c74950cb35139432ac574c11db86b7db
  • master default protected
2 results

string_utils.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    scheduled_executor.cpp 3.22 KiB
    /*
     *  Copyright (C) 2018-2019 Savoir-faire Linux Inc.
     *
     *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
     *
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 3 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
     */
    #include "scheduled_executor.h"
    #include "logger.h"
    
    namespace jami {
    
    ScheduledExecutor::ScheduledExecutor() : thread_([this]{
            while (running_.load())
                loop();
        })
    {}
    
    ScheduledExecutor::~ScheduledExecutor()
    {
        stop();
        if (thread_.joinable())
            thread_.join();
    }
    
    void
    ScheduledExecutor::stop()
    {
        {
            std::lock_guard<std::mutex> lock(jobLock_);
            running_ = false;
            jobs_.clear();
        }
        cv_.notify_all();
    }
    
    void
    ScheduledExecutor::run(Job&& job)
    {
        {
            std::lock_guard<std::mutex> lock(jobLock_);
            auto now = clock::now();
            jobs_[now].emplace_back(std::move(job));
        }
        cv_.notify_all();
    }
    
    std::shared_ptr<Task>
    ScheduledExecutor::schedule(Job&& job, time_point t)
    {
        auto ret = std::make_shared<Task>(std::move(job));
        schedule(ret, t);
        return ret;
    }
    
    std::shared_ptr<Task>
    ScheduledExecutor::scheduleIn(Job&& job, duration dt)
    {