changeset 600:af299c1729cf

Timer: use Boost.Asio instead
author David Demelier <markand@malikania.fr>
date Fri, 02 Dec 2016 22:21:03 +0100
parents 9016afda8527
children 1e23b6f0d605
files CMakeLists.txt modules/timer/CMakeLists.txt modules/timer/test/main.cpp modules/timer/timer.hpp
diffstat 4 files changed, 0 insertions(+), 274 deletions(-) [+]
line wrap: on
line diff
--- a/CMakeLists.txt	Fri Dec 02 22:20:36 2016 +0100
+++ b/CMakeLists.txt	Fri Dec 02 22:21:03 2016 +0100
@@ -52,7 +52,6 @@
 add_subdirectory(modules/js)
 add_subdirectory(modules/net)
 add_subdirectory(modules/signals)
-add_subdirectory(modules/timer)
 add_subdirectory(modules/unicode)
 add_subdirectory(modules/xdg)
 
--- a/modules/timer/CMakeLists.txt	Fri Dec 02 22:20:36 2016 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,22 +0,0 @@
-#
-# CMakeLists.txt -- code building for common code
-#
-# Copyright (c) 2016 David Demelier <markand@malikania.fr>
-#
-# Permission to use, copy, modify, and/or distribute this software for any
-# purpose with or without fee is hereby granted, provided that the above
-# copyright notice and this permission notice appear in all copies.
-#
-# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
-# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
-# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
-# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
-# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
-# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
-# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
-#
-
-code_define_module(
-    NAME timer
-    SOURCES timer.hpp
-)
--- a/modules/timer/test/main.cpp	Fri Dec 02 22:20:36 2016 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,66 +0,0 @@
-/*
- * main.cpp -- test irccd timer
- *
- * Copyright (c) 2016 David Demelier <markand@malikania.fr>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#include <gtest/gtest.h>
-
-#include "timer.hpp"
-
-using namespace std::chrono_literals;
-
-TEST(Basic, repeat)
-{
-    Timer timer(Timer::Repeat, 500);
-    int max = 0;
-
-    timer.setHandler([&] () {
-        max ++;
-    });
-    timer.start();
-
-    // Should be at least 5
-    std::this_thread::sleep_for(3s);
-
-    ASSERT_GE(max, 5);
-}
-
-TEST(Basic, restart)
-{
-    Timer timer(Timer::Repeat, 500);
-    int max = 0;
-
-    timer.setHandler([&] () {
-        max ++;
-    });
-
-    timer.start();
-    std::this_thread::sleep_for(3s);
-    timer.stop();
-    std::this_thread::sleep_for(3s);
-    timer.start();
-    std::this_thread::sleep_for(3s);
-
-    ASSERT_GE(max, 10);
-    ASSERT_LT(max, 15);
-}
-
-int main(int argc, char **argv)
-{
-    testing::InitGoogleTest(&argc, argv);
-
-    return RUN_ALL_TESTS();
-}
--- a/modules/timer/timer.hpp	Fri Dec 02 22:20:36 2016 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,185 +0,0 @@
-/*
- * timer.hpp -- threaded timers
- *
- * Copyright (c) 2016 David Demelier <markand@malikania.fr>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- */
-
-#ifndef TIMER_HPP
-#define TIMER_HPP
-
-/**
- * \file timer.hpp
- * \brief Threaded timers.
- * \author David Demelier <markand@malikania.fr>
- */
-
-#include <atomic>
-#include <cassert>
-#include <condition_variable>
-#include <chrono>
-#include <cstdint>
-#include <functional>
-#include <mutex>
-#include <thread>
-
-/**
- * \brief Threaded timer.
- */
-class Timer {
-public:
-    /**
-     * \brief Type of timer.
-     */
-    enum Type {
-        Repeat,                 //!< Periodic timer,
-        Single                  //!< Oneshot timer.
-    };
-
-private:
-    Type m_type;
-    std::atomic<bool> m_alive{true};
-    std::thread m_thread;
-    std::mutex m_mutex;
-    std::condition_variable m_condition;
-    std::function<void ()> m_handler;
-    std::uint32_t m_delay;
-
-    inline void call()
-    {
-        if (m_alive && m_handler)
-            m_handler();
-    }
-
-    inline void wait()
-    {
-        std::unique_lock<std::mutex> lock(m_mutex);
-
-        m_condition.wait_for(lock, std::chrono::milliseconds(m_delay), [&] () {
-            return static_cast<bool>(!m_alive);
-        });
-    }
-
-    inline void runOne()
-    {
-        wait();
-        call();
-    }
-
-    inline void runForever()
-    {
-        while (m_alive) {
-            wait();
-            call();
-        }
-    }
-
-public:
-    /**
-     * Constructor.
-     *
-     * \param type the type of timer
-     * \param delay the delay in milliseconds
-     */
-    inline Timer(Type type, std::uint32_t delay) noexcept
-        : m_type(type)
-        , m_delay(delay)
-    {
-    }
-
-    /**
-     * Stop the timer.
-     */
-    inline ~Timer()
-    {
-        stop();
-    }
-
-    /**
-     * Get the current handler.
-     *
-     * \return the handler
-     */
-    inline const std::function<void ()> &handler() const noexcept
-    {
-        return m_handler;
-    }
-
-    /**
-     * Overloaded function.
-     *
-     * \return the handler
-     */
-    inline std::function<void ()> &handler() noexcept
-    {
-        return m_handler;
-    }
-
-    /**
-     * Set the handler.
-     *
-     * \pre !isActive()
-     * \param handler the handler
-     */
-    inline void setHandler(std::function<void ()> handler)
-    {
-        assert(!m_thread.joinable());
-
-        m_handler = std::move(handler);
-    }
-
-    /**
-     * Tells if a pending thread is still active even if the execution has
-     * finished.
-     *
-     * \return true if active
-     */
-    inline bool isActive() const noexcept
-    {
-        return m_thread.joinable();
-    }
-
-    /**
-     * Start the timer.
-     *
-     * \pre the timer must not be running
-     */
-    void start()
-    {
-        assert(!m_thread.joinable());
-
-        m_alive = true;
-
-        if (m_type == Single)
-            m_thread = std::thread(std::bind(&Timer::runOne, this));
-        else
-            m_thread = std::thread(std::bind(&Timer::runForever, this));
-    }
-
-    /**
-     * Stop the timer.
-     *
-     * Can be safely called multiple times.
-     */
-    void stop()
-    {
-        if (m_thread.joinable()) {
-            m_alive = false;
-            m_condition.notify_one();
-            m_thread.join();
-        }
-    }
-};
-
-#endif // !TIMER_HPP