1#ifndef _DRAMA2_TASK_INC
2#define _DRAMA2_TASK_INC
73 void AddActionFunction(Task *,
74 const std::string &name,
76 const std::string &
descr);
153 typedef std::deque<OrphanDetails> OrphanDetailsQueue;
162#ifndef RUNNING_DOXYGEN
170#define DRAMA2_LOCK_DEBUG
182#ifdef DRAMA2_LOCK_DEBUG2
187#ifndef DRAMA2_LOCK_DEBUG
188#define DRAMA2_LOCK_DEBUG
194#ifdef DRAMA2_TASK_CPP
195#warning "Lock debugging level 2 enabled - will slow things down"
199#define DRAMA2_LOCK_DEBUG2_Only(_A_) _A_
201#define DRAMA2_LOCK_DEBUG2_Only(_A_)
205#ifdef DRAMA2_LOCK_DEBUG
216#ifdef DRAMA2_LOCK_DEBUG2
264 const std::string &
file,
282 const std::string &
func,
283 const std::string &
file,
301 const std::string &
func,
302 const std::string &
file,
320 const std::string &
func,
321 const std::string &
file,
336 const std::string &
file,
414 class Task :
public std::enable_shared_from_this<Task> {
425#ifdef DRAMA2_LOCK_DEBUG
428 typedef std::recursive_timed_mutex
mutexType;
433 typedef std::lock_guard<mutexType>
guardType;
457 std::thread::id _taskCreThreadId;
458 std::thread::id _taskRunThreadId;
468 typedef std::set<RunDramaExitNotifier *> RunDramaExitNotifierSetType;
482 OrphanDetailsQueue _orphanQueue;
497 static Task *orphanTask;
513 RunDramaExitNotifierSetType _loopExitNotifiers;
518 std::unique_ptr<logging::Logger> _taskLogger;
527 virtual void AddAction(
const std::string &name,
bool spawnable,
528 ActionHandlerPtr
obj,
529 const std::string &
descr);
534 std::shared_ptr<Task> _sharedSelf =
nullptr;
539 bool _sharedInit =
false;
544 bool _amShared =
false;
550 bool _sepThreadToRunDramaOK =
false;
562 void AddMethodAction(
565 const std::string &
descr);
567 void AddMethodAction(
570 const std::string &
descr);
650 _sepThreadToRunDramaOK =
true;
670 const std::string &
descr =
"") {
688 virtual void Add(
const std::string &name, MessageHandler *
obj,
689 const std::string &
descr =
"") {
710 const std::string &
descr =
"") {
712 Add(name, std::make_shared<MessageHandlerViaFunctor>(
func),
descr);
733 const std::string &
descr =
"") {
734 thread::AddActionFunction(
this, name,
func,
descr);
755 const std::string &
descr =
"") {
774 const std::string &
descr =
"") {
794 template <
typename T>
798 const std::string &
descr =
"") {
824 template <
typename T>
828 const std::string &
descr =
"") {
857 return _taskCreThreadId;
869 return _taskRunThreadId;
918#ifdef DRAMA2_LOCK_DEBUG
921 _orphanQueue.push_front(
orphan);
948 std::weak_ptr<Task>
TaskPtr() {
993 catch (
const std::bad_weak_ptr &
e)
998 _sharedSelf = std::shared_ptr<Task>(
this,
drama::nodel());
1016 template <
typename T>
1018 return std::dynamic_pointer_cast<T>(std::shared_ptr<Task>(
TaskPtr()));
1074 void Signal(
const std::string &name, sds::Id *arg=
nullptr,
void *data=
nullptr);
1112 void Signal(
long int index, sds::Id *arg=
nullptr,
void *data=
nullptr);
1193 static void CheckLockTaken(
const std::string &
func,
1194 const std::string &
file,
1232 static_assert(std::is_base_of<Task, TaskClass>(),
"TaskClass must be a sub-class of drama::task");
1242 std::cerr <<
"CreateRunDramaTask():drama::Exception thrown"
1247 exit (
e.statusAsSysExitCode());
1249 catch (
const std::exception &
e)
1251 std::cerr <<
"CreateRunDramaTask():std::exception thrown."
1259 std::cerr <<
"CreateRunDramaTask():Non-standard exception thrown."
1294 template <
class TaskClass>
1297 static_assert(std::is_base_of<Task, TaskClass>(),
"TaskClass must be a sub-class of drama::task");
1306 std::cerr <<
"CreateRunDramaTask():drama::Exception thrown"
1311 exit (
e.statusAsSysExitCode());
1313 catch (
const std::exception &
e)
1315 std::cerr <<
"CreateRunDramaTask():std::exception thrown."
1323 std::cerr <<
"CreateRunDramaTask():Non-standard excepton thrown."
An Exception class for exceptions thrown by DRAMA V2 classes.
Definition exception.hh:164
A class which implements a DRAMA Message Handler.
Definition messagehandler.hh:138
DitsPathType path
The DITS path the transaction was sent to.
Definition task.hh:151
OrphanDetails(TransEvtInfo evtInfo, sds::IdPtr arg, DitsPathType thePath, DitsTransIdType tid)
Construct an OrphanDetails item.
Definition task.hh:161
sds::IdPtr argument
The Argument to the transaction.
Definition task.hh:150
DitsTransIdType transId
The DITS Transaction ID.
Definition task.hh:152
Structure which maintains details on orphan transactions.
Definition task.hh:147
Class used by Obey and Kick handlers to indicate rescheduling requirements.
Definition request.hh:78
virtual bool JoinThreads(std::chrono::steady_clock::time_point until)=0
Invoked by DRAMA when the run loop exists.
virtual int RunDramaHasExited()=0
Invoked by DRAMA when the run loop exits.
Class used to arrange for notifications when the RunDrama exits.
Definition task.hh:382
A class which implements a Spawnable DRAMA Message Handler.
Definition spawnable.hh:84
static const int DefBufSize
Default (minimum) global buffer size.
Definition task.hh:634
std::shared_ptr< T > TaskPtrAs()
Returns a shared pointer to the task, dynamically cast to the template type.
Definition task.hh:1044
virtual void Add(const std::string &name, MessageHandlerPtr obj, const std::string &descr="")
Add an action to the task.
Definition task.hh:696
virtual void AddTA(const std::string &name, thread::ThreadActionFunction func, const std::string &descr="")
Add an threaded action to the task.
Definition task.hh:759
Task(const std::string &name, int buffer=DefBufSize, int flags=0, int selfBytes=DefSelfBufSize)
Create a new DRAMA task of the specified name.
virtual void AddSpawnable(const std::string &name, SpawnablePtr obj, const std::string &descr="")
Add a spawnable action to the task.
Definition task.hh:781
void(Task::* ThreadActionMethod)(drama::thread::TAction *taction, const drama::sds::Id &id)
This type is for methods of the task which implement threaded actions.
Definition task.hh:477
void Signal(long int index, sds::Id *arg=nullptr, void *data=nullptr)
Trigger the rescheduling of an action, specifying the index of the action.
std::unique_lock< mutexType > uniqueLockType
Defines the type of a unique_lock type using our mutex type.
Definition task.hh:463
virtual void RunDrama()
Run the DRAMA message loop in the current thread.
virtual double GetJoinTimeout() const
Get the thread join timeout.
std::string GetTaskDescription(const std::string &taskName) const
Get the description of a task.
std::string TaskName() const
Return task name.
Definition task.hh:918
void AddMth(const std::string &actionName, T method, const std::string &descr="")
Add an action to the task, implemented by a method.
Definition task.hh:822
mutexType & Lock()
Reference the DRAMA Task lock.
Definition task.hh:871
std::thread::id GetTaskRunThreadId() const
Return the thread ID of the thread running RunDrama()
Definition task.hh:895
virtual void Add(const std::string &name, MessageReceiveFunction func, const std::string &descr="")
Add an action to the task.
Definition task.hh:736
std::recursive_timed_mutex mutexType
Defines the type of our mutex.
Definition task.hh:455
void AddOrphanToQueue(const OrphanDetails &orphan)
Add an orphan to the queue of orphans to be processed before we next look at the DRAMA message queue.
Definition task.hh:944
void SeparateThreadRunsDrama()
Tell the task we are expecting a separate thread to run the DRAMA message loop.
Definition task.hh:676
logging::Logger & Logger()
Reference to DRAMA 2 Logger.
Definition task.hh:910
int GetTaskType(const std::string &taskName) const
Get the type of a task.
void Signal(const std::string &name, sds::Id *arg=nullptr, void *data=nullptr)
Trigger the rescheduling of an action, specifing the name of the action.
virtual void _MessageUser(const std::string &text) const
Use DRAMA to send a message to the user.
virtual void AddSpawnable(const std::string &name, Spawnable *obj, const std::string &descr="")
Add an action to the task.
Definition task.hh:800
void CancelNotifyOnRunDramaExit(RunDramaExitNotifier *notifier)
Cancel notification for when the DRAMA RunDrama() exits.
std::lock_guard< mutexType > guardType
Defines the type of a lock guard using our mutex type.
Definition task.hh:460
virtual void OrphanHandler(const OrphanDetails &details)
Orphan transaction handler.
void AddMthThd(const std::string &actionName, T method, const std::string &descr="")
Add an action to the task, implemented by a method.
Definition task.hh:852
virtual void Add(const std::string &name, MessageHandler *obj, const std::string &descr="")
Add an action to the task.
Definition task.hh:715
std::weak_ptr< Task > TaskPtr()
Returns a weak pointer to the task.
Definition task.hh:975
std::thread::id GetTaskCreThreadId() const
Return the thread ID of the thread on which the Task object was created.
Definition task.hh:883
static const int DefSelfBufSize
Default (minimum) self buffer size.
Definition task.hh:635
virtual ~Task()
Destructor - shut down the DRAMA task.
drama::Request(Task::* ActionMethod)(drama::MessageHandler *mh)
This type is for methods the task which implement actions.
Definition task.hh:470
void SetDetails(const std::string &descr, int type=0)
Set task details.
void NotifyOnRunDramaExit(RunDramaExitNotifier *notifier)
Arrange notification for when the DRAMA RunDrama() exits.
A class which implements a DRAMA task.
Definition task.hh:441
Implementation of a Class supporting Logging within an AAO DRAMA Task.
Definition logger.hh:769
A C++ Interface to the handling SDS structures.
Definition sds.hh:428
A class which implements a DRAMA Action with runs a thread.
Definition threadaction.hh:199
Header file for a DRAMA 2 class which implements Logging.
DRAMA 2 include file - Message Handler class definition.
std::shared_ptr< Id > IdPtr
A shared pointer for sds::Id items.
Definition sds.hh:3318
std::function< void(TAction *, const sds::Id &)> ThreadActionFunction
Type used for functions specified drama::Task::Add().
Definition task.hh:96
void CreateRunDramaTask()
Create and run a DRAMA task, with standard exception handling.
Definition task.hh:1322
std::shared_ptr< Spawnable > SpawnablePtr
This type is used for passing Spawnable object addresses around.
Definition spawnable.hh:200
EntryCode
Entry type code - indicates the type of a DRAMA event.
Definition entryinfo.hh:67
@ Signal
Signal message received.
std::shared_ptr< MessageHandler > MessageHandlerPtr
This type is used for passing MessageHandler object addresses around.
Definition messagehandler.hh:101
void CheckLockTaken(const std::string func, const std::string &file, const int lineNum)
Ensure the current thread has taken the DRAMA task lock.
std::function< Request(MessageHandler *)> MessageReceiveFunction
Type used for functions specified to drama::MessageHandler::PutObeyHandler(), drama::MessageHandler::...
Definition messagehandler.hh:111
The drama namespace contains all the classes, types etc of the DRAMA 2 implementation.
Definition drama.hh:93
DRAMA 2 include file - Spawnable action implementation.
DitsBulkInfoType bulkInfo
Details on bulk data transfer.
Definition task.hh:134
EntryCode entryReason
Reason for entry.
Definition task.hh:129
StatusType entryStatus
Entry status value.
Definition task.hh:130
std::string entryName
Name with entry.
Definition task.hh:131
std::string loadErrText
Load error text
Definition task.hh:132
bool complete
IS transaction complete.
Definition task.hh:133
Structure is used to store details about a DRAMA reschedule message relating to a transaction,...
Definition task.hh:128
void operator()(void const *) const
Invoked to delete the memory used.
Definition task.hh:117
Declare an operator to be used as a deletion operator by std::shared_ptr.
Definition task.hh:114