diff options
author | Sven Gothel <[email protected]> | 2022-01-12 05:04:32 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2022-01-12 05:04:32 +0100 |
commit | 3ae582af7bc496dc31756a8245da9bcfd41e0744 (patch) | |
tree | f5fc31981b360135933743701e97bc2ef8ef31d7 /include/jau | |
parent | 0aaacdea88915710e8d773192ae957f4f594d7af (diff) |
Added `jau::service_runner`, a reusable dedicated thread performing custom user servicesv0.7.7
Diffstat (limited to 'include/jau')
-rw-r--r-- | include/jau/service_runner.hpp | 140 |
1 files changed, 140 insertions, 0 deletions
diff --git a/include/jau/service_runner.hpp b/include/jau/service_runner.hpp new file mode 100644 index 0000000..867578d --- /dev/null +++ b/include/jau/service_runner.hpp @@ -0,0 +1,140 @@ +/* + * Author: Sven Gothel <[email protected]> + * Copyright (c) 2022 Gothel Software e.K. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef JAU_SERVICERUNNER_HPP_ +#define JAU_SERVICERUNNER_HPP_ + +#include <cstring> +#include <string> +#include <cstdint> +#include <limits> +#include <atomic> +#include <memory> +#include <mutex> +#include <condition_variable> +#include <thread> +#include <algorithm> + +#include <jau/cpp_lang_util.hpp> +#include <jau/basic_types.hpp> +#include <jau/ordered_atomic.hpp> +#include <jau/function_def.hpp> + +namespace jau { + + /** + * Service runner, a reusable dedicated thread performing custom user services. + */ + class service_runner { + public: + typedef service_runner& service_runner_ref; + typedef FunctionDef<void, service_runner_ref> Callback; + + private: + std::string name; + + /** + * Maximum time in milliseconds to wait for a thread shutdown. + */ + nsize_t service_shutdown_timeout_ms; + + Callback service_work; + Callback service_init_locked; + Callback service_end_locked; + Callback service_end_post_notify; + + jau::sc_atomic_bool shall_stop; + jau::sc_atomic_bool running; + pthread_t thread_id; + + std::mutex mtx_lifecycle; + std::condition_variable cv_init; + + void workerThread(); + + public: + /** + * Service runner constructor. + * + * start() shall be issued to kick off this service. + * + * @param name service name + * @param service_shutdown_timeout_ms maximum time in milliseconds to wait for a thread shutdown, may be nullptr + * @param service_work service working function + * @param service_init_locked optional service init function, lifecycle mutex is locked + * @param service_end_locked optional service end function, lifecycle mutex is locked + * @param service_end_post_notify optional service end function post mutex lock and notify + */ + service_runner(const std::string& name, + const nsize_t service_shutdown_timeout_ms, + Callback service_work, + Callback service_init_locked = Callback(), + Callback service_end_locked = Callback(), + Callback service_end_post_notify = Callback()) noexcept; + + /** + * Service runner destructor. + * + * Issues stop() + */ + ~service_runner() noexcept; + + std::string get_name() const noexcept { return name; } + pthread_t get_threadid() const noexcept { return thread_id; } + + bool is_running() const noexcept { return running; } + bool get_shall_stop() const noexcept { return shall_stop; } + + /** + * Starts this service + */ + void start() noexcept; + + /** + * Stops this service + * + * Method attempts to stop the worker thread + * - by flagging `shall stop` + * - sending SIGALRM + * - waiting until running and not timeout + */ + void stop() noexcept; + + /** + * Only marks the worker thread to stop in due process + * by flagging `shall stop`. + */ + void set_shall_stop() noexcept { shall_stop = true; } + + /** + * Returns a string representation of this service + */ + std::string toString() const noexcept; + }; + +} // namespace jau + + + +#endif /* JAU_SERVICERUNNER_HPP_ */ |