Add some internal convenience functions.

This commit is contained in:
Todd C. Miller
2014-08-29 09:44:08 -06:00
parent ef11361eea
commit e9370718f9
4 changed files with 46 additions and 17 deletions

View File

@@ -147,6 +147,9 @@ __dso_public bool sudo_ev_got_break_v1(struct sudo_event_base *base);
/* Magic pointer value to use self pointer as callback arg. */ /* Magic pointer value to use self pointer as callback arg. */
#define sudo_ev_self_cbarg() ((void *)-1) #define sudo_ev_self_cbarg() ((void *)-1)
/* Add an event to the base's active queue and mark it active (internal). */
void sudo_ev_activate(struct sudo_event_base *base, struct sudo_event *ev);
/* /*
* Backend implementation. * Backend implementation.
*/ */

View File

@@ -52,6 +52,38 @@
/* XXX - use non-exiting allocators? */ /* XXX - use non-exiting allocators? */
/*
* Add an event to the base's active queue and mark it active.
*/
void
sudo_ev_activate(struct sudo_event_base *base, struct sudo_event *ev)
{
TAILQ_INSERT_TAIL(&base->active, ev, active_entries);
SET(ev->flags, SUDO_EVQ_ACTIVE);
}
/*
* Remove an event from the base's active queue and mark it inactive.
*/
static inline void
sudo_ev_deactivate(struct sudo_event_base *base, struct sudo_event *ev)
{
CLR(ev->flags, SUDO_EVQ_ACTIVE);
TAILQ_REMOVE(&base->active, ev, active_entries);
}
/*
* Clear out the base's active queue and mark all events as inactive.
*/
static void
sudo_ev_deactivate_all(struct sudo_event_base *base)
{
struct sudo_event *ev;
while ((ev = TAILQ_FIRST(&base->active)) != NULL)
sudo_ev_deactivate(base, ev);
}
struct sudo_event_base * struct sudo_event_base *
sudo_ev_base_alloc_v1(void) sudo_ev_base_alloc_v1(void)
{ {
@@ -305,8 +337,7 @@ rescan:
*/ */
while ((ev = TAILQ_FIRST(&base->active)) != NULL) { while ((ev = TAILQ_FIRST(&base->active)) != NULL) {
/* Pop first event off the active queue. */ /* Pop first event off the active queue. */
CLR(ev->flags, SUDO_EVQ_ACTIVE); sudo_ev_deactivate(base, ev);
TAILQ_REMOVE(&base->active, ev, active_entries);
/* Remove from base unless persistent. */ /* Remove from base unless persistent. */
if (!ISSET(ev->events, SUDO_EV_PERSIST)) if (!ISSET(ev->events, SUDO_EV_PERSIST))
sudo_ev_del(base, ev); sudo_ev_del(base, ev);
@@ -315,20 +346,14 @@ rescan:
if (ISSET(base->flags, SUDO_EVBASE_LOOPBREAK)) { if (ISSET(base->flags, SUDO_EVBASE_LOOPBREAK)) {
/* Stop processing events immediately. */ /* Stop processing events immediately. */
SET(base->flags, SUDO_EVBASE_GOT_BREAK); SET(base->flags, SUDO_EVBASE_GOT_BREAK);
while ((ev = TAILQ_FIRST(&base->active)) != NULL) { sudo_ev_deactivate_all(base);
CLR(ev->flags, SUDO_EVQ_ACTIVE);
TAILQ_REMOVE(&base->active, ev, active_entries);
}
goto done; goto done;
} }
if (ISSET(base->flags, SUDO_EVBASE_LOOPCONT)) { if (ISSET(base->flags, SUDO_EVBASE_LOOPCONT)) {
/* Rescan events and start polling again. */ /* Rescan events and start polling again. */
CLR(base->flags, SUDO_EVBASE_LOOPCONT); CLR(base->flags, SUDO_EVBASE_LOOPCONT);
if (!ISSET(flags, SUDO_EVLOOP_ONCE)) { if (!ISSET(flags, SUDO_EVLOOP_ONCE)) {
while ((ev = TAILQ_FIRST(&base->active)) != NULL) { sudo_ev_deactivate_all(base);
CLR(ev->flags, SUDO_EVQ_ACTIVE);
TAILQ_REMOVE(&base->active, ev, active_entries);
}
goto rescan; goto rescan;
} }
} }
@@ -336,10 +361,13 @@ rescan:
if (ISSET(base->flags, SUDO_EVBASE_LOOPEXIT)) { if (ISSET(base->flags, SUDO_EVBASE_LOOPEXIT)) {
/* exit loop after once through */ /* exit loop after once through */
SET(base->flags, SUDO_EVBASE_GOT_EXIT); SET(base->flags, SUDO_EVBASE_GOT_EXIT);
goto done; sudo_ev_deactivate_all(base);
}
if (ISSET(flags, SUDO_EVLOOP_ONCE))
break; break;
}
if (ISSET(flags, SUDO_EVLOOP_ONCE)) {
sudo_ev_deactivate_all(base);
break;
}
} }
done: done:
base->flags &= SUDO_EVBASE_GOT_MASK; base->flags &= SUDO_EVBASE_GOT_MASK;

View File

@@ -173,8 +173,7 @@ sudo_ev_scan_impl(struct sudo_event_base *base, int flags)
"%s: polled fd %d, events %d, activating %p", "%s: polled fd %d, events %d, activating %p",
__func__, ev->fd, what, ev); __func__, ev->fd, what, ev);
ev->revents = what; ev->revents = what;
TAILQ_INSERT_TAIL(&base->active, ev, active_entries); sudo_ev_activate(base, ev);
SET(ev->flags, SUDO_EVQ_ACTIVE);
} }
} }
break; break;

View File

@@ -201,8 +201,7 @@ sudo_ev_scan_impl(struct sudo_event_base *base, int flags)
"%s: selected fd %d, events %d, activating %p", "%s: selected fd %d, events %d, activating %p",
__func__, ev->fd, what, ev); __func__, ev->fd, what, ev);
ev->revents = what; ev->revents = what;
TAILQ_INSERT_TAIL(&base->active, ev, active_entries); sudo_ev_activate(base, ev);
SET(ev->flags, SUDO_EVQ_ACTIVE);
} }
} }
} }