diff --git a/src/factory/WFTask.h b/src/factory/WFTask.h index 421fa946..110a01a0 100644 --- a/src/factory/WFTask.h +++ b/src/factory/WFTask.h @@ -620,6 +620,14 @@ public: this->mailbox = mailbox; } + WFMailboxTask(std::function&& cb) : + next(&this->user_data), + value(2), + callback(std::move(cb)) + { + this->mailbox = &this->user_data; + } + protected: virtual ~WFMailboxTask() { } }; diff --git a/src/factory/WFTaskFactory.cc b/src/factory/WFTaskFactory.cc index fbf77ad1..284ad49e 100644 --- a/src/factory/WFTaskFactory.cc +++ b/src/factory/WFTaskFactory.cc @@ -295,6 +295,33 @@ void WFTaskFactory::count_by_name(const std::string& counter_name, unsigned int __CounterMap::get_instance()->count_n(counter_name, n); } +/********MailboxTask*************/ + +class __WFMailboxTask : public WFMailboxTask +{ +public: + __WFMailboxTask(size_t size, mailbox_callback_t&& cb) : + WFMailboxTask(new void *[size], size, std::move(cb)) + { + } + + virtual ~__WFMailboxTask() + { + delete []this->mailbox; + } +}; + +WFMailboxTask *WFTaskFactory::create_mailbox_task(size_t size, + mailbox_callback_t callback) +{ + return new __WFMailboxTask(size, std::move(callback)); +} + +WFMailboxTask *WFTaskFactory::create_mailbox_task(mailbox_callback_t callback) +{ + return new WFMailboxTask(std::move(callback)); +} + WFDNSTask *WFTaskFactory::create_dns_task(const std::string& host, unsigned short port, dns_callback_t callback) diff --git a/src/factory/WFTaskFactory.h b/src/factory/WFTaskFactory.h index 64fd9d4e..c3e3838a 100644 --- a/src/factory/WFTaskFactory.h +++ b/src/factory/WFTaskFactory.h @@ -83,6 +83,10 @@ using fsync_callback_t = std::function; using timer_callback_t = std::function; using counter_callback_t = std::function; +// Mailbox is like counter with data passing +using mailbox_callback_t = std::function; + +// Graph (DAG) task. using graph_callback_t = std::function; // DNS task. For internal usage only. @@ -214,6 +218,13 @@ public: * creation, and more than one counter may reach target value. */ static void count_by_name(const std::string& counter_name, unsigned int n); +public: + static WFMailboxTask *create_mailbox_task(size_t size, + mailbox_callback_t callback); + + /* Use 'user_data' as mailbox. Store only one message. */ + static WFMailboxTask *create_mailbox_task(mailbox_callback_t callback); + public: template static WFGoTask *create_go_task(const std::string& queue_name,