Skip to content

Commit 91f679a

Browse files
authored
Add return value for WFTaskFactory::XXXX_by_name(). (#1627)
* Add return value for WFTaskFactory::XXXX_by_name(). * Update about-counter.md * Update about-timer.md
1 parent 9420446 commit 91f679a

4 files changed

Lines changed: 52 additions & 35 deletions

File tree

docs/about-counter.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,16 @@ int main(void)
160160
class WFTaskFactory
161161
{
162162
...
163-
static void count_by_name(const std::string& counter_name);
163+
static int count_by_name(const std::string& counter_name);
164164

165-
static void count_by_name(const std::string& counter_name, unsigned int n);
165+
static int count_by_name(const std::string& counter_name, unsigned int n);
166166
...
167167
};
168168
~~~
169169
WFTaskFactory::count_by_name方法还可以传入一个整数n,表示这一次操作要增加的计数值,显然:
170170
count_by_name("c1")等价于count_by_name("c1", 1)。
171171
如果"c1"计数器不存在(未创建或已经完成),那么对"c1"的操作不产生任何效果,因此不会有匿名计数器野指针的问题。
172+
函数的返回值表示被唤醒的计数器个数。当n大于1时,count_by_name操作可能让多个计数器达到目标值。
172173

173174
# 命名计数器详细行为定义
174175

docs/about-timer.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public:
1919
time_t seconds, long nanoseconds,
2020
timer_callback_t callback);
2121

22-
static void cancel_by_name(const std::string& timer_name)
22+
static int cancel_by_name(const std::string& timer_name)
2323
{
2424
cancel_by_name(const std::string& timer_name, (size_t)-1);
2525
}
2626

27-
static void cancel_by_name(const std::string& timer_name, size_t max);
27+
static int cancel_by_name(const std::string& timer_name, size_t max);
2828
};
2929
~~~
3030
我们通过seconds和nanoseconds两个参数来指定一个定时器的定时时间。其中,nanoseconds的取值范围在[0,1000000000)。
@@ -35,7 +35,8 @@ public:
3535
3636
如果在创建定时器任务时传入一个名称,那么这个定时器就可以在被提前中断。
3737
中断一个定时任务的方法是通过WFTaskFactory::cancel_by_name这个接口,这个接口默认情况下,会取消这个名称下的所有定时器。
38-
因此,我们也支持传入一个max参数,让操作最多取消max个定时器。当然,如果没有这个名称下的定时器,cancel操作不会产生任何效果。
38+
因此,我们也支持传入一个max参数,让操作最多取消max个定时器。无论哪个接口,返回值都是代表实际被取消的定时器个数。
39+
如果没有这个名称下的定时器,cancel操作不会产生任何效果,并返回0。
3940
定时器在被创建之后就可取消,并非一定要等它被启动之后。以这个代码为例:
4041
~~~cpp
4142
#include <stdio.h>

src/factory/WFTaskFactory.cc

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static class __NamedTimerMap
161161
timer_callback_t&& cb);
162162

163163
public:
164-
void cancel(const std::string& name, size_t max);
164+
int cancel(const std::string& name, size_t max);
165165

166166
private:
167167
struct rb_root root_;
@@ -272,10 +272,11 @@ WFTimerTask *__NamedTimerMap::create(const std::string& name,
272272
return task;
273273
}
274274

275-
void __NamedTimerMap::cancel(const std::string& name, size_t max)
275+
int __NamedTimerMap::cancel(const std::string& name, size_t max)
276276
{
277277
struct __timer_node *node;
278278
TimerList *timers;
279+
int ret = 0;
279280

280281
mutex_.lock();
281282
timers = __get_object_list<TimerList>(name, &root_, false);
@@ -296,6 +297,7 @@ void __NamedTimerMap::cancel(const std::string& name, size_t max)
296297

297298
node->task = NULL;
298299
max--;
300+
ret++;
299301
if (timers->empty())
300302
{
301303
rb_erase(&timers->rb, &root_);
@@ -306,6 +308,7 @@ void __NamedTimerMap::cancel(const std::string& name, size_t max)
306308

307309
mutex_.unlock();
308310
delete timers;
311+
return ret;
309312
}
310313

311314
WFTimerTask *WFTaskFactory::create_timer_task(const std::string& name,
@@ -317,9 +320,9 @@ WFTimerTask *WFTaskFactory::create_timer_task(const std::string& name,
317320
std::move(callback));
318321
}
319322

320-
void WFTaskFactory::cancel_by_name(const std::string& name, size_t max)
323+
int WFTaskFactory::cancel_by_name(const std::string& name, size_t max)
321324
{
322-
__timer_map.cancel(name, max);
325+
return __timer_map.cancel(name, max);
323326
}
324327

325328
/****************** Named Counter ******************/
@@ -342,7 +345,7 @@ static class __NamedCounterMap
342345
WFCounterTask *create(const std::string& name, unsigned int target_value,
343346
counter_callback_t&& cb);
344347

345-
void count_n(const std::string& name, unsigned int n);
348+
int count_n(const std::string& name, unsigned int n);
346349
void count(CounterList *counters, struct __counter_node *node);
347350

348351
void remove(CounterList *counters, struct __counter_node *node)
@@ -444,12 +447,13 @@ bool __NamedCounterMap::count_n_locked(CounterList *counters, unsigned int n,
444447
return false;
445448
}
446449

447-
void __NamedCounterMap::count_n(const std::string& name, unsigned int n)
450+
int __NamedCounterMap::count_n(const std::string& name, unsigned int n)
448451
{
449452
LIST_HEAD(task_list);
450453
struct __counter_node *node;
451454
CounterList *counters;
452455
bool erased = false;
456+
int ret = 0;
453457

454458
mutex_.lock();
455459
counters = __get_object_list<CounterList>(name, &root_, false);
@@ -465,7 +469,10 @@ void __NamedCounterMap::count_n(const std::string& name, unsigned int n)
465469
node = list_entry(task_list.next, struct __counter_node, list);
466470
list_del(&node->list);
467471
node->task->WFCounterTask::count();
472+
ret++;
468473
}
474+
475+
return ret;
469476
}
470477

