AIStatefulTask ‐ Asynchronous, Stateful Task Scheduler library.

Threads-like task objects evolving through user-defined states.

Public Member Functions | List of all members
AIPackagedTask< R(Args...)> Class Template Reference

Detailed Description

template<typename R, typename ... Args>
class AIPackagedTask< R(Args...)>

A wrapper for a (member) function to be executed by the AIThreadPool.

An AIPackagedTask is supposed to be a member of a class (e.g. MyTask) derived from AIStatefulTask. The this pointer of that task should be passed to the constructor, along with a function to be called and a handle of the queue to add it to (as obtained by a call to AIThreadPool::new_queue()).

It is possible to use a pointer to a free function or to the member function of a given object. In both cases with arbitrary signature.

For example, suppose you have some object foo of type Foo with a member function called retrieve that has the signature bool Foo::retrieve(int, double), whatever those parameters may mean. Then you could do:

class MyTask : public AIStatefulTask
{
AIPackagedTask<bool(int, double)> m_retrieve; // Space holder for all variables involved.
static constexpr condition_type retrieve_condition = 1; // The condition bit to be used.
public:
MyTask(AIQueueHandle queue_handle) : m_retrieve(this, retrieve_condition, &foo, &Foo::retrieve, queue_handle) { }
...
};
AIStatefulTask::condition_type condition_type
Proxy for AIStatefulTask::condition_type.
Definition: AIFriendOfStatefulTask.h:64
AIPackagedTask(AIStatefulTask *parent_task, AIStatefulTask::condition_type condition, R(*fp)(Args...), AIQueueHandle object_queue_handle)
Definition: AIPackagedTask.h:179
Definition: AIStatefulTask.h:96

where as usual retrieve_condition should be different from any other condition bits that this task is using. And where queue_handle is a handle that was previously returned by AIThreadPool::new_queue().

Then this function can be called (repeatedly, with different parameters if need be) from multiplex_impl as follows:

...
case MyTask_state20:
m_retrieve(n, y); // Copy parameters to m_retrieve.
set_state(MyTask_dispatch);
[[fallthrough]];
case MyTask_dispatch:
if (!m_retrieve.dispatch()) // Put m_retrieve in the queue.
{
yield_frames(1); // Yield because the queue was full.
break;
}
set_state(MyTask_state21); // Continue with state21 once the
break; // function finished executing.
case MyTask_state21:
{
bool result = m_retrieve.get(); // Get the result.
void set_state(state_type new_state)
Proxy for AIStatefulTask::set_state.
Definition: AIFriendOfStatefulTask.h:73

#include <AIPackagedTask.h>

Inheritance diagram for AIPackagedTask< R(Args...)>:
Inheritance graph
[legend]
Collaboration diagram for AIPackagedTask< R(Args...)>:
Collaboration graph
[legend]

Public Member Functions

 AIPackagedTask (AIStatefulTask *parent_task, AIStatefulTask::condition_type condition, R(*fp)(Args...), AIQueueHandle object_queue_handle)
 
template<class C >
 AIPackagedTask (AIStatefulTask *parent_task, AIStatefulTask::condition_type condition, C *object, R(C::*memfp)(Args...), AIQueueHandle object_queue_handle)
 
 ~AIPackagedTask ()
 Destructor.
 
void swap (AIPackagedTask &other) noexcept
 Exchange the state with that of other.
 
void operator() (Args... args)
 Copy the arguments.
 
bool dispatch ()
 
get () const
 

Additional Inherited Members

- Public Types inherited from AIFriendOfStatefulTask
using state_type = AIStatefulTask::state_type
 Proxy for AIStatefulTask::state_type.
 
using condition_type = AIStatefulTask::condition_type
 Proxy for AIStatefulTask::condition_type.
 
- Protected Member Functions inherited from AIFriendOfStatefulTask
 AIFriendOfStatefulTask (AIStatefulTask *task)
 Construct a friend of task.
 
void set_state (state_type new_state)
 Proxy for AIStatefulTask::set_state.
 
void wait (condition_type conditions)
 Proxy for AIStatefulTask::wait.
 
void wait_until (AIWaitConditionFunc const &wait_condition, condition_type conditions)
 Proxy for AIStatefulTask::wait_until.
 
void wait_until (AIWaitConditionFunc const &wait_condition, condition_type conditions, state_type new_state)
 Proxy for AIStatefulTask::wait_until.
 
void finish ()
 Proxy for AIStatefulTask::finish.
 
void yield ()
 Proxy for AIStatefulTask::yield.
 
void target (AIEngine *engine)
 Proxy for AIStatefulTask::target.
 
void yield (AIEngine *engine)
 Proxy for AIStatefulTask::yield.
 
void yield_frame (AIEngine *engine, unsigned int frames)
 Proxy for AIStatefulTask::yield_frame.
 
void yield_ms (AIEngine *engine, unsigned int ms)
 Proxy for AIStatefulTask::yield_ms.
 
bool yield_if_not (AIEngine *engine)
 Proxy for AIStatefulTask::yield_if_not.
 
- Protected Attributes inherited from AIFriendOfStatefulTask
AIStatefulTaskm_task
 The base class of the object that this object is a member of.
 

Constructor & Destructor Documentation

◆ AIPackagedTask() [1/2]

template<typename R , typename ... Args>
AIPackagedTask< R(Args...)>::AIPackagedTask ( AIStatefulTask parent_task,
AIStatefulTask::condition_type  condition,
R(*)(Args...)  fp,
AIQueueHandle  object_queue_handle 
)
inline

Construct a packaged task for a free function.

Parameters
parent_taskThe task that dispatch will call wait_until and signal on.
conditionThe condition to use for wait_until and signal.
fpA pointer to the function that needs to be called.
object_queue_handleA handle to the AIObjectQueue that the delayed function should be placed in.

◆ AIPackagedTask() [2/2]

template<typename R , typename ... Args>
template<class C >
AIPackagedTask< R(Args...)>::AIPackagedTask ( AIStatefulTask parent_task,
AIStatefulTask::condition_type  condition,
C *  object,
R(C::*)(Args...)  memfp,
AIQueueHandle  object_queue_handle 
)
inline

Construct a packaged task for a member function.

Parameters
parent_taskThe task that dispatch will call wait_until and signal on.
conditionThe condition to use for wait_until and signal.
objectPointer to the object of which the member function must be called.
memfpA pointer to the member function that needs to be called.
object_queue_handleA handle to the AIObjectQueue that the delayed function should be placed in.

Member Function Documentation

◆ dispatch()

template<typename R , typename ... Args>
bool AIPackagedTask< R(Args...)>::dispatch

Put the task in a queue for execution in a different thread.

Actually queue the task in the AIObjectQueue whose handle was passed to the constructor and halt the parent_task as passed to the constructor until this task is finished.

Returns
True if the task was successfully queued; false if the queue was full.

◆ get()

template<typename R , typename ... Args>
R AIPackagedTask< R(Args...)>::get ( ) const
inline

Read out the result of the function.

May only be called after parent_task->signal(condition) was called.


The documentation for this class was generated from the following file: