Skip to content

Commit 5fbe73f

Browse files
authored
Add template type on WFTaskFactory::send_by_name()/signal_by_name(). (#1677)
* Add template type on WFTaskFactory::send_by_name()/signal_by_name(). * Remove unused parameter name.
1 parent d791011 commit 5fbe73f

5 files changed

Lines changed: 36 additions & 44 deletions

File tree

docs/about-conditional.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ public:
5858
static WFConditional *create_conditional(const std::string& cond_name, SubTask *task, void **msgbuf);
5959
static int signal_by_name(const std::string& cond_name, void *msg);
6060
static int signal_by_name(const std::string& cond_name, void *msg, size_t max);
61-
static int signal_by_name(const std::string& cond_name, void *const msg[], size_t max);
61+
template<typename T>
62+
static int signal_by_name(const std::string& cond_name, T *const msg[], size_t max);
6263
};
6364
~~~
6465
我们看到,与普通条件任务唯一区别是,命名条件任务创建时,需要传入一个cond_name。
6566
而signal_by_name()接口,默认将msg发送到所有在这个名称上等待的条件任务,将它们全部唤醒。
66-
也可以通过max参数指定唤醒的最大任务数。此时,msg还可以是一个数组,可给不同的条件任务发送不同的消息。
67+
也可以通过max参数指定唤醒的最大任务数。此时,msg还可以是一个指针数组,可给不同的条件任务发送不同的消息。
6768
任何一个signal_by_name的重载函数,其返回值都是表示实际唤醒的条件任务个数。
6869
这就相当于实现了观察者模式。
6970

src/client/WFKafkaClient.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ void KafkaClientTask::kafka_rebalance_callback(__WFKafkaTask *task)
312312
snprintf(name, 64, "%p.cgroup", member);
313313
member->mutex.unlock();
314314