471478
void __NamedCounterMap::count(CounterList *counters,
@@ -496,9 +503,9 @@ WFCounterTask *WFTaskFactory::create_counter_task(const std::string& name,
496503
return __counter_map.create(name, target_value, std::move(callback));
497504
}
498505

499-
void WFTaskFactory::count_by_name(const std::string& name, unsigned int n)
506+
int WFTaskFactory::count_by_name(const std::string& name, unsigned int n)
500507
{
501-
__counter_map.count_n(name, n);
508+
return __counter_map.count_n(name, n);
502509
}
503510

504511
/****************** Named Mailbox ******************/
@@ -521,7 +528,7 @@ static class __NamedMailboxMap
521528
mailbox_callback_t&& cb);
522529
WFMailboxTask *create(const std::string& name, mailbox_callback_t&& cb);
523530

524-
void send(const std::string& name, void *msg, size_t max);
531+
int send(const std::string& name, void *msg, size_t max);
525532
void send(MailboxList *mailboxes, struct __mailbox_node *node, void *msg);
526533

527534
void remove(MailboxList *mailboxes, struct __mailbox_node *node)
@@ -628,12 +635,13 @@ bool __NamedMailboxMap::send_max_locked(MailboxList *mailboxes,
628635
return true;
629636
}
630637

631-
void __NamedMailboxMap::send(const std::string& name, void *msg, size_t max)
638+
int __NamedMailboxMap::send(const std::string& name, void *msg, size_t max)
632639
{
633640
LIST_HEAD(task_list);
634641
struct __mailbox_node *node;
635642
MailboxList *mailboxes;
636643
bool erased = false;
644+
int ret = 0;
637645

638646
mutex_.lock();
639647
mailboxes = __get_object_list<MailboxList>(name, &root_, false);
@@ -649,7 +657,10 @@ void __NamedMailboxMap::send(const std::string& name, void *msg, size_t max)
649657
node = list_entry(task_list.next, struct __mailbox_node, list);
650658
list_del(&node->list);
651659
node->task->WFMailboxTask::send(msg);
660+
ret++;
652661
}
662+
663+
return ret;
653664
}
654665

655666
void __NamedMailboxMap::send(MailboxList *mailboxes,
@@ -680,9 +691,9 @@ WFMailboxTask *WFTaskFactory::create_mailbox_task(const std::string& name,
680691
return __mailbox_map.create(name, std::move(callback));
681692
}
682693

683-
void WFTaskFactory::send_by_name(const std::string& name, void *msg, size_t max)
694+
int WFTaskFactory::send_by_name(const std::string& name, void *msg, size_t max)
684695
{
685-
__mailbox_map.send(name, msg, max);
696+
return __mailbox_map.send(name, msg, max);
686697
}
687698

688699
/****************** Named Conditional ******************/
@@ -705,7 +716,7 @@ static class __NamedConditionalMap
705716
void **msgbuf);
706717
WFConditional *create(const std::string& name, SubTask *task);
707718

708-
void signal(const std::string& name, void *msg, size_t max);
719+
int signal(const std::string& name, void *msg, size_t max);
709720
void signal(ConditionalList *conds, struct __conditional_node *node,
710721
void *msg);
711722

@@ -812,12 +823,13 @@ bool __NamedConditionalMap::signal_max_locked(ConditionalList *conds,
812823
return true;
813824
}
814825

