Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/factory/WFTaskFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,37 @@ class __WFTimerTask : public WFTimerTask
}
};

class __WFCanceledTimerTask : public __WFTimerTask
{
protected:
virtual void dispatch()
{
if (this->scheduler->sleep(this) >= 0)
this->cancel();
else
this->handle(WFT_STATE_SYS_ERROR, errno);
}

public:
__WFCanceledTimerTask(CommScheduler *scheduler, timer_callback_t&& cb) :
__WFTimerTask(-1, 0, scheduler, std::move(cb))
{
}
};

WFTimerTask *WFTaskFactory::create_timer_task(time_t seconds, long nanoseconds,
timer_callback_t callback)
{
return new __WFTimerTask(seconds, nanoseconds, WFGlobal::get_scheduler(),
std::move(callback));
}

WFTimerTask *WFTaskFactory::create_timer_task(timer_callback_t callback)
{
return new __WFCanceledTimerTask(WFGlobal::get_scheduler(),
std::move(callback));
}

/* Deprecated. */
WFTimerTask *WFTaskFactory::create_timer_task(unsigned int microseconds,
timer_callback_t callback)
Expand Down Expand Up @@ -1096,7 +1120,7 @@ int WFTaskFactory::release_guard_safe(const std::string& name, void *msg)
if (!node)
return 0;

timer = WFTaskFactory::create_timer_task(0, 0, [node](WFTimerTask *timer) {
timer = WFTaskFactory::create_timer_task([node](WFTimerTask *timer) {
node->guard->WFConditional::signal(timer->user_data);
});
timer->user_data = msg;
Expand Down
11 changes: 7 additions & 4 deletions src/factory/WFTaskFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,21 +219,24 @@ class WFTaskFactory
static WFTimerTask *create_timer_task(time_t seconds, long nanoseconds,
timer_callback_t callback);

/* create a named timer. */
/* Create a named timer. */
static WFTimerTask *create_timer_task(const std::string& timer_name,
time_t seconds, long nanoseconds,
timer_callback_t callback);

/* cancel all timers under the name. */
/* Cancel all timers under the name. */
static int cancel_by_name(const std::string& timer_name)
{
return WFTaskFactory::cancel_by_name(timer_name, (size_t)-1);
}

/* cancel at most 'max' timers under the name. */
/* Cancel at most 'max' timers under the name. */
static int cancel_by_name(const std::string& timer_name, size_t max);

/* timer in microseconds (deprecated) */
/* Timer to be canceled immediately after started. */
static WFTimerTask *create_timer_task(timer_callback_t callback);

/* Timer in microseconds. (deprecated) */
static WFTimerTask *create_timer_task(unsigned int microseconds,
timer_callback_t callback);

Expand Down
4 changes: 1 addition & 3 deletions src/factory/WFTaskFactory.inl
Original file line number Diff line number Diff line change
Expand Up @@ -491,9 +491,7 @@ SubTask *WFComplexClientTask<REQ, RESP, CTX>::done()
auto&& cb = std::bind(&WFComplexClientTask::switch_callback,
this,
std::placeholders::_1);
WFTimerTask *timer;

timer = WFTaskFactory::create_timer_task(0, 0, std::move(cb));
WFTimerTask *timer = WFTaskFactory::create_timer_task(std::move(cb));
series->push_front(timer);
}
else
Expand Down
6 changes: 1 addition & 5 deletions src/kernel/poller.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,7 +1608,6 @@ int poller_add_timer(const struct timespec *value, void *context, void **timer,
int poller_del_timer(void *timer, poller_t *poller)
{
struct __poller_node *node = (struct __poller_node *)timer;
int stopped = 0;

pthread_mutex_lock(&poller->mutex);
if (!node->removed)
Expand All @@ -1622,9 +1621,6 @@ int poller_del_timer(void *timer, poller_t *poller)

node->error = 0;
node->state = PR_ST_DELETED;
stopped = poller->stopped;
if (!stopped)
write(poller->pipe_wr, &node, sizeof (void *));
}
else
{
Expand All @@ -1633,7 +1629,7 @@ int poller_del_timer(void *timer, poller_t *poller)
}

pthread_mutex_unlock(&poller->mutex);
if (stopped)
if (node)
poller->callback((struct poller_result *)node, poller->context);

return -!node;
Expand Down