Branch data Line data Source code
1 : : /* 2 : : ** Copyright (C) 2020 CERN 3 : : ** 4 : : ** This software is provided 'as-is', without any express or implied 5 : : ** warranty. In no event will the authors be held liable for any damages 6 : : ** arising from the use of this software. 7 : : ** 8 : : ** Permission is granted to anyone to use this software for any purpose, 9 : : ** including commercial applications, and to alter it and redistribute it 10 : : ** freely, subject to the following restrictions: 11 : : ** 12 : : ** 1. The origin of this software must not be misrepresented; you must not 13 : : ** claim that you wrote the original software. If you use this software 14 : : ** in a product, an acknowledgment in the product documentation would be 15 : : ** appreciated but is not required. 16 : : ** 2. Altered source versions must be plainly marked as such, and must not be 17 : : ** misrepresented as being the original software. 18 : : ** 3. This notice may not be removed or altered from any source distribution. 19 : : ** 20 : : ** Created on: 2020-10-06T10:01:02+01:00 21 : : ** Author: Matteo Ferrari <matteof> <matteo.ferari.1@cern.ch> 22 : : ** 23 : : */ 24 : : #ifndef CCUT_THREAD_HPP 25 : : #define CCUT_THREAD_HPP 26 : : 27 : : #include <atomic> 28 : : #include <condition_variable> 29 : : #include <memory> 30 : : #include <mutex> 31 : : #include <string> 32 : : #include <thread> 33 : : 34 : : namespace ccut { 35 : : /** 36 : : * @addtogroup Threading Threading: multi-threading related classes 37 : : * @{ 38 : : */ 39 : : 40 : : class Thread 41 : : { 42 : : public: 43 : 6 : Thread() : Thread(std::string()) {}; // For backward compatibility 44 : : /** 45 : : * @brief Thread constructor 46 : : * @param[in] name name of the thread (used with pthread to set the name) 47 : : */ 48 : : explicit Thread(const std::string &name); 49 : : 50 : : virtual ~Thread(); 51 : : 52 : : /* disable copy */ 53 : : Thread(const Thread &) = delete; 54 : : Thread &operator=(const Thread &) = delete; 55 : : /* allow rvalue construct */ 56 : : Thread(Thread &&) noexcept; 57 : : 58 : : /** 59 : : * @brief start the thread 60 : : */ 61 : : void start(); 62 : : 63 : : /** 64 : : * @brief stop the thread 65 : : */ 66 : : void stop(); 67 : : 68 : : /** 69 : : * @brief run the thread function in the current context 70 : : */ 71 : : void run(); 72 : : 73 : : /** 74 : : * @brief wake the thread 75 : : */ 76 : : virtual void wake(); 77 : : 78 : : /** 79 : : * @brief set interval (auto wake) 80 : : */ 81 : : void setInterval(const std::chrono::milliseconds &ms); 82 : : 83 : : /** 84 : : * @brief set policy and priority 85 : : * @details see "man pthread_setschedparam" 86 : : */ 87 : : void setPriority(int policy, int32_t priority); 88 : : 89 : : /** 90 : : * @brief first function executed after the thread is started 91 : : */ 92 : 11 : virtual void thread_enter() {}; 93 : : 94 : : /** 95 : : * @brief function executed on every awake of the thread 96 : : */ 97 : : virtual void thread_func() = 0; 98 : : 99 : : /** 100 : : * @brief function executed after stop is called 101 : : */ 102 : 13 : virtual void thread_exit() {}; 103 : : 104 : : protected: 105 : : void _thread_func(); 106 : : bool _set_thread_priority(int policy, int32_t priority); 107 : : 108 : : std::string m_name; 109 : : std::atomic<bool> m_started; 110 : : bool m_waked; 111 : : int m_policy; 112 : : int32_t m_priority; 113 : : 114 : : mutable std::mutex m_mutex; 115 : : mutable std::condition_variable m_cond; 116 : : std::unique_ptr<std::thread> m_thread; 117 : : std::chrono::milliseconds m_interval; 118 : : }; 119 : : 120 : : /** @} */ 121 : : } // namespace ccut 122 : : 123 : : #endif // CCUT_THREAD_HPP