315-
WFTaskFactory::signal_by_name(name, (void *)NULL, max);
315+
WFTaskFactory::signal_by_name(name, NULL, max);
316316
}
317317
else
318318
{

src/factory/WFTaskFactory.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,7 @@ int WFTaskFactory::send_by_name(const std::string& name, void *msg,
707707
return __mailbox_map.send(name, &msg, max, 0);
708708
}
709709

710+
template<>
710711
int WFTaskFactory::send_by_name(const std::string& name, void *const msg[],
711712
size_t max)
712713
{
@@ -903,6 +904,7 @@ int WFTaskFactory::signal_by_name(const std::string& name, void *msg,
903904
return __conditional_map.signal(name, &msg, max, 0);
904905
}
905906

907+
template<>
906908
int WFTaskFactory::signal_by_name(const std::string& name, void *const msg[],
907909
size_t max)
908910
{

src/factory/WFTaskFactory.h

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <sys/types.h>
2424
#include <sys/uio.h>
2525
#include <time.h>
26-
#include <stdint.h>
2726
#include <utility>
2827
#include <functional>
2928
#include <openssl/ssl.h>
@@ -254,8 +253,7 @@ class WFTaskFactory
254253

255254
/* Count by a counter's name. When count_by_name(), it's safe to count
256255
* exceeding target_value. When multiple counters share a same name,
257-
* this operation will be performed on the first created. If no counter
258-
* matches the name, nothing is performed. */
256+
* this operation will be performed on the first created. */
259257
static int count_by_name(const std::string& counter_name)
260258
{
261259
return WFTaskFactory::count_by_name(counter_name, 1);
@@ -296,7 +294,8 @@ class WFTaskFactory
296294
static int send_by_name(const std::string& mailbox_name, void *msg,
297295
size_t max);
298296

299-
static int send_by_name(const std::string& mailbox_name, void *const msg[],
297+
template<typename T>
298+
static int send_by_name(const std::string& mailbox_name, T *const msg[],
300299
size_t max);
301300

302301
public:
@@ -331,7 +330,8 @@ class WFTaskFactory
331330
static int signal_by_name(const std::string& cond_name, void *msg,
332331
size_t max);
333332

334-
static int signal_by_name(const std::string& cond_name, void *const msg[],
333+
template<typename T>
334+
static int signal_by_name(const std::string& cond_name, T *const msg[],
335335
size_t max);
336336

337337
public:
@@ -409,37 +409,6 @@ class WFTaskFactory
409409
task->sub_series()->set_last_task(last);
410410
return task;
411411
}
412-
413-
private:
414-
/* Some compilers don't declare 'nullptr_t' although required by C++11. */
415-
using nullptr_t = std::nullptr_t;
416-
417-
public:
418-
/* The following functions are for overload resolution only. */
419-
420-
static int send_by_name(const std::string& mailbox_name, intptr_t msg,
421-
size_t max)
422-
{
423-
return WFTaskFactory::send_by_name(mailbox_name, (void *)msg, max);
424-
}
425-
426-
static int send_by_name(const std::string& mailbox_name, nullptr_t msg,
427-
size_t max)
428-
{
429-
return WFTaskFactory::send_by_name(mailbox_name, (void *)0, max);
430-
}
431-
432-
static int signal_by_name(const std::string& cond_name, intptr_t msg,
433-
size_t max)
434-
{
435-
return WFTaskFactory::signal_by_name(cond_name, (void *)msg, max);
436-
}
437-
438-
static int signal_by_name(const std::string& cond_name, nullptr_t msg,
439-
size_t max)
440-
{
441-
return WFTaskFactory::signal_by_name(cond_name, (void *)0, max);
442-
}
443412
};
444413

445414
template<class REQ, class RESP>

src/factory/WFTaskFactory.inl

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,26 @@ WFTaskFactory::create_dynamic_task(dynamic_create_t create)
6363
return new __WFDynamicTask(std::move(create));
6464
}
6565

66+
template<>
67+
int WFTaskFactory::send_by_name(const std::string&, void *const *, size_t);
68+
69+
template<typename T>
70+
int WFTaskFactory::send_by_name(const std::string& mailbox_name, T *const msg[],
71+
size_t max)
72+
{
73+
return WFTaskFactory::send_by_name(mailbox_name, (void *const *)msg, max);
74+
}
75+
76+
template<>
77+
int WFTaskFactory::signal_by_name(const std::string&, void *const *, size_t);
78+
79+
template<typename T>
80+
int WFTaskFactory::signal_by_name(const std::string& cond_name, T *const msg[],
81+
size_t max)
82+
{
83+
return WFTaskFactory::signal_by_name(cond_name, (void *const *)msg, max);
84+
}
85+
6686
template<class REQ, class RESP, typename CTX = bool>
6787
class WFComplexClientTask : public WFClientTask<REQ, RESP>
6888
{
@@ -709,7 +729,7 @@ void WFTaskFactory::reset_go_task(WFGoTask *task, FUNC&& func, ARGS&&... args)
709729

710730
template<> inline
711731
WFGoTask *WFTaskFactory::create_go_task(const std::string& queue_name,
712-
nullptr_t&& func)
732+
std::nullptr_t&&)
713733
{
714734
return new __WFGoTask(WFGlobal::get_exec_queue(queue_name),
715735
WFGlobal::get_compute_executor(),
@@ -719,7 +739,7 @@ WFGoTask *WFTaskFactory::create_go_task(const std::string& queue_name,
719739
template<> inline
720740
WFGoTask *WFTaskFactory::create_timedgo_task(time_t seconds, long nanoseconds,
721741
const std::string& queue_name,
722-
nullptr_t&& func)
742+
std::nullptr_t&&)
723743
{
724744
return new __WFTimedGoTask(seconds, nanoseconds,
725745
WFGlobal::get_exec_queue(queue_name),
@@ -729,21 +749,21 @@ WFGoTask *WFTaskFactory::create_timedgo_task(time_t seconds, long nanoseconds,
729749

730750
template<> inline
731751
WFGoTask *WFTaskFactory::create_go_task(ExecQueue *queue, Executor *executor,
732-
nullptr_t&& func)
752+
std::nullptr_t&&)
733753
{
734754
return new __WFGoTask(queue, executor, nullptr);
735755
}
736756

737757
template<> inline
738758
WFGoTask *WFTaskFactory::create_timedgo_task(time_t seconds, long nanoseconds,
739759
ExecQueue *queue, Executor *executor,
740-
nullptr_t&& func)
760+
std::nullptr_t&&)
741761
{
742762
return new __WFTimedGoTask(seconds, nanoseconds, queue, executor, nullptr);
743763
}
744764

745765
template<> inline
746-
void WFTaskFactory::reset_go_task(WFGoTask *task, nullptr_t&& func)
766+
void WFTaskFactory::reset_go_task(WFGoTask *task, std::nullptr_t&&)
747767
{
748768
((__WFGoTask *)task)->set_go_func(nullptr);
749769
}

0 commit comments

Comments
 (0)