From: Leandro Lucarella Date: Tue, 2 Jan 2007 18:13:42 +0000 (+0000) Subject: Fix priorities API. X-Git-Tag: 0.1~15 X-Git-Url: https://git.llucax.com/software/eventxx.git/commitdiff_plain/1df0df01b00d420fcfbcb4ad0c0a74e885c828df?ds=inline;hp=1b91677bf8d6ef6b68ee0fa6b9c37fa28bf428f2 Fix priorities API. Priorities were added in the event constructor, but event priority are reseted by libevent when they are associated with an event_base (event_base_set()), so it was useless. Now priorities are set in the dispatcher::add() method, so event_priority_set() y called just after event_base_set() and we can be all happy and have working priorities. --- diff --git a/eventxx b/eventxx index 080f40c..7143ae2 100644 --- a/eventxx +++ b/eventxx @@ -287,15 +287,10 @@ struct event: basic_event * @param fd File descriptor to monitor for events. * @param ev Type of events to monitor. * @param handler Callback functor. - * @param priority Priority of the event. */ - event(int fd, short ev, F& handler, int priority = DEFAULT_PRIORITY) - throw(invalid_priority) + event(int fd, short ev, F& handler) throw() { event_set(this, fd, ev, &wrapper, reinterpret_cast< void* >(&handler)); - if (priority != DEFAULT_PRIORITY - && event_priority_set(this, priority)) - throw invalid_priority(); } protected: @@ -325,16 +320,10 @@ struct event< ccallback_type >: basic_event * @param ev Type of events to monitor. * @param handler C-style callback function. * @param arg Arbitrary pointer to pass to the handler as argument. - * @param priority Priority of the event. */ - event(int fd, short ev, ccallback_type handler, void* arg, - int priority = DEFAULT_PRIORITY) - throw(invalid_priority) + event(int fd, short ev, ccallback_type handler, void* arg) throw() { event_set(this, fd, ev, handler, arg); - if (priority != DEFAULT_PRIORITY - && event_priority_set(this, priority)) - throw invalid_priority(); } protected: @@ -347,7 +336,7 @@ struct event< ccallback_type >: basic_event * Timer event object. * * This is just a special case of event that is fired only when a timeout is - * reached. It's just a shortcut to event(-1, 0, handler, priority). + * reached. It's just a shortcut to event(-1, 0, handler). * * @note This event can't EV_PERSIST. * @see timer< ccallback_type > @@ -360,16 +349,11 @@ struct timer: event< F > * Creates a new timer event. * * @param handler Callback functor. - * @param priority Priority of the event. */ - timer(F& handler, int priority = DEFAULT_PRIORITY) - throw(invalid_priority) + timer(F& handler) throw() { evtimer_set(this, &event< F >::wrapper, reinterpret_cast< void* >(&handler)); - if (priority != DEFAULT_PRIORITY - && event_priority_set(this, priority)) - throw invalid_priority(); } }; // struct timer< F > @@ -390,15 +374,10 @@ struct timer< ccallback_type >: event< ccallback_type > * * @param handler C-style callback function. * @param arg Arbitrary pointer to pass to the handler as argument. - * @param priority Priority of the event. */ - timer(ccallback_type handler, void* arg, int priority = DEFAULT_PRIORITY) - throw(invalid_priority) + timer(ccallback_type handler, void* arg) throw() { evtimer_set(this, handler, arg); - if (priority != DEFAULT_PRIORITY - && event_priority_set(this, priority)) - throw invalid_priority(); } }; // struct timer< ccallback_type > @@ -409,7 +388,7 @@ struct timer< ccallback_type >: event< ccallback_type > * * This is just a special case of event that is fired when a signal is raised * (instead of a file descriptor being active). It's just a shortcut to - * event(signal, EV_SIGNAL, handler, priority). + * event(signal, EV_SIGNAL, handler). * * @note This event allways EV_PERSIST. * @see signal< ccallback_type > @@ -423,16 +402,11 @@ struct signal: event< F > * * @param signum Signal number to monitor. * @param handler Callback functor. - * @param priority Priority of the event. */ - signal(int signum, F& handler, int priority = DEFAULT_PRIORITY) - throw(invalid_priority) + signal(int signum, F& handler) throw() { signal_set(this, signum, &event< F >::wrapper, reinterpret_cast< void* >(&handler)); - if (priority != DEFAULT_PRIORITY - && event_priority_set(this, priority)) - throw invalid_priority(); } /** @@ -464,16 +438,10 @@ struct signal< ccallback_type >: event< ccallback_type > * @param signum Signal number to monitor. * @param handler C-style callback function. * @param arg Arbitrary pointer to pass to the handler as argument. - * @param priority Priority of the event. */ - signal(int signum, ccallback_type handler, void* arg, - int priority = DEFAULT_PRIORITY) - throw(invalid_priority) + signal(int signum, ccallback_type handler, void* arg) throw() { signal_set(this, signum, handler, arg); - if (priority != DEFAULT_PRIORITY - && event_priority_set(this, priority)) - throw invalid_priority(); } /** @@ -542,10 +510,15 @@ struct dispatcher * Adds an event to the dispatcher. * * @param e Event to add. + * @param priority Priority of the event. */ - void add(basic_event& e) throw() + void add(basic_event& e, int priority = DEFAULT_PRIORITY) + throw(invalid_priority) { internal::event_base_set(_event_base, &e); + if (priority != DEFAULT_PRIORITY + && internal::event_priority_set(&e, priority)) + throw invalid_priority(); internal::event_add(&e, 0); } @@ -557,10 +530,16 @@ struct dispatcher * * @param e Event to add. * @param to Timeout. + * @param priority Priority of the event. */ - void add(basic_event& e, const time& to) throw() + void add(basic_event& e, const time& to, + int priority = DEFAULT_PRIORITY) + throw(invalid_priority) { internal::event_base_set(_event_base, &e); + if (priority != DEFAULT_PRIORITY + && internal::event_priority_set(&e, priority)) + throw invalid_priority(); internal::event_add(&e, const_cast< time* >(&to)); // XXX HACK libevent don't use const } diff --git a/test/Makefile b/test/Makefile index f582ca4..3b45f26 100644 --- a/test/Makefile +++ b/test/Makefile @@ -2,10 +2,10 @@ CXXFLAGS=-I.. -g -Wall # Uncomment this if you have a fixed event_base_free(). # See http://www.mail-archive.com/libevent-users@monkey.org/msg00112.html -CXXFLAGS+=-DEVENT_BASE_FREE_FIX +#CXXFLAGS+=-DEVENT_BASE_FREE_FIX LDFLAGS=-levent -targets=bench test-eof test-time test-weof trivial c-way +targets=bench test-eof test-time test-weof trivial c-way functor-way prio-test all: $(targets) diff --git a/test/prio-test.cpp b/test/prio-test.cpp new file mode 100644 index 0000000..cd6f73a --- /dev/null +++ b/test/prio-test.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + +using eventxx::dispatcher; + +#define OSASSERT(func, ...) if (func(__VA_ARGS__) == -1) { perror(#func "()"); exit(1); } + +struct handler +{ + dispatcher& d; + int fds[4]; + handler(dispatcher& d): d(d) + { + OSASSERT(pipe, fds); + OSASSERT(pipe, fds+2); + } + void operator() (int fd, short event) + { + char buf[7]; + OSASSERT(read, fd, buf, 7); + std::cout << "Read from fd " << fd << ": " << buf << "\n"; + d.exit(); + } +}; + +int main() +{ + dispatcher d(2); + handler h(d); + OSASSERT(write, h.fds[1], "hola 1", 7); + OSASSERT(write, h.fds[3], "hola 2", 7); + eventxx::event< handler > e1(h.fds[0], EV_READ, h); + eventxx::event< handler > e2(h.fds[2], EV_READ, h); + d.add(e1, 1); + d.add(e2, 0); + d.dispatch(); + return 0; +} +