mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-13 06:16:10 -04:00
Added event_base_get_num_events()
This commit is contained in:
parent
90a8c539aa
commit
0fa107d8cb
21
event.c
21
event.c
@ -1183,6 +1183,27 @@ event_base_get_npriorities(struct event_base *base)
|
||||
return (n);
|
||||
}
|
||||
|
||||
int
|
||||
event_base_get_num_events(struct event_base *base, unsigned int type)
|
||||
{
|
||||
int r = 0;
|
||||
|
||||
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
||||
|
||||
if (type & EVENT_BASE_COUNT_ACTIVE)
|
||||
r += base->event_count_active;
|
||||
|
||||
if (type & EVENT_BASE_COUNT_VIRTUAL)
|
||||
r += base->virtual_event_count;
|
||||
|
||||
if (type & EVENT_BASE_COUNT_ADDED)
|
||||
r += base->event_count;
|
||||
|
||||
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
||||
|
||||
return r;
|
||||
}
|
||||
|
||||
/* Returns true iff we're currently watching any events. */
|
||||
static int
|
||||
event_haveevents(struct event_base *base)
|
||||
|
@ -385,6 +385,43 @@ const char *event_base_get_method(const struct event_base *);
|
||||
*/
|
||||
const char **event_get_supported_methods(void);
|
||||
|
||||
/**
|
||||
@name event type flag
|
||||
|
||||
Flags to pass to event_base_get_num_events() to specify the kinds of events
|
||||
we want to aggregate counts for
|
||||
*/
|
||||
/**@{*/
|
||||
/** count the number of active events, which have been triggered.*/
|
||||
#define EVENT_BASE_COUNT_ACTIVE 1U
|
||||
/** count the number of virtual events, which is used to represent an internal
|
||||
* condition, other than a pending event, that keeps the loop from exiting. */
|
||||
#define EVENT_BASE_COUNT_VIRTUAL 2U
|
||||
/** count the number of events which have been added to event base, including
|
||||
* internal events. */
|
||||
#define EVENT_BASE_COUNT_ADDED 4U
|
||||
/**@}*/
|
||||
|
||||
/**
|
||||
Gets the number of events in event_base, as specified in the flags.
|
||||
|
||||
Since event base has some internal events added to make some of its
|
||||
functionalities work, EVENT_BASE_COUNT_ADDED may return more than the
|
||||
number of events you added using event_add().
|
||||
|
||||
If you pass EVENT_BASE_COUNT_ACTIVE and EVENT_BASE_COUNT_ADDED together, an
|
||||
active event will be counted twice. However, this might not be the case in
|
||||
future libevent versions. The return value is an indication of the work
|
||||
load, but the user shouldn't rely on the exact value as this may change in
|
||||
the future.
|
||||
|
||||
@param eb the event_base structure returned by event_base_new()
|
||||
@param flags a bitwise combination of the kinds of events to aggregate
|
||||
counts for
|
||||
@return the number of events specified in the flags
|
||||
*/
|
||||
int event_base_get_num_events(struct event_base *, unsigned int);
|
||||
|
||||
/**
|
||||
Allocates a new event configuration object.
|
||||
|
||||
|
125
test/regress.c
125
test/regress.c
@ -1311,6 +1311,130 @@ test_event_assign_selfarg(void *ptr)
|
||||
event_base_dispatch(base);
|
||||
}
|
||||
|
||||
static void
|
||||
test_event_base_get_num_events(void *ptr)
|
||||
{
|
||||
struct basic_test_data *data = ptr;
|
||||
struct event_base *base = data->base;
|
||||
struct event ev;
|
||||
int event_count_active;
|
||||
int event_count_virtual;
|
||||
int event_count_added;
|
||||
int event_count_active_virtual;
|
||||
int event_count_active_added;
|
||||
int event_count_virtual_added;
|
||||
int event_count_active_added_virtual;
|
||||
|
||||
struct timeval qsec = {0, 100000};
|
||||
|
||||
event_assign(&ev, base, -1, EV_READ, event_selfarg_cb,
|
||||
event_self_cbarg());
|
||||
|
||||
event_add(&ev, &qsec);
|
||||
event_count_active = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE);
|
||||
event_count_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_active_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_virtual_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_added_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|
|
||||
EVENT_BASE_COUNT_ADDED|
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
tt_int_op(event_count_active, ==, 0);
|
||||
tt_int_op(event_count_virtual, ==, 0);
|
||||
/* libevent itself adds a timeout event, so the event_count is 2 here */
|
||||
tt_int_op(event_count_added, ==, 2);
|
||||
tt_int_op(event_count_active_virtual, ==, 0);
|
||||
tt_int_op(event_count_active_added, ==, 2);
|
||||
tt_int_op(event_count_virtual_added, ==, 2);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 2);
|
||||
|
||||
event_active(&ev, EV_READ, 1);
|
||||
event_count_active = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE);
|
||||
event_count_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_active_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_virtual_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_added_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|
|
||||
EVENT_BASE_COUNT_ADDED|
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
tt_int_op(event_count_active, ==, 1);
|
||||
tt_int_op(event_count_virtual, ==, 0);
|
||||
tt_int_op(event_count_added, ==, 3);
|
||||
tt_int_op(event_count_active_virtual, ==, 1);
|
||||
tt_int_op(event_count_active_added, ==, 4);
|
||||
tt_int_op(event_count_virtual_added, ==, 3);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 4);
|
||||
|
||||
event_base_loop(base, 0);
|
||||
event_count_active = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE);
|
||||
event_count_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_active_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_virtual_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_added_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|
|
||||
EVENT_BASE_COUNT_ADDED|
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
tt_int_op(event_count_active, ==, 0);
|
||||
tt_int_op(event_count_virtual, ==, 0);
|
||||
tt_int_op(event_count_added, ==, 0);
|
||||
tt_int_op(event_count_active_virtual, ==, 0);
|
||||
tt_int_op(event_count_active_added, ==, 0);
|
||||
tt_int_op(event_count_virtual_added, ==, 0);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 0);
|
||||
|
||||
event_base_add_virtual_(base);
|
||||
event_count_active = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE);
|
||||
event_count_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_VIRTUAL);
|
||||
event_count_active_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_virtual_added = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_VIRTUAL|EVENT_BASE_COUNT_ADDED);
|
||||
event_count_active_added_virtual = event_base_get_num_events(base,
|
||||
EVENT_BASE_COUNT_ACTIVE|
|
||||
EVENT_BASE_COUNT_ADDED|
|
||||
EVENT_BASE_COUNT_VIRTUAL);
|
||||
tt_int_op(event_count_active, ==, 0);
|
||||
tt_int_op(event_count_virtual, ==, 1);
|
||||
tt_int_op(event_count_added, ==, 0);
|
||||
tt_int_op(event_count_active_virtual, ==, 1);
|
||||
tt_int_op(event_count_active_added, ==, 0);
|
||||
tt_int_op(event_count_virtual_added, ==, 1);
|
||||
tt_int_op(event_count_active_added_virtual, ==, 1);
|
||||
|
||||
end:
|
||||
;
|
||||
}
|
||||
|
||||
static void
|
||||
test_bad_assign(void *ptr)
|
||||
{
|
||||
@ -2850,6 +2974,7 @@ struct testcase_t main_testcases[] = {
|
||||
BASIC(manipulate_active_events, TT_FORK|TT_NEED_BASE),
|
||||
BASIC(event_new_selfarg, TT_FORK|TT_NEED_BASE),
|
||||
BASIC(event_assign_selfarg, TT_FORK|TT_NEED_BASE),
|
||||
BASIC(event_base_get_num_events, TT_FORK|TT_NEED_BASE),
|
||||
|
||||
BASIC(bad_assign, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
|
||||
BASIC(bad_reentrant, TT_FORK|TT_NEED_BASE|TT_NO_LOGS),
|
||||
|
Loading…
x
Reference in New Issue
Block a user