AsyncTask.h
// AsyncTask.h
//
// Author David Barrett-Lennard
// (C)opyright Cedanet Pty Ltd 2022
#pragma once
#ifndef Ceda_cxMessage_AsyncTask_H
#define Ceda_cxMessage_AsyncTask_H
#include "cxMessage.h"
#include <atomic>
#include <memory>
#include <functional>
namespace ceda
{
class IoContextPool;
class AsyncTaskExecuter;
class AsyncPeriodicTimer;
class AsyncRepeatedTask;
class AsyncSignaledTask;
class AsyncTimeOut;
cxMessage_API bool ThreadsAreEnabled();
// numThreads must be nonnegative.
// If 0 then the number of threads is set to std::thread::hardware_concurrency()
cxMessage_API IoContextPool* CreateIoContextPool(int numThreads);
cxMessage_API void Close(IoContextPool*);
// If this is not called then the default IoContextPool uses std::thread::hardware_concurrency() threads.
// If this function is used to set a different number of threads then it must be called at most once and before the
// first call to GetTheDefaultIoContextPool()
cxMessage_API void SetTheDefaultIoContextPoolSize(int numThreads);
cxMessage_API IoContextPool* GetTheDefaultIoContextPool();
cxMessage_API std::shared_ptr<AsyncTaskExecuter> MakeAsyncTaskExecuter(IoContextPool* pool = nullptr);
cxMessage_API void Post(AsyncTaskExecuter& ate, std::function<void()> task);
cxMessage_API const std::atomic<bool>& GetAbortFlag(const AsyncTaskExecuter& ate);
cxMessage_API void Stop(AsyncTaskExecuter& ate);
cxMessage_API std::shared_ptr<AsyncPeriodicTimer> MakeAsyncPeriodicTimer(IoContextPool* pool = nullptr);
cxMessage_API void Start(AsyncPeriodicTimer& e, int timeoutMilliSecs, std::function<void(bool expired, std::atomic<bool>& abort)> task);
cxMessage_API void Reset(AsyncPeriodicTimer& e, int timeoutMilliSecs = -1);
cxMessage_API void Stop(AsyncPeriodicTimer& e);
cxMessage_API std::shared_ptr<AsyncRepeatedTask> MakeAsyncRepeatedTask(IoContextPool* pool = nullptr);
cxMessage_API void SetDelay(AsyncRepeatedTask& art, int delayMilliSecs);
cxMessage_API void Start(AsyncRepeatedTask& art, int delayMilliSecs, std::function<void(std::atomic<bool>& abort)> taskFn);
cxMessage_API void Stop(AsyncRepeatedTask& art);
cxMessage_API std::shared_ptr<AsyncSignaledTask> MakeAsyncSignaledTask(IoContextPool* pool = nullptr);
cxMessage_API void Start(AsyncSignaledTask& ast, std::function<void(std::atomic<bool>& abort)> taskFn);
cxMessage_API void Signal(AsyncSignaledTask& ast);
cxMessage_API void Stop(AsyncSignaledTask& ast);
cxMessage_API std::shared_ptr<AsyncTimeOut> MakeAsyncTimeOut(IoContextPool* pool = nullptr);
cxMessage_API void Start(AsyncTimeOut& ato, int timeoutMilliSecs, std::function<void(bool expired, std::atomic<bool>& abort)> taskFn);
cxMessage_API void Reset(AsyncTimeOut& ato);
cxMessage_API void Reset(AsyncTimeOut& ato, int timeoutMilliSecs);
cxMessage_API void AsyncReset(AsyncTimeOut& ato, int timeoutMilliSecs = -1);
cxMessage_API void Stop(AsyncTimeOut& ato);
} // namespace ceda
#endif // include guard