815-
void __NamedConditionalMap::signal(const std::string& name, void *msg, size_t max)
826+
int __NamedConditionalMap::signal(const std::string& name, void *msg, size_t max)
816827
{
817828
LIST_HEAD(cond_list);
818829
struct __conditional_node *node;
819830
ConditionalList *conds;
820831
bool erased = false;
832+
int ret = 0;
821833

822834
mutex_.lock();
823835
conds = __get_object_list<ConditionalList>(name, &root_, false);
@@ -833,7 +845,10 @@ void __NamedConditionalMap::signal(const std::string& name, void *msg, size_t ma
833845
node = list_entry(cond_list.next, struct __conditional_node, list);
834846
list_del(&node->list);
835847
node->cond->WFConditional::signal(msg);
848+
ret++;
836849
}
850+
851+
return ret;
837852
}
838853

839854
void __NamedConditionalMap::signal(ConditionalList *conds,
@@ -863,10 +878,10 @@ WFConditional *WFTaskFactory::create_conditional(const std::string& name,
863878
return __conditional_map.create(name, task);
864879
}
865880

866-
void WFTaskFactory::signal_by_name(const std::string& name, void *msg,
881+
int WFTaskFactory::signal_by_name(const std::string& name, void *msg,
867882
size_t max)
868883
{
869-
__conditional_map.signal(name, msg, max);
884+
return __conditional_map.signal(name, msg, max);
870885
}
871886

872887
/****************** Named Guard ******************/

src/factory/WFTaskFactory.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,13 +228,13 @@ class WFTaskFactory
228228
timer_callback_t callback);
229229

230230
/* cancel all timers under the name. */
231-
static void cancel_by_name(const std::string& timer_name)
231+
static int cancel_by_name(const std::string& timer_name)
232232
{
233-
WFTaskFactory::cancel_by_name(timer_name, (size_t)-1);
233+
return WFTaskFactory::cancel_by_name(timer_name, (size_t)-1);
234234
}
235235

236236
/* cancel at most 'max' timers under the name. */
237-
static void cancel_by_name(const std::string& timer_name, size_t max);
237+
static int cancel_by_name(const std::string& timer_name, size_t max);
238238

239239
/* timer in microseconds (deprecated) */
240240
static WFTimerTask *create_timer_task(unsigned int microseconds,
@@ -258,15 +258,15 @@ class WFTaskFactory
258258
* exceeding target_value. When multiple counters share a same name,
259259
* this operation will be performed on the first created. If no counter
260260
* matches the name, nothing is performed. */
261-
static void count_by_name(const std::string& counter_name)
261+
static int count_by_name(const std::string& counter_name)
262262
{
263-
WFTaskFactory::count_by_name(counter_name, 1);
263+
return WFTaskFactory::count_by_name(counter_name, 1);
264264
}
265265

266266
/* Count by name with a value n. When multiple counters share this name,
267267
* the operation is performed on the counters in the sequence of its
268268
* creation, and more than one counter may reach target value. */
269-
static void count_by_name(const std::string& counter_name, unsigned int n);
269+
static int count_by_name(const std::string& counter_name, unsigned int n);
270270

271271
public:
272272
static WFMailboxTask *create_mailbox_task(void **mailbox,
@@ -290,13 +290,13 @@ class WFTaskFactory
290290

291291
/* The 'msg' will be sent to the all mailbox tasks under the name, and
292292
* would be lost if no task matched. */
293-
static void send_by_name(const std::string& mailbox_name, void *msg)
293+
static int send_by_name(const std::string& mailbox_name, void *msg)
294294
{
295-
WFTaskFactory::send_by_name(mailbox_name, msg, (size_t)-1);
295+
return WFTaskFactory::send_by_name(mailbox_name, msg, (size_t)-1);
296296
}
297297

298-
static void send_by_name(const std::string& mailbox_name, void *msg,
299-
size_t max);
298+
static int send_by_name(const std::string& mailbox_name, void *msg,
299+
size_t max);
300300

301301
public:
302302
static WFSelectorTask *create_selector_task(size_t candidates,
@@ -322,13 +322,13 @@ class WFTaskFactory
322322
static WFConditional *create_conditional(const std::string& cond_name,
323323
SubTask *task);
324324

325-
static void signal_by_name(const std::string& cond_name, void *msg)
325+
static int signal_by_name(const std::string& cond_name, void *msg)
326326
{
327-
WFTaskFactory::signal_by_name(cond_name, msg, (size_t)-1);
327+
return WFTaskFactory::signal_by_name(cond_name, msg, (size_t)-1);
328328
}
329329

330-
static void signal_by_name(const std::string& cond_name, void *msg,
331-
size_t max);
330+
static int signal_by_name(const std::string& cond_name, void *msg,
331+
size_t max);
332332

333333
public:
334334
static WFConditional *create_guard(const std::string& resource_name,

0 commit comments

Comments
 (0)